• 工具方法
    • 示例1,基本使用
    • 示例2,设置时区
    • 示例3,StrToTime

    工具方法

    https://godoc.org/github.com/gogf/gf/g/os/gtime

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

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

    示例1,基本使用

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/g/os/gtime"
    5. )
    6. func main() {
    7. fmt.Println("Date :", gtime.Date())
    8. fmt.Println("Datetime :", gtime.Datetime())
    9. fmt.Println("Second :", gtime.Second())
    10. fmt.Println("Millisecond:", gtime.Millisecond())
    11. fmt.Println("Microsecond:", gtime.Microsecond())
    12. fmt.Println("Nanosecond :", gtime.Nanosecond())
    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,设置时区

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/gogf/gf/g/os/gtime"
    5. "time"
    6. )
    7. func main() {
    8. // 先使用标准库打印当前时间
    9. fmt.Println(time.Now().String())
    10. // 设置进程全局时区
    11. err := gtime.SetTimeZone("Asia/Tokyo")
    12. if err != nil {
    13. panic(err)
    14. }
    15. // 使用gtime获取当前时间
    16. fmt.Println(gtime.Now().String())
    17. // 使用标准库获取当前时间
    18. fmt.Println(time.Now().String())
    19. }

    执行后,输出结果为:

    1. 2018-11-21 22:50:56.723429 +0800 CST m=+0.000649366
    2. 2018-11-21 23:50:56
    3. 2018-11-21 23:50:56.723832 +0900 JST m=+0.001052780

    示例3,StrToTime

    gtime支持常见的时间字符串解析,生成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. 日期连接符号支持'-'、'/'、'.'

    使用示例:

    package main
    
    import (
        "fmt"
        "github.com/gogf/gf/g/os/glog"
        "github.com/gogf/gf/g/os/gtime"
        "time"
    )
    
    func main() {
        array := []string{
            "2017-12-14 04:51:34 +0805 LMT",
            "2006-01-02T15:04:05Z07:00",
            "2014-01-17T01:19:15+08:00",
            "2018-02-09T20:46:17.897Z",
            "2018-02-09 20:46:17.897",
            "2018-02-09T20:46:17Z",
            "2018-02-09 20:46:17",
            "2018.02.09 20:46:17",
            "2018-02-09",
            "2017/12/14 04:51:34 +0805 LMT",
            "2018/02/09 12:00:15",
            "01/Nov/2018:13:28:13 +0800",
            "01-Nov-2018 11:50:28 +0805 LMT",
            "01-Nov-2018T15:04:05Z07:00",
            "01-Nov-2018T01:19:15+08:00",
            "01-Nov-2018 11:50:28 +0805 LMT",
            "01/Nov/2018 11:50:28",
            "01/Nov/2018:11:50:28",
            "01.Nov.2018:11:50:28",
            "01/Nov/2018",
        }
        cstLocal, _ := time.LoadLocation("Asia/Shanghai")
        for _, s := range array {
            if t, err := gtime.StrToTime(s); err == nil {
                fmt.Println(s)
                fmt.Println(t.UTC().String())
                fmt.Println(t.In(cstLocal).String())
            } else {
                glog.Error(s, err)
            }
            fmt.Println()
        }
    }
    

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

    2017-12-14 04:51:34 +0805 LMT
    2017-12-13 20:46:34
    2017-12-14 04:46:34 +0800 CST
    
    2006-01-02T15:04:05Z07:00
    2006-01-02 22:04:05
    2006-01-03 06:04:05 +0800 CST
    
    2014-01-17T01:19:15+08:00
    2014-01-16 17:19:15
    2014-01-17 01:19:15 +0800 CST
    
    2018-02-09T20:46:17.897Z
    2018-02-09 20:46:17
    2018-02-10 04:46:17.897 +0800 CST
    
    2018-02-09 20:46:17.897
    2018-02-09 12:46:17
    2018-02-09 20:46:17.897 +0800 CST
    
    2018-02-09T20:46:17Z
    2018-02-09 20:46:17
    2018-02-10 04:46:17 +0800 CST
    
    2018-02-09 20:46:17
    2018-02-09 12:46:17
    2018-02-09 20:46:17 +0800 CST
    
    2018.02.09 20:46:17
    2018-02-09 12:46:17
    2018-02-09 20:46:17 +0800 CST
    
    2018-02-09
    2018-02-08 16:00:00
    2018-02-09 00:00:00 +0800 CST
    
    2017/12/14 04:51:34 +0805 LMT
    2017-12-13 20:46:34
    2017-12-14 04:46:34 +0800 CST
    
    2018/02/09 12:00:15
    2018-02-09 04:00:15
    2018-02-09 12:00:15 +0800 CST
    
    01/Nov/2018:13:28:13 +0800
    2018-11-01 05:28:13
    2018-11-01 13:28:13 +0800 CST
    
    01-Nov-2018 11:50:28 +0805 LMT
    2018-11-01 03:45:28
    2018-11-01 11:45:28 +0800 CST
    
    01-Nov-2018T15:04:05Z07:00
    2018-11-01 22:04:05
    2018-11-02 06:04:05 +0800 CST
    
    01-Nov-2018T01:19:15+08:00
    2018-10-31 17:19:15
    2018-11-01 01:19:15 +0800 CST
    
    01-Nov-2018 11:50:28 +0805 LMT
    2018-11-01 03:45:28
    2018-11-01 11:45:28 +0800 CST
    
    01/Nov/2018 11:50:28
    2018-11-01 03:50:28
    2018-11-01 11:50:28 +0800 CST
    
    01/Nov/2018:11:50:28
    2018-11-01 03:50:28
    2018-11-01 11:50:28 +0800 CST
    
    01.Nov.2018:11:50:28
    2018-11-01 03:50:28
    2018-11-01 11:50:28 +0800 CST
    
    01/Nov/2018
    2018-10-31 16:00:00
    2018-11-01 00:00:00 +0800 CST