使用GMT偏移将时间转换为UTC

时间:2012-04-19 21:25:06

标签: python time utc

我有以下格式的日期时间和UTC偏移量。

  

22-01-2012 22:01:30 +0530

     

12-02-2012 13:00:34 -0400

如何使用Python中的pytz模块将其转换为UTC?

2 个答案:

答案 0 :(得分:4)

我会使用http://labix.org/python-dateutil#head-2f49784d6b27bae60cde1cff6a535663cf87497b中的dateutil.parser。确保为您的python版本使用正确的版本。

import datetime
import dateutil.parser
import pytz
loc_dt = dateutil.parser.parse('22-01-2012 22:01:30 +0530')
loc_dt.astimezone(pytz.utc)

答案 1 :(得分:1)

python3

>>> import time
>>> from datetime import datetime
>>> tm = '22-01-2012 22:01:30 +0530'
>>> fmt = '%d-%m-%Y %H:%M:%S %z'
>>> time.asctime(datetime.strptime(tm, fmt).utctimetuple())
'Sun Jan 22 16:31:30 2012'