使用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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章