Working with Unix time
Unix time is the number of seconds (or milliseconds, microseconds, or nanoseconds) passed since January 1, 1970 UTC (the epoch.) Go uses int64
to represent these values, so Unix time as seconds can represent billions of years into the past or the future. Unix time as nanoseconds can represent date values between 1678 and 2262. Unix time is an absolute measure of an instance as the duration since (or until) the epoch. It is independent of the location, so with two Unix times, s
and t
, if s<t
, then s
happened before t
, no matter the location. Because of these properties, Unix time is usually used as a timestamp that marks when an event happened (when a log is written, when a record is inserted, etc.).
How to do it...
- To get the current Unix time, use the following:
time.Now().Unix() int64
: Unix time in secondstime.Now().UnixMilli() int64
: Unix time in millisecondstime.Now().UnixMicro() int64
: Unix time in microsecondstime.Now().UnixNano() int64
: Unix...