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

time: ParseInLocation error #49284

Closed
FreeDemon opened this issue Nov 2, 2021 · 7 comments
Closed

time: ParseInLocation error #49284

FreeDemon opened this issue Nov 2, 2021 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@FreeDemon
Copy link

FreeDemon commented Nov 2, 2021

What version of Go are you using (go version)?

$ go version
go version go1.17.2 darwin/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/freedemon/Library/Caches/go-build"
GOENV="/Users/freedemon/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/freedemon/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/freedemon/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/usr/local/Cellar/go/1.17.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.17.2/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/freedemon/Documents/code/goProject/go2/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9y/nkxhf5yd3c7fvqdsdvdwm18r0000gn/T/go-build2346662675=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Why are these times incorrect and missed some time ?

package main

import (
	"fmt"
	"time"
)

func main() {
	timeLayout := "2006-01-02 15:04:05"
	loc, _ := time.LoadLocation("Asia/Shanghai")

	time1, _ := time.ParseInLocation(timeLayout,"1991-09-14 16:50:50", loc)
	fmt.Println(time1)

	time2, _ := time.ParseInLocation(timeLayout,"1991-09-14 17:00:00", loc)
	fmt.Println(time2)

	time3, _ := time.ParseInLocation(timeLayout,"1991-09-15 00:50:00", loc)
	fmt.Println(time3)

	time4, _ := time.ParseInLocation(timeLayout,"1991-09-15 01:00:00", loc)
	fmt.Println(time4)
}

What did you expect to see?

1991-09-14 16:50:50 +0900 CDT
1991-09-14 17:00:00 +0900 CDT
1991-09-15 00:50:00 +0900 CDT
1991-09-15 01:00:00 +0800 CST

What did you see instead?

1991-09-14 16:50:50 +0900 CDT
1991-09-14 18:00:00 +0900 CDT
1991-09-15 01:50:00 +0900 CDT
1991-09-15 01:00:00 +0800 CST

@ALTree ALTree changed the title time ParseInLocation error time: ParseInLocation error Nov 2, 2021
@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 2, 2021
@ALTree
Copy link
Member

ALTree commented Nov 2, 2021

This does seem a bug.

In absence of a timezone indicator, ParseInLocation should interpret the timestamp as if it was in the given location. Asia/Shanghai did end Daylight Saving Time at 02:00, Sep 15 1991, but 1991-09-14 17:00:00 is several hours before that. Why is it corrected to 18:00 ?

cc @ianlancetaylor

@gopherbot
Copy link

Change https://golang.org/cl/360616 mentions this issue: time: fix looking for zone offset when date is close to a zone transition

@rchunping
Copy link

seems a bug
use this instead:

loc:= time.FixedZone("Asia/Shanghai",3600*8)

@FreeDemon
Copy link
Author

seems a bug use this instead:

loc:= time.FixedZone("Asia/Shanghai",3600*8)

thank u, i'll try it

@zhouguangyuan0718
Copy link
Contributor

zhouguangyuan0718 commented Nov 5, 2021

seems a bug use this instead:
loc:= time.FixedZone("Asia/Shanghai",3600*8)

thank u, i'll try it

The actual time between 1991-04-14 02:00:00 to 1991-09-15 01:00:00 in Asia/Shanghai is Daylight Saving Time. We can't use fixed offset for it.

This bugs happens because it get an incorrect offset in the function time.Data:

_, offset, _, _, _ = loc.lookup(start - 1)

At that time. the start is invalid, we can't get the correctly offset by lookup(start-1).

@FreeDemon You can try the CL above. I think It can solve this problem.
#49284 (comment)

@zhouguangyuan0718
Copy link
Contributor

@gopherbot please open backport to 1.16 and 1.17

@gopherbot
Copy link

Backport issue(s) opened: #49406 (for 1.16), #49407 (for 1.17).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@golang golang locked and limited conversation to collaborators Nov 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants