1 time包
1.1 相关类型
Location
代表一个地区,并表示该地区所在的时区
Time
代表一个纳秒精度的时间点
Duration
纳秒时间差,最大290年,原因是 type Duration int64
Timer 和 Ticker
定时器
Weekday 和 Month
星期以及月份
1.2 Time 类型详解
Time 的内部结构
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()
函数。
// Now returns the current local time.
func Now() Time {
sec, nsec := now()
return Time{sec + unixToInternal, nsec, Local}
}
零值的判断
因为 Time
的零值是 sec 和 nsec 都是 0,表示 1 年 1 月 1 日。
Time.IsZero() 函数用于判断 Time 表示的时间是否是 0 值。
与 Unix 时间戳的转换
相关函数或方法:
- time.Unix(sec, nsec int64) 通过 Unix 时间戳生成
time.Time
实例; - time.Time.Unix() 得到 Unix 时间戳;
- time.Time.UnixNano() 得到 Unix 时间戳的纳秒表示;
格式化和解析
- time.Parse 和 time.ParseInLocation
- time.Time.Format