使用strftime將python datetime轉換爲紀元 - Convert python datetime to epoch with strftime

問題:

I have a time in UTC from which I want the number of seconds since epoch. 我在UTC有一個時間,我希望從該時間起可以經過的秒數。

I am using strftime to convert it to the number of seconds. 我正在使用strftime將其轉換爲秒數。 Taking 1st April 2012 as an example. 以2012年4月1日爲例。

>>>datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'

1st of April 2012 UTC from epoch is 1333238400 but this above returns 1333234800 which is different by 1 hour. 從紀元開始的UTC時間爲2012年4月1日,但上述時間返回1333234800,相差1小時。

So it looks like that strftime is taking my system time into account and applies a timezone shift somewhere. 因此,看來strftime正在考慮我的系統時間,並在某處應用了時區偏移。 I thought datetime was purely naive? 我以爲約會時間純粹是天真的?

How can I get around that? 我該如何解決? If possible avoiding to import other libraries unless standard. 如果可能,除非標準,否則避免導入其他庫。 (I have portability concerns). (我有可移植性問題)。


解決方案:

參考一: https://en.stackoom.com/question/nGtX
參考二: https://stackoom.com/question/nGtX
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章