Thursday, October 3, 2024
HomeGolangUnix time in Go (Golang)

Unix time in Go (Golang) [cheatsheet]



The time package deal in Go comprises handy features for working on Unix time (often known as Epoch time, Posix time, seconds for the reason that Epoch, or UNIX Epoch time). Utilizing them, you may simply get the present Unix timestamp, convert a time.Time struct to Unix time, and vice versa – Unix timestamp to time.Time. Try this cheatsheet to discover ways to do it and are available again to it everytime you neglect 😉

Convert date/time to Unix epoch time

The time.Time struct comprises Unix(), UnixMilli(), UnixMicro() and UnixNano() strategies for changing a time object to a Unix timestamp of sort int64.

date := time.Date(2022, 6, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(date.Unix())
fmt.Println(date.UnixMilli())
fmt.Println(date.UnixMicro())
fmt.Println(date.UnixNano())

Output:

1654041600
1654041600000
1654041600000000
1654041600000000000

Get present Unix timestamp

To get the present Unix timestamp in Go, you simply must get the present time of sort time.Time utilizing the time.Now() perform and convert it to Unix timestamp utilizing Unix(), UnixMilli(), UnixMicro(), or UnixNano() technique.

Get present Unix/Epoch time in seconds

fmt.Println(time.Now().Unix())

Output:

Get present Unix/Epoch time in milliseconds

fmt.Println(time.Now().UnixMilli())

Output:

Get present Unix/Epoch time in microseconds

fmt.Println(time.Now().UnixMicro())

Output:

Get present Unix/Epoch time in nanoseconds

fmt.Println(time.Now().UnixNano())

Output:

Convert Unix timestamp to time.Time

The time package deal has three features to transform from an int64 Unix timestamp to a time.Time object:

Second Unix timestamp to time.Time

var timestamp int64 = 1657861095

t := time.Unix(timestamp, 0)
fmt.Println(t.UTC())

Output:

2022-07-15 04:58:15 +0000 UTC

Millisecond Unix timestamp to time.Time

var timestamp int64 = 1657861095093

t := time.UnixMilli(timestamp)
fmt.Println(t.UTC())

Output:

2022-07-15 04:58:15.093 +0000 UTC

Microsecond Unix timestamp to time.Time

var timestamp int64 = 1657861095093115

t := time.UnixMicro(timestamp)
fmt.Println(t.UTC())

Output:

2022-07-15 04:58:15.093115 +0000 UTC

Nanosecond Unix timestamp to time.Time

var timestamp int64 = 1657861095093117123

t := time.Unix(0, timestamp)
fmt.Println(t.UTC())

Output:

2022-07-15 04:58:15.093117123 +0000 UTC

Parse Unix timestamp from string to time.Time

To parse a string Unix timestamp and get a time.Time object, you have to first convert the timestamp from string to int64, after which convert it to time.Time utilizing the Unix(), UnixMilli(), or UnixMicro() perform.

timeString := "1657861095"
timestamp, err := strconv.ParseInt(timeString, 10, 64)
if err != nil {
    panic(err)
}
t := time.Unix(timestamp, 0)
fmt.Println(t.UTC())

Output:

2022-07-15 04:58:15 +0000 UTC
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments