From 421b68f9364b211c8d2e271559bba7cc498ac4d7 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 14 Apr 2024 22:17:01 +0100 Subject: [PATCH] Improve timezone handling --- cif/helpers.go | 12 +++--------- cif/helpers_test.go | 2 ++ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/cif/helpers.go b/cif/helpers.go index 79a0fbd..b2a8f6a 100644 --- a/cif/helpers.go +++ b/cif/helpers.go @@ -10,13 +10,8 @@ import ( // Fetches the day string for the provided date. func getDayString(t time.Time) string { - london, err := time.LoadLocation("Europe/London") - if err != nil { - log.Error("Unable to load time zone info", zap.Error(err)) - } - - timeNow := t.In(london) - day := timeNow.Weekday() + time := t.In(londonTimezone) + day := time.Weekday() dayStrings := [...]string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"} @@ -44,7 +39,6 @@ func isSameToday(t time.Time) bool { // Returns how many days ago `t` was compared to today func howManyDaysAgo(t time.Time) int { - log.Debug("Calculating how many days ago", zap.Time("Input time", t)) // Truncate both times to midnight in UTC timezone today := time.Now().UTC().Truncate(24 * time.Hour) input := t.UTC().Truncate(24 * time.Hour) @@ -89,7 +83,7 @@ func ParseCifDate(input *string, startOrEnd string) time.Time { return time.Time{} } - return t + return t.UTC() } // Parses CIF days_run field and converts to array of day strings diff --git a/cif/helpers_test.go b/cif/helpers_test.go index 4cf734a..d23f12b 100644 --- a/cif/helpers_test.go +++ b/cif/helpers_test.go @@ -103,6 +103,8 @@ func TestParseCifDate(t *testing.T) { for _, tc := range testCases { result := ParseCifDate(&tc.dateString, tc.startOrEnd) + result = result.In(londonTimezone) + //fmt.Println(tc.dateString, "|UTC: ", result.In(time.UTC), "|EU/Lon: ", result) if result != tc.expect { t.Errorf("For datestring %s, startOrEnd %s, expected %s, but got %s", tc.dateString, tc.startOrEnd, tc.expect.Format(layout), result.Format(layout)) }