给定 UTC 时间戳和 UTC 偏移量,是否可以在 Python 中获取时区?
我有 UTC 偏移量和 UTC 时间的数据。 鉴于此,Python是否可以获取用户的本地时区(主要是判断是否是夏令时等,可能使用pytz),类似于PHP timezone_name_from_abbr
中的函数?
例如:
如果我的纪元时间是1238720309, 我可以得到 UTC 时间为:
>>> d = datetime.utcfromtimestamp(1238720309)
>>> print d + dt.timedelta(0,-28800) #offset for pacific I think
2009-04-02 17:04:41.712143
这是正确的,除了现在是 PDT,所以应该是:
2009-04-02 18:04:41.712413
我认为我需要获取在 pytz 中使用的时区来确定它是否是夏令时?
I have data that is the UTC offset and the UTC time. Given that, is it possible in Python to get the user's local timezone (mainly to figure if it is DST etc. probably using pytz), similar to the function in PHP timezone_name_from_abbr
?
For example:
If my epoch time is 1238720309,
I can get the UTC time as:
>>> d = datetime.utcfromtimestamp(1238720309)
>>> print d + dt.timedelta(0,-28800) #offset for pacific I think
2009-04-02 17:04:41.712143
This is correct except it is PDT right now, so it should be:
2009-04-02 18:04:41.712413
I need to get the timezone to use in pytz to figure out if it is daylight saving, I think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于一般来说,给定时区偏移量有多个可能的时区,因此一般答案是“不,没有更多信息就不行”。 更多信息通常是时间适用的位置 - 哪个国家、州或城市。
Since, in general, there is more than one possible time zone for a given time zone offset, the general answer is "No, not without more information". The more information is typically the location to which the time applies - which country, or state, or city.
不。时区太复杂,而且有太多与 UTC 相差 X 小时的时区。
http://en.wikipedia.org/wiki/List_of_time_zones
例如,-5(来自 UTC)可能是加拿大、纽约、古巴、牙买加、厄瓜多尔等。
赤道地区可能不使用 DST,因为他们的一天大约一年长 12 小时。 南美洲的国家,如果他们使用某种形式的夏令时,可能会与北美国家的时间表相反,因为他们的夏季/冬季(即短日/长日)时间表也相反。
No. Time zones are too complicated and there are too many that are X hours from UTC.
http://en.wikipedia.org/wiki/List_of_time_zones
For example, -5 from UTC could be Canada, New York, Cuba, Jamaica, Ecuador, etc.
The equator zones probably don't use DST since their day is roughly 12 hours year long. The south american ones, if they use some form of DST are probably on the opposite schedule of the north american ones because their summer/winter (i.e. short days/long days) schedules are also opposite.
否。http://en.wikipedia.org/wiki/List_of_U.S._states_by_time_zone< /a>
No. http://en.wikipedia.org/wiki/List_of_U.S._states_by_time_zone