Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In date.DateParamToEpoch handle tz parameter #853

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,30 @@ 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)
if err != nil {
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()
}

Expand All @@ -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)
Expand All @@ -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()
}
22 changes: 11 additions & 11 deletions date/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
Loading