time包提供了时间的显示和测量用的函数。日历的计算采用的是公历。
当前时间戳
获取当前时间、时间戳函数和方法:
Now返回当前本地时间。
func (t Time) Unix() int64
Unix将t表示为Unix时间,即从时间点January 1, 1970 UTC到时间点t所经过的时间(单位秒)。
func (t Time) UnixNano() int64
UnixNano将t表示为Unix时间,即从时间点January 1, 1970 UTC到时间点t所经过的时间(单位纳秒)。如果纳秒为单位的unix时间超出了int64能表示的范围,结果是未定义的。注意这就意味着Time零值调用UnixNano方法的话,结果是未定义的。
代码实现:
package main
import (
"fmt"
"time"
)
func main() {
current := time.Now()
fmt.Println("当前时间:", current)
now := current.Unix()
fmt.Println("当前时间戳:", now)
nanosecond := time.Now().UnixNano()
fmt.Println("当前纳秒时间戳:", nanosecond)
}