time.Time precision : improve documentation

680 views
Skip to first unread message

David Terranova

unread,
Aug 27, 2020, 1:02:12 PM8/27/20
to golang-dev
`time.Time` has different precision depending on the operating system it is being used (on darwin since High Sierra, `time.Now()` has a micro second precision and has a nanosecond precision on linux).
While it is not a problem in most cases, we faced some weird behaviour while testing a piece of code on these two different systems, some passed, some others didn't.

I propose to clearly indicate in the documentation that the time precision can be different from an operating system to another.

It is the first time for me to post something here, do not hesitate to re-orient me If I am not posting the right way.

Kind regards,
David

peterGo

unread,
Aug 27, 2020, 4:30:39 PM8/27/20
to golang-dev
It is also a problem on Windows, which uses 100-nanosecond precision.

package main

import (
    "fmt"
    "runtime"
    "time"
)

func main() {
    fmt.Println(runtime.GOOS, runtime.GOARCH)
    for i := time.Duration(0); i < 7; i++ {
        start := time.Now()
        time.Sleep(i * time.Millisecond)
        fmt.Println(time.Since(start))
    }
}

windows amd64
0s
1.0307ms
2.1366ms
3.5485ms
4.1181ms
5.0403ms
6.1694ms

linux amd64
160ns
1.061245ms
2.095457ms
3.100238ms
4.25249ms
5.222099ms
6.197004ms


The Go Project
https://golang.org/project/

Reporting issues

If you spot bugs, mistakes, or inconsistencies in the Go project's code or documentation, please let us know by filing a ticket on our issue tracker. (Of course, you should check it's not an existing issue before creating a new one.)

https://github.com/golang/go/issues


Peter

Anthony Martin

unread,
Aug 27, 2020, 5:56:28 PM8/27/20
to David Terranova, golang-dev
David Terranova <da...@smallpdf.com> once said:
> time.Now ... has a nanosecond precision on linux

It has nanosecond resolution. It may have nanosecond
precision on some machine but I doubt that's common.

Anthony

Dave Cheney

unread,
Aug 27, 2020, 6:07:02 PM8/27/20
to Anthony Martin, David Terranova, golang-dev
As far as I’m aware linux returns a tv with nanosecond resolution but the values beyond the microsecond are often blank, random, or guesstimated.

David: you mentioned that this is causing you a cross platform issue. Is this tracked in the golang/go issue tracker?

> On 28 Aug 2020, at 07:56, Anthony Martin <al...@pbrane.org> wrote:
> --
> You received this message because you are subscribed to the Google Groups "golang-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/20200827215606.GA1053%40alice.

David Terranova

unread,
Aug 28, 2020, 4:48:37 AM8/28/20
to Dave Cheney, Anthony Martin, golang-dev
From our point of view, Linux machines return all the time a time.Time with nanosecond precision. 
Dave Cheney: I just opened one https://github.com/golang/go/issues/41087 

IMHO the most important is about documenting this difference.

Dave Cheney

unread,
Aug 28, 2020, 5:31:44 AM8/28/20
to David Terranova, Anthony Martin, golang-dev


On 28 Aug 2020, at 18:48, David Terranova <da...@smallpdf.com> wrote:


From our point of view, Linux machines return all the time a time.Time with nanosecond precision. 

The trick is they might return a value with 9 decimal places, but at least three of those are random.

David Terranova

unread,
Aug 28, 2020, 6:38:10 AM8/28/20
to golang-dev
If they are not reliable I don't see the point having a such precision.
If I remember correctly it is the reason why nanoseconds precision has been abandoned since MacOS high sierra.

Robert Engels

unread,
Aug 28, 2020, 7:49:02 AM8/28/20
to David Terranova, golang-dev
It depends on the clock source. You can add an external clock to the machine and configure that as the source. You can get pico precision. 

This is frequently done in HFT systems. 

There is still the problem that if it takes nanos to read it, what good are picos...

On Aug 28, 2020, at 5:38 AM, David Terranova <da...@smallpdf.com> wrote:

If they are not reliable I don't see the point having a such precision.

David Terranova

unread,
Aug 28, 2020, 7:57:19 AM8/28/20
to Robert Engels, golang-dev
Fine, however, IMHO the documentation should mention it

David Terranova

unread,
Sep 3, 2020, 3:44:41 AM9/3/20
to golang-dev
Should I make a PR to improve the documentation of the `time.Now` ?

Ian Lance Taylor

unread,
Sep 4, 2020, 11:04:36 PM9/4/20
to David Terranova, golang-dev
On Thu, Sep 3, 2020 at 12:44 AM David Terranova <da...@smallpdf.com> wrote:
>
> Should I make a PR to improve the documentation of the `time.Now` ?

What do you suggest should be added? An additional sentence saying
"The resolution of the result is system-dependent."? I don't think we
should add more than that. And I'm not sure that helps much, but I
don't feel strongly about it.

Ian
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/6f5743e0-ce35-423f-a540-59d5fe7820d2n%40googlegroups.com.

David Terranova

unread,
Sep 7, 2020, 1:37:24 AM9/7/20
to golang-dev
Exactly, such a sentence makes things clear about what to expect
Reply all
Reply to author
Forward
0 new messages