pytz
允許您選擇UTC抵銷前/使用is_dst
參數DST轉換後:如果您確保時間戳嚴格增加,那麼你可以[使用`pytz`模塊解析這些]
>>> import pytz
>>> tz = pytz.timezone('Europe/Paris')
>>> from datetime import datetime
>>> naive = datetime.strptime('2014-03-30 02:00:00', '%Y-%m-%d %H:%M:%S')
>>> tz.localize(naive, is_dst=None)
Traceback (most recent call last)
...
NonExistentTimeError: 2014-03-30 02:00:00
>>> tz.localize(naive) #XXX WRONG
datetime.datetime(2014, 3, 30, 2, 0, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>)
>>> tz.normalize(tz.localize(naive)) # you want this (default is after the transition)
datetime.datetime(2014, 3, 30, 3, 0, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>)
>>> tz.localize(naive, is_dst=False) #XXX WRONG
datetime.datetime(2014, 3, 30, 2, 0, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>)
>>> tz.localize(naive, is_dst=True) #XXX WRONG
datetime.datetime(2014, 3, 30, 2, 0, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>)
>>> tz.normalize(tz.localize(naive, is_dst=False)) # time corresponding to the offset
datetime.datetime(2014, 3, 30, 3, 0, tzinfo=<DstTzInfo 'Europe/Paris' CEST+2:00:00 DST>)
>>> tz.normalize(tz.localize(naive, is_dst=True)) # time corresponding to the offset
datetime.datetime(2014, 3, 30, 1, 0, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>)
來源
2015-06-08 18:04:09
jfs
(HTTP ://stackoverflow.com/a/26221183/4279) – jfs
NonExistentTimeError表明,無論是你的輸入錯誤,或者你使用了錯誤的時區來解釋它。 – jfs
我一直在與時間的問題相當長的一段時間,我從來沒有聽說過「左手」與「右」或「左行」與「右旋」適用於時間戳。如果這是我剛纔沒有聽說過的已知事情,那麼請指出我的來源。無論哪種方式,請詳細解釋您的意思,以便其他人也可以學習。 –