aboutsummaryrefslogtreecommitdiff
path: root/src/time/time.go
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 02:12:23 +0000
commitf6103e9b560e1089a1491cc6438b24074ad05957 (patch)
treef0079d3e141a7e72b2c0143e4761bb85d8599efd /src/time/time.go
parent7da96592c4d1d92984cef3be6c5cc66c288fc79b (diff)
downloadgo-f6103e9b560e1089a1491cc6438b24074ad05957.tar.gz
go-f6103e9b560e1089a1491cc6438b24074ad05957.zip
[release-branch.go1.16] 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 #49406 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/+/361954 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Diffstat (limited to 'src/time/time.go')
-rw-r--r--src/time/time.go14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/time/time.go b/src/time/time.go
index 8ae62308e5..c8f8b9081a 100644
--- a/src/time/time.go
+++ b/src/time/time.go
@@ -1373,17 +1373,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)
}