【发布时间】:2015-02-16 08:42:50
【问题描述】:
这只是pytz 上的另一个帖子。
有两个函数可以在两个时区之间转换日期时间对象。第二个功能适用于所有情况。第一个函数在两种情况下失败,(3)和(4)。类似的SO post 没有这样的问题。任何基于localize(datetime.datetime) 和replace(tzinfo) 之间差异的解释都会有很大帮助。
>>> from dateutil.parser import parse
>>> import pytz
第一个功能(越野车)
下面的函数使用datetime.datetime.replace(tzinfo)。
def buggy_timezone_converter(input_dt, current_tz='UTC', target_tz='US/Eastern'):
'''input_dt is a datetime.datetime object'''
current_tz = pytz.timezone(current_tz)
target_tz = pytz.timezone(target_tz)
target_dt = input_dt.replace(tzinfo=current_tz).astimezone(target_tz)
return target_tz.normalize(target_dt)
现在注意四个日期时间转换。
(1) 从 UTC 到 EST -- OK
>>> buggy_timezone_converter(parse('2013-02-26T04:00:00'))
Out[608]: datetime.datetime(2013, 2, 25, 23, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
(2) 从 UTC 到 EDT -- OK
>>> buggy_timezone_converter(parse('2013-05-26T04:00:00'))
Out[609]: datetime.datetime(2013, 5, 26, 0, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
(3) 从 EST 到 UTC - 不正确。时间偏移为 4 小时 56 分钟。应该是5小时
>>> buggy_timezone_converter(parse('2013-02-26T04:00:00'), target_tz='UTC', current_tz='US/Eastern')
Out[610]: datetime.datetime(2013, 2, 26, 8, 56, tzinfo=<UTC>)
(4) 从 EDT 到 UTC——不正常。时间偏移为 4 小时 56 分钟。应该是4小时。不考虑夏令时。
>>> buggy_timezone_converter(parse('2013-05-26T04:00:00'), current_tz='US/Eastern', target_tz='UTC')
Out[611]: datetime.datetime(2013, 5, 26, 8, 56, tzinfo=<UTC>)
第二个功能(完美运行)
下面的函数使用pytz.timezone.localize(datetime.datetime)。效果很好
def good_timezone_converter(input_dt, current_tz='UTC', target_tz='US/Eastern'):
current_tz = pytz.timezone(current_tz)
target_tz = pytz.timezone(target_tz)
target_dt = current_tz.localize(input_dt).astimezone(target_tz)
return target_tz.normalize(target_dt)
(1) 从 UTC 到 EST -- OK
>>> good_timezone_converter(parse('2013-02-26T04:00:00'))
Out[618]: datetime.datetime(2013, 2, 25, 23, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
(2) 从 UTC 到 EDT -- OK
>>> good_timezone_converter(parse('2013-05-26T04:00:00'))
Out[619]: datetime.datetime(2013, 5, 26, 0, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
(3) 从 EST 到 UTC -- 好的。
>>> good_timezone_converter(parse('2013-02-26T04:00:00'), current_tz='US/Eastern', target_tz='UTC')
Out[621]: datetime.datetime(2013, 2, 26, 9, 0, tzinfo=<UTC>)
(4) 从 EDT 到 UTC -- 好的。
>>> good_timezone_converter(parse('2013-05-26T04:00:00'), current_tz='US/Eastern', target_tz='UTC')
Out[620]: datetime.datetime(2013, 5, 26, 8, 0, tzinfo=<UTC>)
【问题讨论】:
-
无法重现:
>>> timezone_converter(datetime.datetime(2013,02,26,4,0,0,0), target_tz='UTC', current_tz='US/Eastern')datetime.datetime(2013, 2, 26, 9, 0, tzinfo=<UTC>) -
抱歉,忘记添加导入行 >>> from dateutil.parser import parse >>> import pytz
-
您的问题与pytz localize vs datetime replace 重复,或者如果您对现有答案不满意,您应该提出更具体的问题 (update the current question)。请为第一个和第二个函数使用不同的名称,例如
convert_tz_replace()和convert_tz()。 -
@MarkRansom:再次检查(更新
pytz)。timezone_converter_replace(datetime(2013,2,26,4,0,0,0), target_tz='UTC', current_tz='US/Eastern')->datetime.datetime(2013, 2, 26, 8, 56, tzinfo=<UTC>)即.replace()失败,如记录。 -
@J.F.Sebastian 这是我测试的直接复制/粘贴。我不知道为什么我得到了不同的结果,但我做到了。也许这是某种线索。
标签: python python-2.7 datetime timezone pytz