forked from mmcgrana/gobyexample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepoch.go
29 lines (23 loc) · 732 Bytes
/
epoch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// A common requirement in programs is getting the number
// of seconds, milliseconds, or nanoseconds since the
// [Unix epoch](http://en.wikipedia.org/wiki/Unix_time).
// Here's how to do it in Go.
package main
import (
"fmt"
"time"
)
func main() {
// Use `time.Now` with `Unix`, `UnixMilli` or `UnixNano`
// to get elapsed time since the Unix epoch in seconds,
// milliseconds or nanoseconds, respectively.
now := time.Now()
fmt.Println(now)
fmt.Println(now.Unix())
fmt.Println(now.UnixMilli())
fmt.Println(now.UnixNano())
// You can also convert integer seconds or nanoseconds
// since the epoch into the corresponding `time`.
fmt.Println(time.Unix(now.Unix(), 0))
fmt.Println(time.Unix(0, now.UnixNano()))
}