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.Before return an unexpected result #42056

Closed
ironzhang opened this issue Oct 19, 2020 · 1 comment
Closed

time: time.Before return an unexpected result #42056

ironzhang opened this issue Oct 19, 2020 · 1 comment

Comments

@ironzhang
Copy link

ironzhang commented Oct 19, 2020

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

$ go version
go version go1.14.3 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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/zhanghui/Library/Caches/go-build"
GOENV="/Users/zhanghui/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/zhanghui/gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/zhanghui/gopath"
GOPRIVATE=""
GOPROXY="http://goproxy.intra.xiaojukeji.com"
GOROOT="/usr/local/go"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
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/03/x_5wmgc108z6rsmh0b4xc2800000gp/T/go-build618623655=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

This is my test case

import (
        "fmt"
        "syscall"
        "testing"
        "time"
)

func SetSysTime(t time.Time) error {
        tv := syscall.NsecToTimeval(t.UnixNano())
        return syscall.Settimeofday(&tv)
}

func TestTimeNow(t *testing.T) {
        n1 := time.Now()
        defer func() {
                SetSysTime(n1)
        }()

        t1 := n1.Add(2 * time.Hour)
        t2 := n1.Add(3 * time.Hour)
        if err := SetSysTime(t2); err != nil {
                t.Fatalf("set sys time: %v", err)
        }
        n2 := time.Now()

        fmt.Printf("n1=[%v], n2=[%v]\n", n1, n2)
        fmt.Printf("t1=[%v], t2=[%v]\n", t1, n2)

        if t2.Before(t1) {
                t.Errorf("t2 before t1, t2=[%v][%d], t1=[%v][%d]", t2, t2.UnixNano(), t1, t1.UnixNano())
        }
        if n2.Before(t1) {
                t.Errorf("n2 before t1, n2=[%v][%d], t1=[%v][%d]", n2, n2.UnixNano(), t1, t1.UnixNano())
        }
}

What did you expect to see?

I expected to see the test can pass.

What did you see instead?

when I run sudo go test,it failed, the output is

n1=[2020-10-19 11:19:20.762809 +0800 CST m=+0.000549490], n2=[2020-10-19 14:19:20.766093 +0800 CST m=+0.003840186]
t1=[2020-10-19 13:19:20.762809 +0800 CST m=+7200.000549490], t2=[2020-10-19 14:19:20.766093 +0800 CST m=+0.003840186]
--- FAIL: TestTimeNow (0.01s)
    time_test.go:35: n2 before t1, n2=[2020-10-19 14:19:20.766093 +0800 CST m=+0.003840186][1603088360766093000], t1=[2020-10-19 13:19:20.762809 +0800 CST m=+7200.000549490][1603084760762809000]
FAIL
exit status 1
@ironzhang ironzhang changed the title A bug of time package time: time.Before return an unexpected result Oct 19, 2020
@darren
Copy link

darren commented Oct 19, 2020

https://golang.org/pkg/time/

Calling time.Now() before and after settimeofday always has hasMonotonic set
And time comparisons use the monotonic clock reading, so n2.Before(t1) uses the monotonic clock.

I think what you expect is just use wall clock n2.UnixNano() < t1.UnixNano() for comparison.

more info: https://go.googlesource.com/proposal/+/master/design/12914-monotonic.md

@golang golang locked and limited conversation to collaborators Oct 19, 2021
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

3 participants