简体   繁体   English

将时区缩写的非UTC时间字符串转换为python中的UTC时间,同时考虑到夏时制

[英]Convert non-UTC time string with timezone abbreviation into UTC time in python, while accounting for daylight savings

I am having a hard time converting a string representation of non-UTC times to UTC due to the timezone abbreviation. 由于时区的缩写,我很难将非UTC时间的字符串表示形式转换为UTC。

(update: it seems that the timezone abbreviations may not be unique . if so, perhaps i should also be trying to take this into account. ) (更新:似乎时区缩写可能不是唯一的 。如果是这样,也许我也应该尝试考虑这一点。

I've been trying to look for a way around this using datetutil and pytz, but haven't had any luck. 我一直在尝试使用datetutil和pytz来解决此问题,但是没有任何运气。

Suggestions or workaround would be appreciated. 建议或解决方法,将不胜感激。

string = "Jun 20, 4:00PM EDT" 

I'd like to convert that into UTC time, accounting for daylight savings when appropriate. 我想将其转换为UTC时间,并在适当时考虑夏令时。

UPDATE: Found some references that may help more experienced users answer the Q. 更新:找到了一些参考,可以帮助更有经验的用户回答问题。

Essentially, I would imagine part of the solution doing the reverse of this . 从本质上讲,我会想象的解决方案做相反的一部分这个

FINAL UPDATE (IMPORTANT) 最后更新(重要)

Taken from the dateutil docs examples . 取自dateutil文档示例

Some simple examples based on the date command, using the TZOFFSET dictionary to provide the BRST timezone offset. 一些基于date命令的简单示例,使用TZOFFSET词典提供BRST时区偏移量。

parse("Thu Sep 25 10:36:28 BRST 2003", tzinfos=TZOFFSETS) datetime.datetime(2003, 9, 25, 10, 36, 28, tzinfo=tzoffset('BRST', -10800)) parse(“星期四2003年9月25日10:36:28”,tzinfos = TZOFFSETS)datetime.datetime(2003,9,25,10,36,28,tzinfo = tzoffset('BRST',-10800))

parse("2003 10:36:28 BRST 25 Sep Thu", tzinfos=TZOFFSETS) datetime.datetime(2003, 9, 25, 10, 36, 28, tzinfo=tzoffset('BRST', -10800)) parse(“ 2003 10:36:28 BRST Sep Sep”,tzinfos = TZOFFSETS)datetime.datetime(2003,9,25,10,36,28,tzinfo = tzoffset('BRST',-10800))

Combine this with a library such as found here. 将其与此处找到的库相结合 and you will have a solution to this problem. 您将可以解决此问题。

Using Nas Banov's excellent dictionary mapping timezone abbreviations to UTC offset: 使用Nas Banov出色的词典将时区缩写映射为UTC偏移量:

import dateutil
import pytz

# timezone dictionary built here: https://stackoverflow.com/a/4766400/366335
# tzd = {...}

string = 'Jun 20, 4:00PM EDT'
date = dateutil.parser.parse(string, tzinfos=tzd).astimezone(pytz.utc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM