[英]Convert datetime object in a particular timezone to epoch seconds in that timezone
例如:
>>> print dt
2012-12-04 19:00:00-05:00
如您所見,我有這個日期時間對象如何將此日期時間對象轉換為GMT -5中的紀元秒。
我該怎么做呢?
你的日期時間不是一個天真的日期時間,它知道它所在的時區(你的打印狀態是-5)。 因此,在將其轉換為紀元之前,您只需將其設置為utc即可
>>> import time, pytz
>>> utc = pytz.timezone('UTC')
>>> utc_dt = utc.normalize(dt.astimezone(utc))
>>> time.mktime(utc_dt.timetuple())
1355270789.0 # This is just to show the format it outputs
如果dt對象是一個天真的日期時間對象,你需要使用時區來遵守夏令時,同時在GMT 0之間找到正確的小時數。例如,羅馬尼亞在冬天,它有+2,在夏天+3。
對於你的-5例子,紐約將做:
>>> import time,pytz
>>> timezone = pytz.timezone('America/New_York')
>>> local_dt = timezone.localize(dt)
現在你有一個非天真的日期時間,你可以像我第一次解釋的那樣得到紀元時間。 玩得開心
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.