go标准包系列-Time包

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值