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(0, 0).IsZero() == false #33597

Closed
h4xnoodle opened this issue Aug 12, 2019 · 6 comments
Closed

time: time.Unix(0, 0).IsZero() == false #33597

h4xnoodle opened this issue Aug 12, 2019 · 6 comments

Comments

@h4xnoodle
Copy link

@h4xnoodle h4xnoodle commented Aug 12, 2019

I'm aware that IsZero is implemented with year 1, Jan 1 being 'zero'. (https://golang.org/pkg/time/#Time.IsZero) However, it causes confusion when most developers use epoch time at 0, and the IsZero function is then false for time.Unix(0,0).

Why is zero time considered year 1, Jan 1 instead of epoch 0?

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

$ go version
go version go1.12.7 darwin/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
GOARCH="amd64"
GOBIN="/Users/bex/workspace/bin"
GOCACHE="/Users/bex/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bex/workspace/bosh-cli-workspace"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/z7/dyfnyjn14rngs_4g0bfwc5440000gp/T/go-build443496455=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

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

What did you expect to see?

IsZero evaluates to true for the beginning of epoch time. I'm unsure what the compelling reason for deviating from typical standards is.

What did you see instead?

Using epoch time within my applications and golang requires special consideration and it does not work as expected. I feel the deviation is unnecessarily complicated, though now would be a breaking change so we cannot just change it now.

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Aug 12, 2019

This is working as intended and documented:

https://golang.org/pkg/time/#Time
https://golang.org/pkg/time/#Time.IsZero

Even if we wanted to change it, we couldn't at this point for go1compat reasons.

Go's time.Time zero value is not the Unix epoch zero value. The Unix func converts from Unix to Go.

@bradfitz bradfitz closed this Aug 12, 2019
@runner-mei
Copy link

@runner-mei runner-mei commented Sep 2, 2019

@bradfitz Can fix it on go2

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Sep 2, 2019

@runner-mei We could change it in a v2 of the time package, but we aren't going to do that. See the discussion at https://golang.org/src/time/time.go?#L346.

@Dongss
Copy link

@Dongss Dongss commented Oct 31, 2019

var TimeFromUnixZero = time.Unix(0, 0)
t := time.Now()
isZero := t.Equal(TimeFromUnixZero)

I use this instead

@runner-mei
Copy link

@runner-mei runner-mei commented Oct 31, 2019

add IsUnixZero()?

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 1, 2019

I don't think this comes up often enough to need a special function or method.

@golang golang locked and limited conversation to collaborators Oct 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.