time
time 包提供了时间的显示和测量用的函数。日历的计算采用的是公历。
一些对象
时间类型 Time
time.Time
代表一个纳秒精度的时间点。
- 程序中应使用 Time 类型值来保存和传递时间,而不是指针。就是说,表示时间的变量和字段,应为 time.Time 类型,而不是 *time.Time. 类型。
- 一个 Time 类型值可以被多个 go 协程同时使用。
- 时间点可以使用 Before、After 和 Equal 方法进行比较。Sub 方法让两个时间点相减,生成一个 Duration 类型值(代表时间段)。Add 方法给一个时间点加上一个时间段,生成一个新的 Time 类型时间点。
- 通过
==
比较 Time 时,Location 信息也会参与比较,因此 Time 不应该作为 map 的 key。
内部结构
type Time struct {
// sec gives the number of seconds elapsed since
// January 1, year 1 00:00:00 UTC.
sec int64
// nsec specifies a non-negative nanosecond
// offset within the second named by Seconds.
// It must be in the range [0, 999999999].
nsec int32
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
// that correspond to this Time.
// Only the zero Time has a nil Location.
// In that case it is interpreted to mean UTC.
loc *Location
}
time.Now()
我们可以通过 time.Now()
函数获取当前的时间对象,然后获取时间对象的年月日时分秒等信息。
// Now returns the current local time.
func Now() Time {
sec, nsec := now()
return Time{sec + unixToInternal, nsec, Local}
}
now()
的具体实现在 runtime
包中,以 linux/amd64 为例,在 sys_linux_amd64.s 中的 time · now
,这是汇编实现的
回到
time.Now()
的实现,现在我们得到了 sec 和 nsec,从Time{sec + unixToInternal, nsec, Local}
这句可以看出,Time 结构的 sec 并非 Unix 时间戳,实际上,加上的unixToInternal
是 1-1-1 到 1970-1-1 经历的秒数。也就是Time
中的 sec 是从 1-1-1 算起的秒数,而不是 Unix 时间戳。
示例代码如下:
func timeDemo() {
now := time.Now() //获取当前时间
fmt.Printf("current time:%v\n", now)
year := now.Year() //年
month := now.Month() //月
day := now.Day() //日
hour := now.Hour() //小时
minute := now.Minute() //分钟
second := now.Second() //秒
fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
}
时间戳
时间戳是自 1970 年 1 月 1 日(08:00:00GMT)至当前时间的总毫秒数。它也被称为 Unix 时间戳(UnixTimestamp)。
time.Time.Unix() //得到 Unix 时间戳;
time.Time.UnixNano() //得到 Unix 时间戳的纳秒表示;
基于时间对象获取时间戳的示例代码如下:
func timestampDemo() {
now := time.Now() //获取当前时间
timestamp1 := now.Unix() //时间戳
timestamp2 := now.UnixNano() //纳秒时间戳
fmt.Printf("current timestamp1:%v\n", timestamp1)
fmt.Printf("current timestamp2:%v\n", timestamp2)
}
使用 time.Unix()
函数可以将时间戳转为时间格式。
func timestampDemo2(timestamp int64) {
timeObj := time.Unix(timestamp, 0) //将时间戳转为时间格式
fmt.Println(timeObj)
}
时间间隔 Duration 对象
time.Duration 是 time 包定义的一个类型,它代表两个时间点之间经过的时间,以纳秒为单位。time.Duration 表示一段时间间隔,可表示的最长时间段大约 290 年。
time 包中定义的时间间隔类型的常量如下:
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
例如:time.Duration 表示 1 纳秒,time.Second 表示 1 秒。
时间操作
Add
我们在日常的编码过程中可能会遇到要求时间 + 时间间隔的需求,Go 语言的时间对象有提供 Add 方法如下:
func (t Time) Add(d Duration) Time
举个例子,求一个小时之后的时间:
func main() {
now := time.Now()
later := now.Add(time.Hour) // 当前时间加1小时后的时间
fmt.Println(later)
}
Sub
求两个时间之间的差值:
func (t Time) Sub(u Time) Duration
返回一个时间段 t-u。如果结果超出了 Duration 可以表示的最大值/最小值,将返回最大值/最小值。
要获取时间点 t-d(d 为 Duration),可以使用 t.Add(-d)。
Equal
func (t Time) Equal(u Time) bool
判断两个时间是否相同,会考虑时区的影响,因此不同时区标准的时间也可以正确比较。
本方法和用 t==u 不同,这种方法还会比较地点和时区信息。
Before
func (t Time) Before(u Time) bool
如果 t 代表的时间点在 u 之前,返回真; 否则返回假。
After
func (t Time) After(u Time) bool
如果 t 代表的时间点在 u 之后,返回真;否则返回假。