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

Comments

Projects
None yet
5 participants
@huydx
Copy link

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

This comment has been minimized.

Copy link

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 this to the Go1.12 milestone Aug 28, 2018

@agnivade

This comment has been minimized.

Copy link
Member

commented Aug 28, 2018

@VadimKorniychuk

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Aug 28, 2018

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.

@randall77 randall77 closed this Aug 28, 2018

@mark-rushakoff

This comment has been minimized.

Copy link
Contributor

commented Aug 28, 2018

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.