aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>2021-11-02 23:17:21 +0800
committerIan Lance Taylor <iant@golang.org>2021-11-29 19:46:17 +0000
commitb7651e504651e1d1a26daf41f9d5ecd1cadc1b81 (patch)
tree379bcc0e436c5d5d7974eed7f0ab5d3bfbcc42d7
parent4eaf0b7a58d024e7d44f58a26914fb1885fd4dd6 (diff)
downloadgo-b7651e504651e1d1a26daf41f9d5ecd1cadc1b81.tar.gz
go-b7651e504651e1d1a26daf41f9d5ecd1cadc1b81.zip
[release-branch.go1.17] time: fix looking for zone offset when date is close to a zone transition
The old implement passed start - 1 or end in func lookup to adjust the offset.But if the time is close to the last zoneTrans, like the issue, testcase and comment, the "start" from lookup will be omega. It can't be adjusted correctly. Fixes #49407 Change-Id: Ibaf82dc4db6d5dd3279796f003d2b19c38a26341 Reviewed-on: https://go-review.googlesource.com/c/go/+/360616 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Robert Findley <rfindley@google.com> (cherry picked from commit 90462dfc3aa99649de90bb587af56a9cb0214665) Reviewed-on: https://go-review.googlesource.com/c/go/+/361955 Trust: Dmitri Shuralyov <dmitshur@golang.org>
-rw-r--r--src/time/time.go14
-rw-r--r--src/time/time_test.go42
2 files changed, 49 insertions, 7 deletions
diff --git a/src/time/time.go b/src/time/time.go
index 4ecc3d82dc..a7d06cdef3 100644
--- a/src/time/time.go
+++ b/src/time/time.go
@@ -1416,17 +1416,17 @@ func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) T
unix := int64(abs) + (absoluteToInternal + internalToUnix)
- // Look for zone offset for t, so we can adjust to UTC.
- // The lookup function expects UTC, so we pass t in the
+ // Look for zone offset for expected time, so we can adjust to UTC.
+ // The lookup function expects UTC, so first we pass unix in the
// hope that it will not be too close to a zone transition,
// and then adjust if it is.
_, offset, start, end, _ := loc.lookup(unix)
if offset != 0 {
- switch utc := unix - int64(offset); {
- case utc < start:
- _, offset, _, _, _ = loc.lookup(start - 1)
- case utc >= end:
- _, offset, _, _, _ = loc.lookup(end)
+ utc := unix - int64(offset)
+ // If utc is valid for the time zone we found, then we have the right offset.
+ // If not, we get the correct offset by looking up utc in the location.
+ if utc < start || utc >= end {
+ _, offset, _, _, _ = loc.lookup(utc)
}
unix -= int64(offset)
}
diff --git a/src/time/time_test.go b/src/time/time_test.go
index cea5f2d3f5..906ceb8def 100644
--- a/src/time/time_test.go
+++ b/src/time/time_test.go
@@ -1569,3 +1569,45 @@ func TestTimeAddSecOverflow(t *testing.T) {
}
}
}
+
+// Issue 49284: time: ParseInLocation incorrectly because of Daylight Saving Time
+func TestTimeWithZoneTransition(t *testing.T) {
+ ForceZipFileForTesting(true)
+ defer ForceZipFileForTesting(false)
+
+ loc, err := LoadLocation("Asia/Shanghai")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ tests := [...]struct {
+ give Time
+ want Time
+ }{
+ // 14 Apr 1991 - Daylight Saving Time Started
+ // When time of "Asia/Shanghai" was about to reach
+ // Sunday, 14 April 1991, 02:00:00 clocks were turned forward 1 hour to
+ // Sunday, 14 April 1991, 03:00:00 local daylight time instead.
+ // The UTC time was 13 April 1991, 18:00:00
+ 0: {Date(1991, April, 13, 17, 50, 0, 0, loc), Date(1991, April, 13, 9, 50, 0, 0, UTC)},
+ 1: {Date(1991, April, 13, 18, 0, 0, 0, loc), Date(1991, April, 13, 10, 0, 0, 0, UTC)},
+ 2: {Date(1991, April, 14, 1, 50, 0, 0, loc), Date(1991, April, 13, 17, 50, 0, 0, UTC)},
+ 3: {Date(1991, April, 14, 3, 0, 0, 0, loc), Date(1991, April, 13, 18, 0, 0, 0, UTC)},
+
+ // 15 Sep 1991 - Daylight Saving Time Ended
+ // When local daylight time of "Asia/Shanghai" was about to reach
+ // Sunday, 15 September 1991, 02:00:00 clocks were turned backward 1 hour to
+ // Sunday, 15 September 1991, 01:00:00 local standard time instead.
+ // The UTC time was 14 September 1991, 17:00:00
+ 4: {Date(1991, September, 14, 16, 50, 0, 0, loc), Date(1991, September, 14, 7, 50, 0, 0, UTC)},
+ 5: {Date(1991, September, 14, 17, 0, 0, 0, loc), Date(1991, September, 14, 8, 0, 0, 0, UTC)},
+ 6: {Date(1991, September, 15, 0, 50, 0, 0, loc), Date(1991, September, 14, 15, 50, 0, 0, UTC)},
+ 7: {Date(1991, September, 15, 2, 00, 0, 0, loc), Date(1991, September, 14, 18, 00, 0, 0, UTC)},
+ }
+
+ for i, tt := range tests {
+ if !tt.give.Equal(tt.want) {
+ t.Errorf("#%d:: %#v is not equal to %#v", i, tt.give.Format(RFC3339), tt.want.Format(RFC3339))
+ }
+ }
+}