Is it possible to get a timezone in Python given a UTC timestamp and a UTC offset?

六眼飞鱼酱① 提交于 2019-12-10 18:39:44

问题


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?


回答1:


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.




回答2:


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.




回答3:


No. http://en.wikipedia.org/wiki/List_of_U.S._states_by_time_zone



来源:https://stackoverflow.com/questions/712322/is-it-possible-to-get-a-timezone-in-python-given-a-utc-timestamp-and-a-utc-offse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!