1、+0800 CST 与+0805 LMT区别
大多数计算机保留的有关时区的数据来自IANA时区数据库(Time Zone Database, TZDB)。TZDB 中 "Asia/Shanghai"的时区偏移 不止 +08:00 一个,在 1850-1900 年,偏移量为 +08:05:43,1901年以及后,偏移量才是 +08:00。所以时区+0800 CST 与+0805 LMT都是中国时区,不过对应的是不同的阶段。
更详细的就是以LMT时间1901年,也就是UTC时间1900/12/31 15:54:17,之前都是使用LMT时区偏移量,之后都使用CST时区偏移量。如下例子可以直观感受:
//go
t0 := time.Date(1900, 12, 31, 15, 54, 16, 59, time.UTC)
t1 := time.Date(1900, 12, 31, 15, 54, 17, 0, time.UTC)
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println(err)
}
fmt.Println("UTC 1900/12/31 15:54:17之前:")
fmt.Println(t0.In(loc))
fmt.Println("Location:", t0.In(loc).Location())
t0zone, t0offset := t0.In(loc).Zone()
fmt.Println("zone:", t0zone)
fmt.Println("offset:", t0offset)
fmt.Println("UTC 1900/12/31 15:54:17以后:")
fmt.Println(t1.In(loc))
fmt.Println("Location:", t1.In(loc).Location())
t1zone, t1offset := t1.In(loc).Zone()
fmt.Println("zone:", t1zone)
fmt.Println("offset:", t1offset)
输出结果:
UTC 1900/12/31 15:54:17之前:
1900-12-31 23:59:59.000000059 +0805 LMT
Location: Asia/Shanghai
zone: LMT
offset: 29143
UTC 1900/12/31 15:54:17以后:
1900-12-31 23:54:17 +0800 CST
Location: Asia/Shanghai
zone: CST
offset: 28800
2、LMT转CST
那么怎么对UTC 1900/12/31 15:54:17之前的时间显示成CST呢,如下:
//go
tim := time.Date(1800, 12, 31, 15, 54, 17, 59, time.UTC)
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println(err)
}
fmt.Println(tim.In(loc))
shanghai_cst := time.FixedZone("CST", 8*60*60)
fmt.Println(tim.In(shanghai_cst))
输出:
1801-01-01 00:00:00.000000059 +0805 LMT
1800-12-31 23:54:17.000000059 +0800 CST
3、CST转LMT
同理对UTC 1900/12/31 15:54:17之后的时间也可以显示成LMT,如下:
tim := time.Date(2000, 12, 31, 15, 54, 17, 59, time.UTC)
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println(err)
}
fmt.Println(tim.In(loc))
shanghai_lmt := time.FixedZone("LMT", 8*60*60+5*60+43)
fmt.Println(tim.In(shanghai_lmt))
输出结果:
2000-12-31 23:54:17.000000059 +0800 CST
2001-01-01 00:00:00.000000059 +0805 LMT
其实8*60*60,8*60*60+5*60+43就是俩个时区的偏移量