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: UnixNano() is rounded to microsecond #27301

Closed
huydx opened this issue Aug 28, 2018 · 5 comments
Closed

time: UnixNano() is rounded to microsecond #27301

huydx opened this issue Aug 28, 2018 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin
Milestone

Comments

@huydx
Copy link

huydx commented Aug 28, 2018

Please answer these questions before submitting your issue. Thanks!

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

1.11

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/huydx/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/huydx/.gvm/pkgsets/go1.11/global"
GOPROXY=""
GORACE=""
GOROOT="/Users/huydx/.gvm/gos/go1.11"
GOTMPDIR=""
GOTOOLDIR="/Users/huydx/.gvm/gos/go1.11/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/fx/gbbzrp_s1dz7b3dtxy6bzd240000gp/T/go-build430174837=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now().UnixNano())
}

What did you expect to see?

NOT microsecond rounded every time

What did you see instead?

microsecond rounded every time

@VadimKorniychuk
Copy link

VadimKorniychuk commented Aug 28, 2018

Noticed same behaviour today, looks like introduced here https://go-review.googlesource.com/c/go/+/110655

@agnivade agnivade changed the title go1.11 seems round UnixNano() to microsecond everytime time: UnixNano() is rounded to microsecond Aug 28, 2018
@agnivade agnivade added OS-Darwin NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 28, 2018
@agnivade agnivade added this to the Go1.12 milestone Aug 28, 2018
@agnivade
Copy link
Contributor

/cc @randall77 @ianlancetaylor

@VadimKorniychuk
Copy link

VadimKorniychuk commented Aug 28, 2018

https://go-review.googlesource.com/c/go/+/110655 introduced getting time by calling gettimeofday
but it does not return nanoseconds at all according to man

     struct timeval {
             time_t       tv_sec;   /* seconds since Jan. 1, 1970 */
             suseconds_t  tv_usec;  /* and microseconds */
     };

@randall77
Copy link
Contributor

Well, we use gettimeofday for the wall clock and mach_absolute_time for the monotonic clock.
Yes, gettimeofday returns only microsecond resolution. I couldn't find anything on Darwin that gives higher resolution.
However, mach_absolute_time has nanosecond resolution. Thus, the monotonic time has nanosecond resolution. If you do:

package main

import (
	"fmt"
	"time"
)

func main() {
	a := time.Now()
	b := time.Now()
	fmt.Println(b.Sub(a))
}

You'll get nanosecond resolution.

So I'm going to close this as unfortunate. Nanosecond resolution is available for timing things within a process. You'll only get microsecond resolution when timing things across processes or machines. That seems not so bad.

@mark-rushakoff
Copy link
Contributor

A slightly more complete example of getting, roughly, "now" in nanoseconds:

package main

import (
	"fmt"
	"time"
)

func main() {
	start := time.Now()
	for i := 0; i < 4; i++ {
		diff := time.Since(start)
		fmt.Println(start.Add(diff).UnixNano()) // 🤢
		time.Sleep(time.Millisecond)
	}
}

// Output like:
// 1535499310436503279
// 1535499310437885044
// 1535499310439308031
// 1535499310440693511

@golang golang locked and limited conversation to collaborators Aug 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin
Projects
None yet
Development

No branches or pull requests

6 participants