Golang时区的夏令时无法正常工作。

huangapple go评论74阅读模式
英文:

golang timezone daylight savings time doesn't work

问题

我知道在2014年10月26日凌晨2点时钟应该倒退1小时,但是我无法使用一个简单的golang程序来复现这个情况。

const timeFormat = "2 Jan, 2006 3:04pm"

loc, err := time.LoadLocation("Europe/Moscow")
log.Print(loc, err)

testz , _ := time.ParseInLocation( timeFormat, "26 Oct, 2014 01:59am",  loc)
fmt.Println( testz , testz.UTC())
testz = testz.Add( time.Minute )
fmt.Println( testz , testz.UTC())
testz = testz.Add( time.Minute )
fmt.Println( testz , testz.UTC())

输出结果为:

2014-10-26 01:59:00 +0300 MSK 2014-10-25 22:59:00 +0000 UTC
2014-10-26 02:00:00 +0300 MSK 2014-10-25 23:00:00 +0000 UTC
2014-10-26 02:01:00 +0300 MSK 2014-10-25 23:01:00 +0000 UTC

使用的是go 1.6版本,运行在Linux系统上。

英文:

I know that on 26 Oct, 2014 02:00am the clock should go backwards 1 hour, but I can't reproduce this using a simple golang program

const timeFormat = "2 Jan, 2006 3:04pm"

loc, err := time.LoadLocation("Europe/Moscow")
log.Print(loc, err)

testz , _ := time.ParseInLocation( timeFormat, "26 Oct, 2014 01:59am",  loc)
fmt.Println( testz , testz.UTC())
testz = testz.Add( time.Minute )
fmt.Println( testz , testz.UTC())
testz = testz.Add( time.Minute )
fmt.Println( testz , testz.UTC())

outputs

2014-10-26 01:59:00 +0300 MSK 2014-10-25 22:59:00 +0000 UTC
2014-10-26 02:00:00 +0300 MSK 2014-10-25 23:00:00 +0000 UTC
2014-10-26 02:01:00 +0300 MSK 2014-10-25 23:01:00 +0000 UTC

go 1.6, linux

答案1

得分: 2

根据你的输出,看起来这行代码:

testz , _ := time.ParseInLocation(timeFormat, "26 Oct, 2014 01:59am", loc)

...实际上给出的是两个1:59am中较晚的一个,因为它显示了一个+3的偏移量。换句话说,你看到的是比你想要的时间晚一个小时的三分钟——几乎是在夏令时转换后的一个小时之后开始。

你可以将代码更改为从一个明确的值开始,像这样:

testz, _ := time.ParseInLocation(timeFormat, "26 Oct, 2014 00:59am", loc)
testz = testz.Add(time.Hour)

我预计这样会显示:

2014-10-26 01:59:00 +0400 MSK 2014-10-25 21:59:00 +0000 UTC
2014-10-26 01:00:00 +0300 MSK 2014-10-25 22:00:00 +0000 UTC
2014-10-26 01:01:00 +0300 MSK 2014-10-25 22:01:00 +0000 UTC

(我没有尝试过,因为我没有安装Go,但我预计它会起作用。)

英文:

Given your output, it looks like this line:

testz , _ := time.ParseInLocation(timeFormat, "26 Oct, 2014 01:59am", loc)

... is actually giving you the later of the two occurrences of 1:59am, given that it's showing an offset of +3. In other words, you're looking at three minutes of time an hour later than you want to be - starting nearly an hour after the daylight saving transition.

You can change your code to start with an unambiguous value like this:

testz, _ := time.ParseInLocation(timeFormat, "26 Oct, 2014 00:59am", loc)
testz = testz.Add(time.Hour)

I'd expect that to then show

2014-10-26 01:59:00 +0400 MSK 2014-10-25 21:59:00 +0000 UTC
2014-10-26 01:00:00 +0300 MSK 2014-10-25 22:00:00 +0000 UTC
2014-10-26 01:01:00 +0300 MSK 2014-10-25 22:01:00 +0000 UTC

(I haven't tried this, as I don't have Go installed - but I'd expect it to work.)

答案2

得分: -2

这是因为你的代码没有错误,只是因为time.UTC()会打印出UTC时间,即时区为0的时间。从你的代码time.LoadLocation("Europe/Moscow")可以知道你可能在莫斯科,莫斯科属于东三区时区,所以时间会比UTC时间提前三个小时,也就是说**<你的时间>=<UTC时间>+3**。

英文:

It's not because your code has error,just for time.UTC() will print UTC time,that is timezone 0 time,and from your code time.LoadLocation(&quot;Europe/Moscow&quot;) we can know maybe you are in Moscow,which belongs to east timezone 3,so the time will be earlier than utc time by three hours,meaning that &lt;your time>=&lt;utc time>+3.

huangapple
  • 本文由 发表于 2016年4月10日 17:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/36528017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定