- 工具方法
- 示例1,基本使用
- 示例2,设置时区
- 示例3,
StrToTime
工具方法
https://godoc.org/github.com/gogf/gf/g/os/gtime
方法比较简单,比较常用的是以下几个方法;
Second
用于获得当前时间戳,Millisecond
、Microsecond
及Nanosecond
用于获得当前的毫秒、微秒和纳秒值;Date
和Datetime
用于获得当前日期及当前日期时间;SetTimeZone
用于设置当前进程的全局时区;- 其他方法说明请查看接口文档;
示例1,基本使用
package main
import (
"fmt"
"github.com/gogf/gf/g/os/gtime"
)
func main() {
fmt.Println("Date :", gtime.Date())
fmt.Println("Datetime :", gtime.Datetime())
fmt.Println("Second :", gtime.Second())
fmt.Println("Millisecond:", gtime.Millisecond())
fmt.Println("Microsecond:", gtime.Microsecond())
fmt.Println("Nanosecond :", gtime.Nanosecond())
}
执行后,输出结果为:
Date : 2018-07-22
Datetime : 2018-07-22 11:52:22
Second : 1532231542
Millisecond: 1532231542688
Microsecond: 1532231542688688
Nanosecond : 1532231542688690259
示例2,设置时区
package main
import (
"fmt"
"github.com/gogf/gf/g/os/gtime"
"time"
)
func main() {
// 先使用标准库打印当前时间
fmt.Println(time.Now().String())
// 设置进程全局时区
err := gtime.SetTimeZone("Asia/Tokyo")
if err != nil {
panic(err)
}
// 使用gtime获取当前时间
fmt.Println(gtime.Now().String())
// 使用标准库获取当前时间
fmt.Println(time.Now().String())
}
执行后,输出结果为:
2018-11-21 22:50:56.723429 +0800 CST m=+0.000649366
2018-11-21 23:50:56
2018-11-21 23:50:56.723832 +0900 JST m=+0.001052780
示例3,StrToTime
gtime
支持常见的时间字符串解析,生成gtime.Time
对象,常见的时间字符串如下:
"2017-12-14 04:51:34 +0805 LMT",
"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/10/31 - 16:38:46"
"2018-02-09",
"2018.02.09",
01-Nov-2018 11:50:28
01/Nov/2018 11:50:28
01.Nov.2018 11:50:28
01.Nov.2018:11:50:28
日期连接符号支持'-'、'/'、'.'
使用示例:
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