Skip to content

Commit 90dde5d

Browse files
KimMachineGungopherbot
authored andcommitted
time: fix timezone lookup logic for non-DST zones
This change fixes time.LoadLocationFromTZData and time.Location.lookup logic if the given time is after the last transition and the extend string doesn't have the DST rule. Fixes #58682 Change-Id: Ie34a6d658d14c2b33098b29ab83c041ef0d34266 GitHub-Last-Rev: f6681eb GitHub-Pull-Request: #58684 Reviewed-on: https://go-review.googlesource.com/c/go/+/471020 Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent e671fe0 commit 90dde5d

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

src/time/time_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,16 +1860,26 @@ func TestZoneBounds(t *testing.T) {
18601860
5: {Date(1991, September, 14, 17, 0, 0, 0, loc), boundTwo, boundThree},
18611861
6: {Date(1991, September, 15, 0, 50, 0, 0, loc), boundTwo, boundThree},
18621862

1863+
// The ZoneBounds of a "Asia/Shanghai" after the last transition (Standard Time)
1864+
7: {boundThree, boundThree, Time{}},
1865+
8: {Date(1991, December, 15, 1, 50, 0, 0, loc), boundThree, Time{}},
1866+
9: {Date(1992, April, 13, 17, 50, 0, 0, loc), boundThree, Time{}},
1867+
10: {Date(1992, April, 13, 18, 0, 0, 0, loc), boundThree, Time{}},
1868+
11: {Date(1992, April, 14, 1, 50, 0, 0, loc), boundThree, Time{}},
1869+
12: {Date(1992, September, 14, 16, 50, 0, 0, loc), boundThree, Time{}},
1870+
13: {Date(1992, September, 14, 17, 0, 0, 0, loc), boundThree, Time{}},
1871+
14: {Date(1992, September, 15, 0, 50, 0, 0, loc), boundThree, Time{}},
1872+
18631873
// The ZoneBounds of a local time would return two local Time.
18641874
// Note: We preloaded "America/Los_Angeles" as time.Local for testing
1865-
7: {makeLocalTime(0), makeLocalTime(-5756400), makeLocalTime(9972000)},
1866-
8: {makeLocalTime(1221681866), makeLocalTime(1205056800), makeLocalTime(1225616400)},
1867-
9: {makeLocalTime(2152173599), makeLocalTime(2145916800), makeLocalTime(2152173600)},
1868-
10: {makeLocalTime(2152173600), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1869-
11: {makeLocalTime(2152173601), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1870-
12: {makeLocalTime(2159200800), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1871-
13: {makeLocalTime(2172733199), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1872-
14: {makeLocalTime(2172733200), makeLocalTime(2172733200), makeLocalTime(2177452800)},
1875+
15: {makeLocalTime(0), makeLocalTime(-5756400), makeLocalTime(9972000)},
1876+
16: {makeLocalTime(1221681866), makeLocalTime(1205056800), makeLocalTime(1225616400)},
1877+
17: {makeLocalTime(2152173599), makeLocalTime(2145916800), makeLocalTime(2152173600)},
1878+
18: {makeLocalTime(2152173600), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1879+
19: {makeLocalTime(2152173601), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1880+
20: {makeLocalTime(2159200800), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1881+
21: {makeLocalTime(2172733199), makeLocalTime(2152173600), makeLocalTime(2172733200)},
1882+
22: {makeLocalTime(2172733200), makeLocalTime(2172733200), makeLocalTime(2177452800)},
18731883
}
18741884
for i, tt := range realTests {
18751885
start, end := tt.giveTime.ZoneBounds()

src/time/zoneinfo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (l *Location) lookup(sec int64) (name string, offset int, start, end int64,
203203
// If we're at the end of the known zone transitions,
204204
// try the extend string.
205205
if lo == len(tx)-1 && l.extend != "" {
206-
if ename, eoffset, estart, eend, eisDST, ok := tzset(l.extend, end, sec); ok {
206+
if ename, eoffset, estart, eend, eisDST, ok := tzset(l.extend, start, sec); ok {
207207
return ename, eoffset, estart, eend, eisDST
208208
}
209209
}
@@ -264,12 +264,12 @@ func (l *Location) firstZoneUsed() bool {
264264
}
265265

266266
// tzset takes a timezone string like the one found in the TZ environment
267-
// variable, the end of the last time zone transition expressed as seconds
267+
// variable, the time of the last time zone transition expressed as seconds
268268
// since January 1, 1970 00:00:00 UTC, and a time expressed the same way.
269269
// We call this a tzset string since in C the function tzset reads TZ.
270270
// The return values are as for lookup, plus ok which reports whether the
271271
// parse succeeded.
272-
func tzset(s string, initEnd, sec int64) (name string, offset int, start, end int64, isDST, ok bool) {
272+
func tzset(s string, lastTxSec, sec int64) (name string, offset int, start, end int64, isDST, ok bool) {
273273
var (
274274
stdName, dstName string
275275
stdOffset, dstOffset int
@@ -290,7 +290,7 @@ func tzset(s string, initEnd, sec int64) (name string, offset int, start, end in
290290

291291
if len(s) == 0 || s[0] == ',' {
292292
// No daylight savings time.
293-
return stdName, stdOffset, initEnd, omega, false, true
293+
return stdName, stdOffset, lastTxSec, omega, false, true
294294
}
295295

296296
dstName, s, ok = tzsetName(s)

src/time/zoneinfo_read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
329329
} else if l.extend != "" {
330330
// If we're at the end of the known zone transitions,
331331
// try the extend string.
332-
if name, offset, estart, eend, isDST, ok := tzset(l.extend, l.cacheEnd, sec); ok {
332+
if name, offset, estart, eend, isDST, ok := tzset(l.extend, l.cacheStart, sec); ok {
333333
l.cacheStart = estart
334334
l.cacheEnd = eend
335335
// Find the zone that is returned by tzset to avoid allocation if possible.

src/time/zoneinfo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ func TestTzset(t *testing.T) {
271271
{"PST8PDT,M3.2.0,M11.1.0", 0, 2172733199, "PDT", -7 * 60 * 60, 2152173600, 2172733200, true, true},
272272
{"PST8PDT,M3.2.0,M11.1.0", 0, 2172733200, "PST", -8 * 60 * 60, 2172733200, 2177452800, false, true},
273273
{"PST8PDT,M3.2.0,M11.1.0", 0, 2172733201, "PST", -8 * 60 * 60, 2172733200, 2177452800, false, true},
274+
{"KST-9", 592333200, 1677246697, "KST", 9 * 60 * 60, 592333200, 1<<63 - 1, false, true},
274275
} {
275276
name, off, start, end, isDST, ok := time.Tzset(test.inStr, test.inEnd, test.inSec)
276277
if name != test.name || off != test.off || start != test.start || end != test.end || isDST != test.isDST || ok != test.ok {

0 commit comments

Comments
 (0)