Converting dates to epoch and vice versa
The epoch is the universal system to describe the point in time. The beginning of epoch time is defined as 00:00:00 1 Jan 1970 UTC
. The value of epoch is the amount of seconds since the timestamp, minus the amount of leap seconds since then.
The time
package and Time
type provide you with the ability to operate and find out the UNIX epoch time.
How to do it...
- Open the console and create the folder
chapter04/recipe04
. - Navigate to the directory.
- Create the
epoch.go
file with the following content:
package main import ( "fmt" "time" ) func main() { // Set the epoch from int64 t := time.Unix(0, 0) fmt.Println(t) // Get the epoch // from Time instance epoch := t.Unix() fmt.Println(epoch) // Current epoch time apochNow := time.Now().Unix() fmt.Printf("Epoch time in seconds: %d\n", apochNow) apochNano...