Skip to content

Commit 7fe58db

Browse files
cbowman0Civil
authored andcommitted
In DateParamToEpoch handle tz parameter
1 parent cdab347 commit 7fe58db

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

date/date.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,30 @@ func DateParamToEpoch(s, qtz string, d int64, defaultTimeZone *time.Location) in
5353
return d
5454
}
5555

56+
var tz = defaultTimeZone
57+
if qtz != "" {
58+
if z, err := time.LoadLocation(qtz); err == nil {
59+
tz = z
60+
}
61+
}
62+
5663
// relative timestamp
5764
if s[0] == '-' {
5865
offset, err := parser.IntervalString(s, -1)
5966
if err != nil {
6067
return d
6168
}
6269

63-
return timeNow().Add(time.Duration(offset) * time.Second).Unix()
70+
return timeNow().In(tz).Add(time.Duration(offset) * time.Second).Unix()
6471
}
6572

6673
switch s {
6774
case "now":
68-
return timeNow().Unix()
75+
return timeNow().In(tz).Unix()
6976
case "midnight", "noon", "teatime":
70-
yy, mm, dd := timeNow().Date()
77+
yy, mm, dd := timeNow().In(tz).Date()
7178
hh, min, _ := parseTime(s) // error ignored, we know it's valid
72-
dt := time.Date(yy, mm, dd, hh, min, 0, 0, defaultTimeZone)
79+
dt := time.Date(yy, mm, dd, hh, min, 0, 0, tz)
7380
return dt.Unix()
7481
}
7582

@@ -93,23 +100,16 @@ func DateParamToEpoch(s, qtz string, d int64, defaultTimeZone *time.Location) in
93100
return d
94101
}
95102

96-
var tz = defaultTimeZone
97-
if qtz != "" {
98-
if z, err := time.LoadLocation(qtz); err != nil {
99-
tz = z
100-
}
101-
}
102-
103103
var t time.Time
104104
dateStringSwitch:
105105
switch ds {
106106
case "today":
107-
t = timeNow()
107+
t = timeNow().In(tz)
108108
// nothing
109109
case "yesterday":
110-
t = timeNow().AddDate(0, 0, -1)
110+
t = timeNow().In(tz).AddDate(0, 0, -1)
111111
case "tomorrow":
112-
t = timeNow().AddDate(0, 0, 1)
112+
t = timeNow().In(tz).AddDate(0, 0, 1)
113113
default:
114114
for _, format := range TimeFormats {
115115
t, err = time.ParseInLocation(format, ds, tz)
@@ -128,7 +128,7 @@ dateStringSwitch:
128128
}
129129

130130
yy, mm, dd := t.Date()
131-
t = time.Date(yy, mm, dd, hour, minute, 0, 0, defaultTimeZone)
131+
t = time.Date(yy, mm, dd, hour, minute, 0, 0, tz)
132132

133133
return t.Unix()
134134
}

date/date_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestDateParamToEpoch(t *testing.T) {
1010

11-
defaultTimeZone := time.Local
11+
defaultTimeZone := time.UTC
1212
timeNow = func() time.Time {
1313
//16 Aug 1994 15:30
1414
return time.Date(1994, time.August, 16, 15, 30, 0, 100, defaultTimeZone)
@@ -20,22 +20,22 @@ func TestDateParamToEpoch(t *testing.T) {
2020
input string
2121
output string
2222
}{
23-
{"midnight", "00:00 1994-Aug-16"},
24-
{"noon", "12:00 1994-Aug-16"},
25-
{"teatime", "16:00 1994-Aug-16"},
26-
{"tomorrow", "00:00 1994-Aug-17"},
23+
{"midnight", "07:00 1994-Aug-16"},
24+
{"noon", "19:00 1994-Aug-16"},
25+
{"teatime", "23:00 1994-Aug-16"},
26+
{"tomorrow", "07:00 1994-Aug-17"},
2727

28-
{"noon 08/12/94", "12:00 1994-Aug-12"},
29-
{"midnight 20060812", "00:00 2006-Aug-12"},
30-
{"noon tomorrow", "12:00 1994-Aug-17"},
28+
{"noon 08/12/94", "19:00 1994-Aug-12"},
29+
{"midnight 20060812", "07:00 2006-Aug-12"},
30+
{"noon tomorrow", "19:00 1994-Aug-17"},
3131

32-
{"17:04 19940812", "17:04 1994-Aug-12"},
32+
{"17:04 19940812", "00:04 1994-Aug-13"},
3333
{"-1day", "15:30 1994-Aug-15"},
34-
{"19940812", "00:00 1994-Aug-12"},
34+
{"19940812", "07:00 1994-Aug-12"},
3535
}
3636

3737
for _, tt := range tests {
38-
got := DateParamToEpoch(tt.input, "Local", 0, defaultTimeZone)
38+
got := DateParamToEpoch(tt.input, "America/Los_Angeles", 0, defaultTimeZone)
3939
ts, err := time.ParseInLocation(shortForm, tt.output, defaultTimeZone)
4040
if err != nil {
4141
panic(fmt.Sprintf("error parsing time: %q: %v", tt.output, err))

0 commit comments

Comments
 (0)