How to check whether current local time is DST?

前端 未结 3 1483
孤独总比滥情好
孤独总比滥情好 2021-01-04 12:29

In Ruby, for example, there\'s the Time#dst? function, which returns true in the case it is daylight saving time. Is there a Go standard library API call to do

3条回答
  •  感动是毒
    2021-01-04 12:42

    The Location api doesn't export the DST value of the timezone. This was brought up in the golang-nuts forum several years ago. One suggestion is to compare the January 1 timezone offset to the July 1 timezone offset. A working solution of this was posted using this method. One caveat is that goplay has the wrong local time, so it doesn't correctly report the information if you run it there. You can run it locally to verify that it does work.

    Another way would be to use reflection via the reflect package. A solution that I wrote to do this is available here. There are a lot of problems with this method.

    Edit: Really it should probably use cacheZone but does a linear search of the zones to find one that matches. This can lead to errors because some timezones share name and offset. The correct way would be to look at cacheZone and use that if it is set. Otherwise, you'll need to either look at zoneTrans or at least look at how lookup(int64) is implemented.

提交回复
热议问题