Improve timezone handling

This commit is contained in:
Fred Boniface 2024-04-14 22:17:01 +01:00
parent 76f206441a
commit 421b68f936
2 changed files with 5 additions and 9 deletions

View File

@ -10,13 +10,8 @@ import (
// Fetches the day string for the provided date. // Fetches the day string for the provided date.
func getDayString(t time.Time) string { func getDayString(t time.Time) string {
london, err := time.LoadLocation("Europe/London") time := t.In(londonTimezone)
if err != nil { day := time.Weekday()
log.Error("Unable to load time zone info", zap.Error(err))
}
timeNow := t.In(london)
day := timeNow.Weekday()
dayStrings := [...]string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"} 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 // Returns how many days ago `t` was compared to today
func howManyDaysAgo(t time.Time) int { 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 // Truncate both times to midnight in UTC timezone
today := time.Now().UTC().Truncate(24 * time.Hour) today := time.Now().UTC().Truncate(24 * time.Hour)
input := t.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 time.Time{}
} }
return t return t.UTC()
} }
// Parses CIF days_run field and converts to array of day strings // Parses CIF days_run field and converts to array of day strings

View File

@ -103,6 +103,8 @@ func TestParseCifDate(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
result := ParseCifDate(&tc.dateString, tc.startOrEnd) 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 { 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)) t.Errorf("For datestring %s, startOrEnd %s, expected %s, but got %s", tc.dateString, tc.startOrEnd, tc.expect.Format(layout), result.Format(layout))
} }