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 ParseInLocation error #66987

Closed
aix-tech opened this issue Apr 23, 2024 · 3 comments
Closed

time: time ParseInLocation error #66987

aix-tech opened this issue Apr 23, 2024 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@aix-tech
Copy link

Go version

go version go1.22.2 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/abc/Library/Caches/go-build'
GOENV='/Users/abc/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/abc/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/abc/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.2/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.2/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.2'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/h6/h_h0rdm91vgfb68pntxflth80000gp/T/go-build3222925675=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

s := "15/04/90 02:00"
fmt.Println(time.ParseInLocation("02/01/06 15:04", s, time.Local))
s = "15/04/90 03:00"
fmt.Println(time.ParseInLocation("02/01/06 15:04", s, time.Local))

What did you see happen?

➜ demo go run main.go
1990-04-15 03:00:00 +0900 CDT
1990-04-15 03:00:00 +0900 CDT


Both 15/04/90 02:00 and 15/04/90 03:00 are parsed as 1990-04-15 03:00:00

What did you expect to see?

➜ demo go run main.go
1990-04-15 02:00:00 +0900 CDT
1990-04-15 03:00:00 +0900 CDT

@ALTree
Copy link
Member

ALTree commented Apr 23, 2024

Can you post a full, runnable program? Also what's time.Local on this machine? Or the value of the TZ env variable? This doesn't reproduce the issue:

package main

import (
	"fmt"
	"time"
)

func main() {
	loc, err := time.LoadLocation("America/Chicago")
	if err != nil {
		panic(err)
	}
	s := "15/04/90 02:00"
	fmt.Println(time.ParseInLocation("02/01/06 15:04", s, loc))
	s = "15/04/90 03:00"
	fmt.Println(time.ParseInLocation("02/01/06 15:04", s, loc))
}

1990-04-15 02:00:00 -0500 CDT
1990-04-15 03:00:00 -0500 CDT

@ALTree ALTree added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Apr 23, 2024
@aix-tech
Copy link
Author

loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
    fmt.Println("Error loading location:", err)
    return
}

s := "15/04/90 02:00"
fmt.Println(time.ParseInLocation("02/01/06 15:04", s, loc))
s = "15/04/90 03:00"
fmt.Println(time.ParseInLocation("02/01/06 15:04", s, loc))

// output
1990-04-15 03:00:00 +0900 CDT
1990-04-15 03:00:00 +0900 CDT

@ALTree
Copy link
Member

ALTree commented Apr 23, 2024

Oh, it's CDT as in China Daylight Time. There was a time change there on April 15 1990:

$ zdump -c 1989,1991 -v /usr/share/zoneinfo/Asia/Shanghai
[...]
/usr/share/zoneinfo/Asia/Shanghai  Sat Apr 14 17:59:59 1990 UT = Sun Apr 15 01:59:59 1990 CST isdst=0 gmtoff=28800
/usr/share/zoneinfo/Asia/Shanghai  Sat Apr 14 18:00:00 1990 UT = Sun Apr 15 03:00:00 1990 CDT isdst=1 gmtoff=32400
[...]

So 02:00 does not exists. From the documentation:

A daylight savings time transition skips or repeats times. For example, in the United States, March 13, 2011 2:15am never occurred, while November 6, 2011 1:15am occurred twice. In such cases, the choice of time zone, and therefore the time, is not well-defined. Date returns a time that is correct in one of the two zones involved in the transition, but it does not guarantee which.

Closing here as this is working as intended.

@ALTree ALTree closed this as completed Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

2 participants