Reputation: 1170
I read here https://pkg.go.dev/time#Unix that Unix() returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC
And you can also get the milliseconds since, or nanoseconds since 1970 using UnixNano() and UnixMilli() respectively.
I guess I'm confused as to how this is safe. Won't there be a date/time cut off where the number of nano, milli or regular seconds exceeds int64's capacity?
Also, I was experimenting on go playground https://go.dev/play/ by printing out time.Now.Unix()
, I would always get 1257894000 seconds, even if I ran my code like a minute apart.
However I did see a difference if I did
time.Now.Unix()
time.Sleep(time.Duration(1) * time.Second)
time.Now.Unix()
Which gave me 1257894000 and 1257894001.
I'm confused how Unix time works basically.
Upvotes: 0
Views: 3080
Reputation: 3374
Time.Unix()
, Time.UnixMicro()
, Time.UnixMilli()
, Time.UnixNano()
all return signed int64s.
The max signed int64 is 9,223,372,036,854,775,807
This means Time.Unix()
won't run out of integers for 292 billion years. It will undoubtedly outlive humanity.
The worst case scenario is Time.UnixNano()
which will run out of integers on Friday, April 11, 2262 11:47:16.854 PM GMT
. I don't know about you, but I'll be dead and hopefully no one will be using my code.
Upvotes: 8