diff --git a/pkg/utils/date.go b/pkg/utils/date.go index 9e8c84445..f0303a9c4 100644 --- a/pkg/utils/date.go +++ b/pkg/utils/date.go @@ -57,7 +57,7 @@ func formatSecondsAgo(secondsAgo int64) string { // formats the date in a smart way, if the date is today, it will show the time, otherwise it will show the date func UnixToDateSmart(now time.Time, timestamp int64, longTimeFormat string, shortTimeFormat string) string { - date := time.Unix(timestamp, 0) + date := time.Unix(timestamp, 0).In(now.Location()) if date.Day() == now.Day() && date.Month() == now.Month() && date.Year() == now.Year() { return date.Format(shortTimeFormat) diff --git a/pkg/utils/date_test.go b/pkg/utils/date_test.go index 0162f5f67..28ad65df8 100644 --- a/pkg/utils/date_test.go +++ b/pkg/utils/date_test.go @@ -2,6 +2,9 @@ package utils import ( "testing" + "time" + + "github.com/stretchr/testify/assert" ) func TestFormatSecondsAgo(t *testing.T) { @@ -95,3 +98,18 @@ func TestFormatSecondsAgo(t *testing.T) { }) } } + +func TestUnixToDateSmart_UsesNowLocationForFormatting(t *testing.T) { + timestamp := int64(1577844184) // 2020-01-01 02:03:04 UTC + now := time.Date(2020, 1, 1, 5, 3, 4, 0, time.UTC) + + assert.Equal(t, "2:03AM", UnixToDateSmart(now, timestamp, "2006-01-02", "3:04PM")) +} + +func TestUnixToDateSmart_SameTimestampDifferentNowLocation(t *testing.T) { + timestamp := int64(1577844184) // 2020-01-01 02:03:04 UTC + loc := time.FixedZone("UTC+1", 3600) + now := time.Date(2020, 1, 1, 6, 3, 4, 0, loc) + + assert.Equal(t, "3:03AM", UnixToDateSmart(now, timestamp, "2006-01-02", "3:04PM")) +}