8.12 Go语言time包:时间和日期 - Go语言中文社区

8.12 Go语言time包:时间和日期


时间和日期是我们开发中经常会用到的,Go语言中的 time 包提供了时间显示和测量等所用的函数,本节我们就来介绍一下 time 包的基本用法。

time 包简介

时间一般包含时间值和时区,可以从Go语言中 time 包的源码中看出:

  1. type Time struct {
  2. // wall and ext encode the wall time seconds, wall time nanoseconds,
  3. // and optional monotonic clock reading in nanoseconds.
  4. //
  5. // From high to low bit position, wall encodes a 1-bit flag (hasMonotonic),
  6. // a 33-bit seconds field, and a 30-bit wall time nanoseconds field.
  7. // The nanoseconds field is in the range [0, 999999999].
  8. // If the hasMonotonic bit is 0, then the 33-bit field must be zero
  9. // and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext.
  10. // If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit
  11. // unsigned wall seconds since Jan 1 year 1885, and ext holds a
  12. // signed 64-bit monotonic clock reading, nanoseconds since process start.
  13. wall uint64
  14. ext int64
  15. // loc specifies the Location that should be used to
  16. // determine the minute, hour, month, day, and year
  17. // that correspond to this Time.
  18. // The nil location means UTC.
  19. // All UTC times are represented with loc==nil, never loc==&utcLoc.
  20. loc *Location
  21. }

上面代码中:

  • wall:表示距离公元 1 年 1 月 1 日 00:00:00UTC 的秒数;
  • ext:表示纳秒;
  • loc:代表时区,主要处理偏移量,不同的时区,对应的时间不一样。
如何正确表示时间呢?

公认最准确的计算应该是使用“原子震荡周期”所计算的物理时钟了(Atomic Clock, 也被称为原子钟),这也被定义为标准时间(International Atomic Time)。

而我们常常看见的 UTC(Universal Time Coordinated,世界协调时间)就是利用这种 Atomic Clock 为基准所定义出来的正确时间。UTC 标准时间是以 GMT(Greenwich Mean Time,格林尼治时间)这个时区为主,所以本地时间与 UTC 时间的时差就是本地时间与 GMT 时间的时差。

UTC + 时区差 = 本地时间

国内一般使用的是北京时间,与 UTC 的时间关系如下:

UTC + 8 个小时 = 北京时间

在Go语言的 time 包里面有两个时区变量,如下:

  1. time.UTCUTC 时间
  2. time.Local:本地时间

同时,Go语言还提供了 LoadLocation 方法和 FixedZone 方法来获取时区变量,如下:

  1. FixedZone(name string, offset int) *Location

其中,name 为时区名称,offset 是与 UTC 之前的时差。

  1. LoadLocation(name string) (*Location, error)

其中,name 为时区的名字。

时间的获取

1) 获取当前时间

我们可以通过 time.Now() 函数来获取当前的时间对象,然后通过事件对象来获取当前的时间信息。示例代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. now := time.Now() //获取当前时间
  8. fmt.Printf("current time:%v\n", now)
  9. year := now.Year() //年
  10. month := now.Month() //月
  11. day := now.Day() //日
  12. hour := now.Hour() //小时
  13. minute := now.Minute() //分钟
  14. second := now.Second() //秒
  15. fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
  16. }

运行结果如下:

  • current time:2019-12-12 12:33:19.4712277 +0800 CST m=+0.006980401
  • 2019-12-12 12:33:19
2) 获取时间戳

时间戳是自 1970 年 1 月 1 日(08:00:00GMT)至当前时间的总毫秒数,它也被称为 Unix 时间戳(UnixTimestamp)。

基于时间对象获取时间戳的示例代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. now := time.Now() //获取当前时间
  8. timestamp1 := now.Unix() //时间戳
  9. timestamp2 := now.UnixNano() //纳秒时间戳
  10. fmt.Printf("现在的时间戳:%v\n", timestamp1)
  11. fmt.Printf("现在的纳秒时间戳:%v\n", timestamp2)
  12. }

运行结果如下:

  • 现在的时间戳:1576127858
  • 现在的纳秒时间戳:1576127858829900100

使用 time.Unix() 函数可以将时间戳转为时间格式,示例代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. now := time.Now() //获取当前时间
  8. timestamp := now.Unix() //时间戳
  9. timeObj := time.Unix(timestamp, 0) //将时间戳转为时间格式
  10. fmt.Println(timeObj)
  11. year := timeObj.Year() //年
  12. month := timeObj.Month() //月
  13. day := timeObj.Day() //日
  14. hour := timeObj.Hour() //小时
  15. minute := timeObj.Minute() //分钟
  16. second := timeObj.Second() //秒
  17. fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second)
  18. }

运行结果如下:

  • 2019-12-12 13:24:09 +0800 CST
  • 2019-12-12 13:24:09
3) 获取当前是星期几

time 包中的 Weekday 函数能够返回某个时间点所对应是一周中的周几,示例代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. //时间戳
  8. t := time.Now()
  9. fmt.Println(t.Weekday().String())
  10. }

运行结果如下:

Thursday

时间操作函数

1) Add

我们在日常的开发过程中可能会遇到要求某个时间 + 时间间隔之类的需求,Go语言中的 Add 方法如下:

  1. func (t Time) Add(d Duration) Time

Add 函数可以返回时间点 t + 时间间隔 d 的值。

【示例】求一个小时之后的时间:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. now := time.Now()
  8. later := now.Add(time.Hour) // 当前时间加1小时后的时间
  9. fmt.Println(later)
  10. }

运行结果如下:

  • 2019-12-12 16:00:29.9866943 +0800 CST m=+3600.007978201
2) Sub

求两个时间之间的差值:

  1. func (t Time) Sub(u Time) Duration

返回一个时间段 t - u 的值。如果结果超出了 Duration 可以表示的最大值或最小值,将返回最大值或最小值,要获取时间点 t - d(d 为 Duration),可以使用 t.Add(-d)。

3) Equal

判断两个时间是否相同:

  1. func (t Time) Equal(u Time) bool

Equal 函数会考虑时区的影响,因此不同时区标准的时间也可以正确比较,Equal 方法和用 t==u 不同,Equal 方法还会比较地点和时区信息。

4) Before

判断一个时间点是否在另一个时间点之前:

  1. func (t Time) Before(u Time) bool

如果 t 代表的时间点在 u 之前,则返回真,否则返回假。

5) After

判断一个时间点是否在另一个时间点之后:

  1. func (t Time) After(u Time) bool

如果 t 代表的时间点在 u 之后,则返回真,否则返回假。

定时器

使用 time.Tick(时间间隔) 可以设置定时器,定时器的本质上是一个通道(channel),示例代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. ticker := time.Tick(time.Second) //定义一个1秒间隔的定时器
  8. for i := range ticker {
  9. fmt.Println(i) //每秒都会执行的任务
  10. }
  11. }

运行结果如下:

  • 2019-12-12 15:14:26.4158067 +0800 CST m=+16.007460701
  • 2019-12-12 15:14:27.4159467 +0800 CST m=+17.007600701
  • 2019-12-12 15:14:28.4144689 +0800 CST m=+18.006122901
  • 2019-12-12 15:14:29.4159581 +0800 CST m=+19.007612101
  • 2019-12-12 15:14:30.4144337 +0800 CST m=+20.006087701

时间格式化

时间类型有一个自带的 Format 方法进行格式化,需要注意的是Go语言中格式化时间模板不是常见的Y-m-d H:M:S 而是使用Go语言的诞生时间 2006 年 1 月 2 号 15 点 04 分 05 秒。

提示:如果想将时间格式化为 12 小时格式,需指定 PM。

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. now := time.Now()
  8. // 格式化的模板为Go的出生时间2006年1月2号15点04分 Mon Jan
  9. // 24小时制
  10. fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan"))
  11. // 12小时制
  12. fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan"))
  13. fmt.Println(now.Format("2006/01/02 15:04"))
  14. fmt.Println(now.Format("15:04 2006/01/02"))
  15. fmt.Println(now.Format("2006/01/02"))
  16. }

运行结果如下:

  • 2019-12-12 15:20:52.037 Thu Dec
  • 2019-12-12 03:20:52.037 PM Thu Dec
  • 2019/12/12 15:20
  • 15:20 2019/12/12
  • 2019/12/12

解析字符串格式的时间

Parse 函数可以解析一个格式化的时间字符串并返回它代表的时间。

  1. func Parse(layout, value string) (Time, error)

与 Parse 函数类似的还有 ParseInLocation 函数。

  1. func ParseInLocation(layout, value string, loc *Location) (Time, error)

ParseInLocation 与 Parse 函数类似,但有两个重要的不同之处:

  • 第一,当缺少时区信息时,Parse 将时间解释为 UTC 时间,而 ParseInLocation 将返回值的 Location 设置为 loc;
  • 第二,当时间字符串提供了时区偏移量信息时,Parse 会尝试去匹配本地时区,而 ParseInLocation 会去匹配 loc。

示例代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. var layout string = "2006-01-02 15:04:05"
  8. var timeStr string = "2019-12-12 15:22:12"
  9. timeObj1, _ := time.Parse(layout, timeStr)
  10. fmt.Println(timeObj1)
  11. timeObj2, _ := time.ParseInLocation(layout, timeStr, time.Local)
  12. fmt.Println(timeObj2)
  13. }

运行结果如下:

  • 2019-12-12 15:22:12 +0000 UTC
  • 2019-12-12 15:22:12 +0800 CST
版权声明:本教程内容除了本站原创内容外,还有来源自C语言编程网,博客园,CSDN等技术站点,感谢相关博主原创文章,转载请附上原文出处链接和本声明。
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 阅读 ( 612 )
  • 分类:Go

0 条评论

官方社群

GO教程

猜你喜欢