分享
golang中的时间和时区
云上听风 · · 14942 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
先写一段测试代码:
const TIME_LAYOUT = "2006年01月02日 15:04:05"
func parseWithLocation(name string, timeStr string) (time.Time, error) {
locationName := name
if l, err := time.LoadLocation(locationName); err != nil {
println(err.Error())
return time.Time{}, err
} else {
lt, _ := time.ParseInLocation(TIME_LAYOUT, timeStr, l)
fmt.Println(locationName, lt)
return lt, nil
}
}
func testTime() {
fmt.Println("0. now: ", time.Now())
str := "2018年09月10日 00:00:00"
fmt.Println("1. str: ", str)
t, _ := time.Parse(TIME_LAYOUT, str)
fmt.Println("2. Parse time: ", t)
tStr := t.Format(TIME_LAYOUT)
fmt.Println("3. Format time str: ", tStr)
name, offset := t.Zone()
name2, offset2 := t.Local().Zone()
fmt.Printf("4. Zone name: %v, Zone offset: %v\n", name, offset)
fmt.Printf("5. Local Zone name: %v, Local Zone offset: %v\n", name2, offset2)
tLocal := t.Local()
tUTC := t.UTC()
fmt.Printf("6. t: %v, Local: %v, UTC: %v\n", t, tLocal, tUTC)
fmt.Printf("7. t: %v, Local: %v, UTC: %v\n", t.Format(TIME_LAYOUT), tLocal.Format(TIME_LAYOUT), tUTC.Format(TIME_LAYOUT))
fmt.Printf("8. Local.Unix: %v, UTC.Unix: %v\n", tLocal.Unix(), tUTC.Unix())
str2 := "1969年12月31日 23:59:59"
t2, _ := time.Parse(TIME_LAYOUT, str2)
fmt.Printf("9. str2:%v,time: %v, Unix: %v\n", str2, t2, t2.Unix())
fmt.Printf("10. %v, %v\n", tLocal.Format(time.ANSIC), tUTC.Format(time.ANSIC))
fmt.Printf("11. %v, %v\n", tLocal.Format(time.RFC822), tUTC.Format(time.RFC822))
fmt.Printf("12. %v, %v\n", tLocal.Format(time.RFC822Z), tUTC.Format(time.RFC822Z))
//指定时区
parseWithLocation("America/Cordoba", str)
parseWithLocation("Asia/Shanghai", str)
parseWithLocation("Asia/Beijing", str)
}
testTime()
输出:
0. now: 2018年09月19日 19:06:07.3642781 +0800 CST m=+0.005995601
1. str: 2018年09月10日 00:00:00
2. Parse time: 2018年09月10日 00:00:00 +0000 UTC
3. Format time str: 2018年09月10日 00:00:00
4. Zone name: UTC, Zone offset: 0
5. Local Zone name: CST, Local Zone offset: 28800
6. t: 2018年09月10日 00:00:00 +0000 UTC, Local: 2018年09月10日 08:00:00 +0800 CST, UTC: 2018年09月10日 00:00:00 +0000 UTC
7. t: 2018年09月10日 00:00:00, Local: 2018年09月10日 08:00:00, UTC: 2018年09月10日 00:00:00
8. Local.Unix: 1536537600, UTC.Unix: 1536537600
9. str2:1969年12月31日 23:59:59,time: 1969年12月31日 23:59:59 +0000 UTC, Unix: -1
10. Mon Sep 10 08:00:00 2018, Mon Sep 10 00:00:00 2018
11. 10 Sep 18 08:00 CST, 10 Sep 18 00:00 UTC
12. 10 Sep 18 08:00 +0800, 10 Sep 18 00:00 +0000
America/Cordoba 2018年09月10日 00:00:00 -0300 -03
Asia/Shanghai 2018年09月10日 00:00:00 +0800 CST
cannot find Asia/Beijing in zip file C:\Go\/lib/time/zoneinfo.zip
从以上代码的测试结果可以得出几点:
-
time.Now得到的当前时间的时区跟电脑的当前时区一样。 -
time.Parse把时间字符串转换为Time,时区是UTC时区。 - 不管Time变量存储的是什么时区,其
Unix()方法返回的都是距离UTC时间:1970年1月1日0点0分0秒的秒数。 -
Unix()返回的秒数可以是负数,如果时间小于1970年01月01日 00:00:00的话。 -
Zone方法可以获得变量的时区和时区与UTC的偏移秒数,应该支持夏令时和冬令时。 -
time.LoadLocation可以根据时区名创建时区Location,所有的时区名字可以在$GOROOT/lib/time/zoneinfo.zip文件中找到,解压zoneinfo.zip可以得到一堆目录和文件,我们只需要目录和文件的名字,时区名是目录名+文件名,比如"Asia/Shanghai"。中国时区名只有"Asia/Shanghai"和"Asia/Chongqing",而没有"Asia/Beijing"。 -
time.ParseInLocation可以根据时间字符串和指定时区转换Time。 - 感谢中国只有一个时区而且没有夏令时和冬令时,可怕的美国居然有6个时区,想想都可怕。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信14942 次点击
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
先写一段测试代码:
const TIME_LAYOUT = "2006年01月02日 15:04:05"
func parseWithLocation(name string, timeStr string) (time.Time, error) {
locationName := name
if l, err := time.LoadLocation(locationName); err != nil {
println(err.Error())
return time.Time{}, err
} else {
lt, _ := time.ParseInLocation(TIME_LAYOUT, timeStr, l)
fmt.Println(locationName, lt)
return lt, nil
}
}
func testTime() {
fmt.Println("0. now: ", time.Now())
str := "2018年09月10日 00:00:00"
fmt.Println("1. str: ", str)
t, _ := time.Parse(TIME_LAYOUT, str)
fmt.Println("2. Parse time: ", t)
tStr := t.Format(TIME_LAYOUT)
fmt.Println("3. Format time str: ", tStr)
name, offset := t.Zone()
name2, offset2 := t.Local().Zone()
fmt.Printf("4. Zone name: %v, Zone offset: %v\n", name, offset)
fmt.Printf("5. Local Zone name: %v, Local Zone offset: %v\n", name2, offset2)
tLocal := t.Local()
tUTC := t.UTC()
fmt.Printf("6. t: %v, Local: %v, UTC: %v\n", t, tLocal, tUTC)
fmt.Printf("7. t: %v, Local: %v, UTC: %v\n", t.Format(TIME_LAYOUT), tLocal.Format(TIME_LAYOUT), tUTC.Format(TIME_LAYOUT))
fmt.Printf("8. Local.Unix: %v, UTC.Unix: %v\n", tLocal.Unix(), tUTC.Unix())
str2 := "1969年12月31日 23:59:59"
t2, _ := time.Parse(TIME_LAYOUT, str2)
fmt.Printf("9. str2:%v,time: %v, Unix: %v\n", str2, t2, t2.Unix())
fmt.Printf("10. %v, %v\n", tLocal.Format(time.ANSIC), tUTC.Format(time.ANSIC))
fmt.Printf("11. %v, %v\n", tLocal.Format(time.RFC822), tUTC.Format(time.RFC822))
fmt.Printf("12. %v, %v\n", tLocal.Format(time.RFC822Z), tUTC.Format(time.RFC822Z))
//指定时区
parseWithLocation("America/Cordoba", str)
parseWithLocation("Asia/Shanghai", str)
parseWithLocation("Asia/Beijing", str)
}
testTime()
输出:
0. now: 2018年09月19日 19:06:07.3642781 +0800 CST m=+0.005995601
1. str: 2018年09月10日 00:00:00
2. Parse time: 2018年09月10日 00:00:00 +0000 UTC
3. Format time str: 2018年09月10日 00:00:00
4. Zone name: UTC, Zone offset: 0
5. Local Zone name: CST, Local Zone offset: 28800
6. t: 2018年09月10日 00:00:00 +0000 UTC, Local: 2018年09月10日 08:00:00 +0800 CST, UTC: 2018年09月10日 00:00:00 +0000 UTC
7. t: 2018年09月10日 00:00:00, Local: 2018年09月10日 08:00:00, UTC: 2018年09月10日 00:00:00
8. Local.Unix: 1536537600, UTC.Unix: 1536537600
9. str2:1969年12月31日 23:59:59,time: 1969年12月31日 23:59:59 +0000 UTC, Unix: -1
10. Mon Sep 10 08:00:00 2018, Mon Sep 10 00:00:00 2018
11. 10 Sep 18 08:00 CST, 10 Sep 18 00:00 UTC
12. 10 Sep 18 08:00 +0800, 10 Sep 18 00:00 +0000
America/Cordoba 2018年09月10日 00:00:00 -0300 -03
Asia/Shanghai 2018年09月10日 00:00:00 +0800 CST
cannot find Asia/Beijing in zip file C:\Go\/lib/time/zoneinfo.zip
从以上代码的测试结果可以得出几点:
-
time.Now得到的当前时间的时区跟电脑的当前时区一样。 -
time.Parse把时间字符串转换为Time,时区是UTC时区。 - 不管Time变量存储的是什么时区,其
Unix()方法返回的都是距离UTC时间:1970年1月1日0点0分0秒的秒数。 -
Unix()返回的秒数可以是负数,如果时间小于1970年01月01日 00:00:00的话。 -
Zone方法可以获得变量的时区和时区与UTC的偏移秒数,应该支持夏令时和冬令时。 -
time.LoadLocation可以根据时区名创建时区Location,所有的时区名字可以在$GOROOT/lib/time/zoneinfo.zip文件中找到,解压zoneinfo.zip可以得到一堆目录和文件,我们只需要目录和文件的名字,时区名是目录名+文件名,比如"Asia/Shanghai"。中国时区名只有"Asia/Shanghai"和"Asia/Chongqing",而没有"Asia/Beijing"。 -
time.ParseInLocation可以根据时间字符串和指定时区转换Time。 - 感谢中国只有一个时区而且没有夏令时和冬令时,可怕的美国居然有6个时区,想想都可怕。