接口文档:

https://pkg.go.dev/github.com/gogf/gf/v2/os/gtime

方法比较简单,比较常用的是以下几个方法:

  1. Timestamp 用于获得当前时间戳, TimestampMilliTimestampMicroTimestampNano 用于获得当前的毫秒、微秒和纳秒值。
  2. DateDatetime 用于获得当前日期及当前日期时间。
  3. SetTimeZone 用于设置当前进程的全局时区。
  4. 其他方法说明请查看接口文档。

示例1,基本使用

创建时间对象及获取当前时间戳。

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/gtime"
  5. )
  6. func main() {
  7. fmt.Println("Date :", gtime.Date())
  8. fmt.Println("Datetime :", gtime.Datetime())
  9. fmt.Println("Second :", gtime.Timestamp())
  10. fmt.Println("Millisecond:", gtime.TimestampMilli())
  11. fmt.Println("Microsecond:", gtime.TimestampMicro())
  12. fmt.Println("Nanosecond :", gtime.TimestampNano())
  13. }

执行后,输出结果为:

  1. Date : 2018-07-22
  2. Datetime : 2018-07-22 11:52:22
  3. Second : 1532231542
  4. Millisecond: 1532231542688
  5. Microsecond: 1532231542688688
  6. Nanosecond : 1532231542688690259

示例2, StrToTime

除了通过 New 方法外,也可以通过 StrToTime 根据常见的时间字符串解析生成 gtime.Time 对象,常见的时间字符串如下:

  1. 2017-12-14 04:51:34 +0805 LMT
  2. 2017-12-14 04:51:34 +0805 LMT
  3. 2006-01-02T15:04:05Z07:00
  4. 2014-01-17T01:19:15+08:00
  5. 2018-02-09T20:46:17.897Z
  6. 2018-02-09 20:46:17.897
  7. 2018-02-09T20:46:17Z
  8. 2018-02-09 20:46:17
  9. 2018/10/31 - 16:38:46
  10. 2018-02-09
  11. 2018.02.09
  12. 01-Nov-2018 11:50:28
  13. 01/Nov/2018 11:50:28
  14. 01.Nov.2018 11:50:28
  15. 01.Nov.2018:11:50:28
  16. 日期连接符号支持'-''/''.'

使用示例:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/os/glog"
  5. "github.com/gogf/gf/v2/os/gtime"
  6. "time"
  7. )
  8. func main() {
  9. array := []string{
  10. "2017-12-14 04:51:34 +0805 LMT",
  11. "2006-01-02T15:04:05Z07:00",
  12. "2014-01-17T01:19:15+08:00",
  13. "2018-02-09T20:46:17.897Z",
  14. "2018-02-09 20:46:17.897",
  15. "2018-02-09T20:46:17Z",
  16. "2018-02-09 20:46:17",
  17. "2018.02.09 20:46:17",
  18. "2018-02-09",
  19. "2017/12/14 04:51:34 +0805 LMT",
  20. "2018/02/09 12:00:15",
  21. "01/Nov/2018:13:28:13 +0800",
  22. "01-Nov-2018 11:50:28 +0805 LMT",
  23. "01-Nov-2018T15:04:05Z07:00",
  24. "01-Nov-2018T01:19:15+08:00",
  25. "01-Nov-2018 11:50:28 +0805 LMT",
  26. "01/Nov/2018 11:50:28",
  27. "01/Nov/2018:11:50:28",
  28. "01.Nov.2018:11:50:28",
  29. "01/Nov/2018",
  30. }
  31. cstLocal, _ := time.LoadLocation("Asia/Shanghai")
  32. for _, s := range array {
  33. if t, err := gtime.StrToTime(s); err == nil {
  34. fmt.Println(s)
  35. fmt.Println(t.UTC().String())
  36. fmt.Println(t.In(cstLocal).String())
  37. } else {
  38. glog.Error(s, err)
  39. }
  40. fmt.Println()
  41. }
  42. }

在这个示例中,将部分时间格式串使用 StrToTime 方法转换为 gtime.Time 对象,并输出该事件的 UTC 时间和 CST 时间(上海时区时间)。 执行后,输出结果为:

  1. 2017-12-14 04:51:34 +0805 LMT
  2. 2017-12-13 20:46:34
  3. 2017-12-14 04:46:34 +0800 CST
  4. 2006-01-02T15:04:05Z07:00
  5. 2006-01-02 22:04:05
  6. 2006-01-03 06:04:05 +0800 CST
  7. 2014-01-17T01:19:15+08:00
  8. 2014-01-16 17:19:15
  9. 2014-01-17 01:19:15 +0800 CST
  10. 2018-02-09T20:46:17.897Z
  11. 2018-02-09 20:46:17
  12. 2018-02-10 04:46:17.897 +0800 CST
  13. 2018-02-09 20:46:17.897
  14. 2018-02-09 12:46:17
  15. 2018-02-09 20:46:17.897 +0800 CST
  16. 2018-02-09T20:46:17Z
  17. 2018-02-09 20:46:17
  18. 2018-02-10 04:46:17 +0800 CST
  19. 2018-02-09 20:46:17
  20. 2018-02-09 12:46:17
  21. 2018-02-09 20:46:17 +0800 CST
  22. 2018.02.09 20:46:17
  23. 2018-02-09 12:46:17
  24. 2018-02-09 20:46:17 +0800 CST
  25. 2018-02-09
  26. 2018-02-08 16:00:00
  27. 2018-02-09 00:00:00 +0800 CST
  28. 2017/12/14 04:51:34 +0805 LMT
  29. 2017-12-13 20:46:34
  30. 2017-12-14 04:46:34 +0800 CST
  31. 2018/02/09 12:00:15
  32. 2018-02-09 04:00:15
  33. 2018-02-09 12:00:15 +0800 CST
  34. 01/Nov/2018:13:28:13 +0800
  35. 2018-11-01 05:28:13
  36. 2018-11-01 13:28:13 +0800 CST
  37. 01-Nov-2018 11:50:28 +0805 LMT
  38. 2018-11-01 03:45:28
  39. 2018-11-01 11:45:28 +0800 CST
  40. 01-Nov-2018T15:04:05Z07:00
  41. 2018-11-01 22:04:05
  42. 2018-11-02 06:04:05 +0800 CST
  43. 01-Nov-2018T01:19:15+08:00
  44. 2018-10-31 17:19:15
  45. 2018-11-01 01:19:15 +0800 CST
  46. 01-Nov-2018 11:50:28 +0805 LMT
  47. 2018-11-01 03:45:28
  48. 2018-11-01 11:45:28 +0800 CST
  49. 01/Nov/2018 11:50:28
  50. 2018-11-01 03:50:28
  51. 2018-11-01 11:50:28 +0800 CST
  52. 01/Nov/2018:11:50:28
  53. 2018-11-01 03:50:28
  54. 2018-11-01 11:50:28 +0800 CST
  55. 01.Nov.2018:11:50:28
  56. 2018-11-01 03:50:28
  57. 2018-11-01 11:50:28 +0800 CST
  58. 01/Nov/2018
  59. 2018-10-31 16:00:00
  60. 2018-11-01 00:00:00 +0800 CST