Go时区Parse的坑

2021/11/23 实践总结Go

# 背景

当有一个日期字符串(2014-11-12 11:45:26)需要转化为Go的time类型,我们第一时间肯定会想到time包的Parse方法,指定字符串的格式layout:

layout := "2006-01-02 15:04:05"
str := "2014-11-12 11:45:26"
t, err := time.Parse(layout, str)
1
2
3

但是,有个容易忽略的问题,go语言默认的时区用的是UTC,而我们一般要的是本地时区,可以看一下源码:

package time
func Parse(layout, value string) (Time, error) {
   return parse(layout, value, UTC, Local)
}
1
2
3
4

这个又和获取当前时间(time.Now)不同,time.Now用的又是本地时区:

var Local *Location = &localLoc
 
func Now() Time {
   sec, nsec, mono := now()
   mono -= startNano
   sec += unixToInternal - minWall
   if uint64(sec)>>33 != 0 {
      return Time{uint64(nsec), sec + minWall, Local}
   }
   return Time{hasMonotonic | uint64(sec)<<nsecShift | uint64(nsec), mono, Local}
}
1
2
3
4
5
6
7
8
9
10
11

# 解决方案

这时需要用ParseInLocation方法,指定时区为time.Local

layout := "2006-01-02 15:04:05"
str := "2014-11-12 11:45:26"
t, err := time.ParseInLocation(layout, str, time.Local)
1
2
3

另外有更好的方式,日期格式化的时候也加上时区例如:2006-01-02T15:04:05-0700,这样time.Parse 就能正确转化为指定时区:

format:="2006-01-02T15:04:05-0700" // 0700 表示时区
str := time.Now().Format(format)
fmt.Println(str) // 输出:2021-05-08T15:53:33+0800
t, _ := time.Parse(format, str)
fmt.Println(t) // 输出:2021-05-08 15:53:33 +0800 CST
1
2
3
4
5

# 总结

如果系统要支持国外和国内用户,建议是格式化采用:2006-01-02T15:04:05-0700,带时区的格式化,这样就能避免时区的解析错误问题