Skip to content

Incorrect behaviour of time.Date function #47549

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

Closed
sharmaayush358 opened this issue Aug 5, 2021 · 4 comments
Closed

Incorrect behaviour of time.Date function #47549

sharmaayush358 opened this issue Aug 5, 2021 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@sharmaayush358
Copy link

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

$ go version
go1.15.7 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="off"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/go-code/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/go-code"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/.cache/bazel/_bazel_user/b97476d719d716accead0f2d5b93104f/external/go_sdk"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/.cache/bazel/_bazel_user/b97476d719d716accead0f2d5b93104f/external/go_sdk/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build854370504=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/hyyLG_o8ERS

What did you expect to see?

The two time instances should be equal.

What did you see instead?

The two time instances are not equal.

This is happening because (t *Time) setLoc has this if statement

if loc == &utcLoc {
    loc = nil
}

Instead of checking for the equality of pointers, we should check for the equality of the Location structs that they point to.
So, I propose changing it to

if *loc == utcLoc {
    loc = nil
}
@mvdan
Copy link
Member

mvdan commented Aug 5, 2021

Have you seen this piece of the docs?

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.

Your program works fine with Time.Equal.

@mvdan mvdan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Aug 5, 2021
@sharmaayush358
Copy link
Author

While that is fine, I still believe the way setLoc is implemented should be changed. The location provided to time.Date in the playground example was infact utcLoc but the setLoc function deemed it not to be utcLoc because of the way it is implemented.

@mvdan
Copy link
Member

mvdan commented Aug 5, 2021

A location is a pointer, though. If you make a copy of the UTC location, it is not guaranteed to be treated the same. I'm not really sure why you're doing the copy.

If you must do the copy for some reason, and you want equality to be more lax, then you want the Equal method.

@seankhliao
Copy link
Member

See previously #45961 for considering time.Time

@golang golang locked and limited conversation to collaborators Aug 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants