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: Altered time.Time state from calling IsDST or Zone #50559

Closed
myshkin5 opened this issue Jan 12, 2022 · 3 comments
Closed

time: Altered time.Time state from calling IsDST or Zone #50559

myshkin5 opened this issue Jan 12, 2022 · 3 comments

Comments

@myshkin5
Copy link

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

$ go version
go version go1.17.6 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/dschultz/.cache/go-build"
GOENV="/home/dschultz/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/dschultz/workspace/apiserver-go/pkg/mod"
GONOPROXY="github.com/aspenmesh/*"
GONOSUMDB="github.com/aspenmesh/*"
GOOS="linux"
GOPATH="/home/dschultz/workspace/apiserver-go"
GOPRIVATE="github.com/aspenmesh/*"
GOPROXY="https://proxy.golang.org"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/dschultz/workspace/apiserver/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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2743734130=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Observing bizarre state mutations around the location in an instance of time.Time. By calling IsDST (which should be read-only, right?), the state of an instance is changed. Subsequent instances instantiated after the call to IsDST have an updated location and altered state.

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

With output as follows:

before 11810389586750117723, after 12359862718078847701, new 12359862718078847701, pre 11810389586750117723

Showing that the hash of the instance changes after calling IsDST (Zone produces the same results). The hash of a new instance created after the call to IsDST on the first instance equals the new hash.

What did you expect to see?

A consistent hash and unaltered state when calling a seemingly read-only function.

What did you see instead?

A mysteriously changing hash value.

@ericlagergren
Copy link
Contributor

What is the bug here? The time package explicitly says you should not do this:

// Note that the Go == operator compares not just the time instant but also the
// Location and the monotonic clock reading. Therefore, Time values should not
// be used as map or database keys without first guaranteeing that the
// identical Location has been set for all values, which can be achieved
// through use of the UTC or Local method, and that the monotonic clock reading
// has been stripped by setting t = t.Round(0). In general, prefer t.Equal(u)
// to t == u, since t.Equal uses the most accurate comparison available and
// correctly handles the case when only one of its arguments has a monotonic
// clock reading.

@seankhliao
Copy link
Member

The location (time.Local) is only initialized on first use. As this isn't exposed, there's no guarantee on internal values.

Closing as working as intended.

@myshkin5
Copy link
Author

Thanks for the explanation and the documentation is quite clear. Just rather odd behavior. Calling UTC and/or Local had no effect. Adding this init function solved the problem:

func init() {
	_ = time.Unix(0, 0).IsDST()
}

@golang golang locked and limited conversation to collaborators Jan 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants