From 7fe58db1026670ff062908fe37abf823f4999fd2 Mon Sep 17 00:00:00 2001 From: Christopher Bowman Date: Mon, 13 Jan 2025 15:15:55 -0500 Subject: [PATCH] In DateParamToEpoch handle tz parameter --- date/date.go | 30 +++++++++++++++--------------- date/date_test.go | 22 +++++++++++----------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/date/date.go b/date/date.go index c93478d83..783ed6676 100644 --- a/date/date.go +++ b/date/date.go @@ -53,6 +53,13 @@ func DateParamToEpoch(s, qtz string, d int64, defaultTimeZone *time.Location) in return d } + var tz = defaultTimeZone + if qtz != "" { + if z, err := time.LoadLocation(qtz); err == nil { + tz = z + } + } + // relative timestamp if s[0] == '-' { offset, err := parser.IntervalString(s, -1) @@ -60,16 +67,16 @@ func DateParamToEpoch(s, qtz string, d int64, defaultTimeZone *time.Location) in return d } - return timeNow().Add(time.Duration(offset) * time.Second).Unix() + return timeNow().In(tz).Add(time.Duration(offset) * time.Second).Unix() } switch s { case "now": - return timeNow().Unix() + return timeNow().In(tz).Unix() case "midnight", "noon", "teatime": - yy, mm, dd := timeNow().Date() + yy, mm, dd := timeNow().In(tz).Date() hh, min, _ := parseTime(s) // error ignored, we know it's valid - dt := time.Date(yy, mm, dd, hh, min, 0, 0, defaultTimeZone) + dt := time.Date(yy, mm, dd, hh, min, 0, 0, tz) return dt.Unix() } @@ -93,23 +100,16 @@ func DateParamToEpoch(s, qtz string, d int64, defaultTimeZone *time.Location) in return d } - var tz = defaultTimeZone - if qtz != "" { - if z, err := time.LoadLocation(qtz); err != nil { - tz = z - } - } - var t time.Time dateStringSwitch: switch ds { case "today": - t = timeNow() + t = timeNow().In(tz) // nothing case "yesterday": - t = timeNow().AddDate(0, 0, -1) + t = timeNow().In(tz).AddDate(0, 0, -1) case "tomorrow": - t = timeNow().AddDate(0, 0, 1) + t = timeNow().In(tz).AddDate(0, 0, 1) default: for _, format := range TimeFormats { t, err = time.ParseInLocation(format, ds, tz) @@ -128,7 +128,7 @@ dateStringSwitch: } yy, mm, dd := t.Date() - t = time.Date(yy, mm, dd, hour, minute, 0, 0, defaultTimeZone) + t = time.Date(yy, mm, dd, hour, minute, 0, 0, tz) return t.Unix() } diff --git a/date/date_test.go b/date/date_test.go index 3e8ef92c4..82f1af370 100644 --- a/date/date_test.go +++ b/date/date_test.go @@ -8,7 +8,7 @@ import ( func TestDateParamToEpoch(t *testing.T) { - defaultTimeZone := time.Local + defaultTimeZone := time.UTC timeNow = func() time.Time { //16 Aug 1994 15:30 return time.Date(1994, time.August, 16, 15, 30, 0, 100, defaultTimeZone) @@ -20,22 +20,22 @@ func TestDateParamToEpoch(t *testing.T) { input string output string }{ - {"midnight", "00:00 1994-Aug-16"}, - {"noon", "12:00 1994-Aug-16"}, - {"teatime", "16:00 1994-Aug-16"}, - {"tomorrow", "00:00 1994-Aug-17"}, + {"midnight", "07:00 1994-Aug-16"}, + {"noon", "19:00 1994-Aug-16"}, + {"teatime", "23:00 1994-Aug-16"}, + {"tomorrow", "07:00 1994-Aug-17"}, - {"noon 08/12/94", "12:00 1994-Aug-12"}, - {"midnight 20060812", "00:00 2006-Aug-12"}, - {"noon tomorrow", "12:00 1994-Aug-17"}, + {"noon 08/12/94", "19:00 1994-Aug-12"}, + {"midnight 20060812", "07:00 2006-Aug-12"}, + {"noon tomorrow", "19:00 1994-Aug-17"}, - {"17:04 19940812", "17:04 1994-Aug-12"}, + {"17:04 19940812", "00:04 1994-Aug-13"}, {"-1day", "15:30 1994-Aug-15"}, - {"19940812", "00:00 1994-Aug-12"}, + {"19940812", "07:00 1994-Aug-12"}, } for _, tt := range tests { - got := DateParamToEpoch(tt.input, "Local", 0, defaultTimeZone) + got := DateParamToEpoch(tt.input, "America/Los_Angeles", 0, defaultTimeZone) ts, err := time.ParseInLocation(shortForm, tt.output, defaultTimeZone) if err != nil { panic(fmt.Sprintf("error parsing time: %q: %v", tt.output, err))