time.Time round/truncate not removing nanosceonds component

75 views
Skip to first unread message

Jens Hausherr

unread,
May 14, 2018, 2:55:47 AM5/14/18
to golang-nuts
Hi, here is something I have been puzzled for a few days now.

On my OSX machine rounding/truncating of time.Time values apparently does not remove the nanoseconds component as documented when calling Truncate(0)/Round(0).


On play.golang.org it works as expected: https://play.golang.org/p/NXyhK5v9Rwe (Although it might be misleading as it seems to be stuck in 2009 still).

The same code run on my local machine (OSX, go 1.10.2) outputs:

2018-05-14 08:50:02.291049227 +0200 CEST m=+0.000501271
2018-04-14 06:50:02.291049227 +0000 UTC
2018-04-29 06:50:02.291049227 +0000 UTC
2018-05-13 06:50:02.291049227 +0000 UTC

So the nanoseconds persist through UTC conversion and truncating the ns value.

Regards,
Jens



Jakob Borg

unread,
May 14, 2018, 5:12:07 AM5/14/18
to Jens Hausherr, golang-nuts
Truncate(0) is not supposed to remove the nanoseconds part; it removes the monotonic time part. It just happens that the nanoseconds are zero on the "clock" on play:


To remove the subsecond part use .Truncate(time.Second) (or the corresponding Round of course)

//jb

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jens Hausherr

unread,
May 14, 2018, 6:59:42 AM5/14/18
to golang-nuts
Ah, so I misunderstood the documentation.

Thanks for the answer.

I suspect the behaviour of play.golang.org is due to the fake time as part of the sandboxing

//Jens
Reply all
Reply to author
Forward
0 new messages