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: (*Time).Unix not equal in location Asia/Shanghai #37277

Closed
stubbornTanzhe opened this issue Feb 18, 2020 · 5 comments
Closed

time: (*Time).Unix not equal in location Asia/Shanghai #37277

stubbornTanzhe opened this issue Feb 18, 2020 · 5 comments
Labels
ExpertNeeded FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@stubbornTanzhe
Copy link

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

$ go version
go version go1.13.8 windows/amd64

Does this issue reproduce with the latest release?

always

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

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\小年糕\AppData\Local\go-build
set GOENV=C:\Users\小年糕\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS= -mod=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=git.windimg.com
set GONOSUMDB=git.windimg.com
set GOOS=windows
set GOPATH=C:\gopath
set GOPRIVATE=git.windimg.com
set GOPROXY=direct
set GOROOT=C:\goroot
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\goroot\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\gopath\src\git.windimg.com\giantart\art-usercenter\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Use
rs\小年糕\AppData\Local\Temp\go-build796280254=/tmp/go-build -gno-record-gcc-switches

What did you do?

func Test_time_convert(t *testing.T) {
	tz, err := time.LoadLocation("Asia/Shanghai")
	assert.Nil(t, err)
	t1 := time.Date(1890, 01, 01, 0, 0, 0, 0, tz)
	fmt.Println(t1.Unix())

	t2 := time.Unix(t1.Unix(), 0)
	fmt.Println(t2)
}

What did you expect to see?

t2 print output is 1890-01-01 00:00:00 +0800 CST

What did you see instead?

1889-12-31 23:54:17 +0800 CST

btw:
I got this:
https://history.stackexchange.com/questions/8287/why-did-the-clocks-go-back-in-shanghai-on-december-31-1927
and it's means there was a timezone change since 1927.
BUT i try the code from 1890 to 1905,then found out in 1905 case ,t1 equals to t2.
any help will be appreciated

@odeke-em odeke-em changed the title time to unix not equal in location Shanghai time: (*Time).Unix not equal in location Asia/Shanghai Feb 18, 2020
@odeke-em odeke-em added ExpertNeeded NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Feb 18, 2020
@odeke-em
Copy link
Member

Thank you for the report @stubbornTanzhe.

Kindly cc-ing @robpike.

@ulikunitz
Copy link
Contributor

ulikunitz commented Feb 18, 2020

I don't think this is a bug.

If you print out t1 directly, you should get "1890-01-01 00:00:00 +0805 LMT".

The IANA time zone database (https://www.iana.org/time-zones) uses local mean time (LMT) until 1901 for Shanghai and assumes a time difference of +08:05:43 to UTC, in 1890 this was Greenwich Mean Time (GMT).

Your program is using time.Unix() to convert the Unix time to the local time zone and it appears that it is set to China Standard Time (+08:00) and this results in the difference of 5 minutes and 43 seconds.

I will work on a demo program to make that more transparent.

@ulikunitz
Copy link
Contributor

Here is the sample code demonstrating what happens: https://play.golang.org/p/D9e4KwoPdjA

@stubbornTanzhe
Copy link
Author

@ulikunitz thank you very much, I got what you say. Just the region in history may had changed time zone, so that the tick in history may had a different display

@odeke-em
Copy link
Member

Thank you @ulikunitz for owning this issue and for the answer, and thank you @stubbornTanzhe for the question. I shall close this issue then.

@golang golang locked and limited conversation to collaborators Feb 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ExpertNeeded 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

4 participants