golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.98k stars 17.67k forks source link

time: time.LoadLocation reports unknown time zone for European Summer Time #69549

Closed dtext closed 1 month ago

dtext commented 1 month ago

Go version

go version go1.23.0 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/dtextores/Library/Caches/go-build'
GOENV='/Users/dtextores/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/dtextores/go/pkg/mod'
GONOPROXY='redacted'
GONOSUMDB='redacted'
GOOS='darwin'
GOPATH='/Users/dtextores/go'
GOPRIVATE='redacted'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/opt/go/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/opt/go/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/dtextores/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/dtextores/code/redacted/go.mod'
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/0g/tnsylx9n7qn2m0j4gmp452gh0000gn/T/go-build1406944028=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

package main

import (
    "fmt"
    "time"
)

func main() {
    tz, offset := time.Now().Zone()
    fmt.Printf("tz: %s, offset: %d", tz, offset)
    loc, err := time.LoadLocation(tz)
    fmt.Printf("loc: %s, err: %s", loc, err)
}

I'm in the Europe/Berlin Timezone, where the daylight savings time is called "CEST" as reported by time.Now().Zone(). I also tested with Europe/Bucharest ("EEST").

https://go.dev/play/p/_RHfB4rcJzi

What did you see happen?

Running the code right now during daylight savings time yields: tz: CEST, offset: 7200loc: UTC, err: unknown time zone CEST. (Please excuse the formatting here, I threw this together quickly.)

Running it with the clock set to March 1, and timezone updated to CET accordingly yields: tz: CET, offset: 3600loc: CET, err: %!s(<nil>)

What did you expect to see?

I should always be able to load a location from a timezone string obtained by time.Now().Zone() during Daylight Savings Time the same as during Winter Time. In other words, I would expect the first result to be something like: tz: CEST, offset: 7200loc: CEST, err: %!s(<nil>)

gabyhelp commented 1 month ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

seankhliao commented 1 month ago

Duplicate of #34913

dtext commented 1 month ago

@seankhliao is it though? I saw that issue and I also see that CEST is not defined within my (or my colleagues) system. But I'm not specifying some unknown timezone myself, I'm just passing on the value returned by time.Now().Zone(). Why does that return CEST if my system doesn't know that timezone?

ianlancetaylor commented 1 month ago

The time package is just reflecting that time zone information stored on your system.

The Zone method is documented to return the abbreviated timezone name. The abbreviated names are not unique. In general there is no way to load a timezone using just the abbreviated name.

What we may not have is a way to return the full name of the local timezone. That is actually not recorded in the tzdata on a typical Unix system. You can often find it on a Unix system by calling os.Readlink("/etc/localtime").