如何查看当前当地时间是否为夏令时? [英] How to check whether current local time is DST?

查看:350
本文介绍了如何查看当前当地时间是否为夏令时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在Ruby中,有 Time#dst?函数,在夏令时的情况下返回true.是否有Go标准库API调用可以做到这一点?

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 the same?

推荐答案

位置api不会导出时区的DST值.这是在 golang-nuts论坛中提出的.前.一种建议是将1月1日时区偏移量与7月1日时区偏移量进行比较.使用此方法发布了有效的解决方案.一个警告是,goplay的本地时间错误,因此,如果在本地运行它,它将无法正确报告信息.您可以在本地运行它以验证它是否可以工作.

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.

另一种方法是通过 reflect 包使用反射.我为此写的解决方案可在此处获得.这种方法有很多问题.

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.

确实应该使用 cacheZone ,但是对区域进行线性搜索以找到匹配的区域.这可能会导致错误,因为某些时区共享名称和偏移量.正确的方法是查看 cacheZone 并使用(如果已设置).否则,您需要查看 zoneTrans 或至少查看 lookup(int64)的实现方式.

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.

这篇关于如何查看当前当地时间是否为夏令时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆