Code with Go
Ask or search…
K
Links

Epoch

This Go code demonstrates working with the time package to obtain Unix timestamps at different precisions. Let's go through the code with inline comments and explanations:
// Importing necessary packages.
import (
"fmt"
"time"
)
// The main function, where the execution of the program begins.
func main() {
// Getting the current time.
now := time.Now()
fmt.Println(now)
// Converting the current time to Unix timestamp at different precisions.
// Unix timestamp in seconds.
fmt.Println(now.Unix())
// Unix timestamp in milliseconds.
fmt.Println(now.UnixMilli())
// Unix timestamp in nanoseconds.
fmt.Println(now.UnixNano())
// Converting Unix timestamp back to time.Time.
// Creating a time.Time from Unix timestamp in seconds.
fmt.Println(time.Unix(now.Unix(), 0))
// Creating a time.Time from Unix timestamp in nanoseconds.
fmt.Println(time.Unix(0, now.UnixNano()))
}

Output

2024-01-24 13:21:47.6288203 +0530 IST m=+0.001548401
1706082707
1706082707628
1706082707628820300
2024-01-24 13:21:47 +0530 IST
2024-01-24 13:21:47.6288203 +0530 IST
Explanation:
  1. 1.
    Getting Current Time:
    • time.Now() returns the current time.
  2. 2.
    Unix Timestamps:
    • Unix() returns the Unix timestamp in seconds.
    • UnixMilli() returns the Unix timestamp in milliseconds.
    • UnixNano() returns the Unix timestamp in nanoseconds.
  3. 3.
    Converting Unix Timestamps Back to time.Time:
    • time.Unix() is used to create a time.Time instance from a Unix timestamp.
    • The first argument is the number of seconds, and the second argument is the number of nanoseconds.
This code showcases how to obtain and convert Unix timestamps using the time package in Go. It's useful when you need to work with timestamps in different precisions and convert them back to time.Time.