Skip to content

time: time.Before return an unexpected result #42056

@ironzhang

Description

@ironzhang

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions