简体   繁体   English

Python日期转换为EPOCH

[英]Python Date to EPOCH conversion

import time
print(int(time.mktime(time.strptime('2017-08-12T17:07:46', '%Y-%m-%dT%H:%M:%S'))))

I get 1502582866 and I expect 1502557666? 我得到1502582866,我希望得到1502557666? Any help is welcome 欢迎任何帮助

Yes, @Paul is correct: according to the documentation, mktime will take a struct_time in local time and convert it to seconds since the epoch , which has no timezones nor daylight savings. 是的,@ Paul是正确的:根据文档, mktime将采用本地时间struct_time并将其转换为自epoch以来的秒数,该纪元 struct_time区也没有夏时制。

So, your initial time of: 因此,您的初始时间:

`2017-08-12T17:07:46`

becomes: 变为:

1502582866 ==> `GMT: Sunday, August 13, 2017 12:07:46 AM`

which is correct. 哪个是对的。

If you are wondering how to correctly convert back, you would use localtime : 如果您想知道如何正确地转换回去,可以使用localtime

import time
epoch_time = 1502582866
time_string = time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(epoch_time))
print time_string
>> 2017-08-12T17:07:46

Also see a sample in action here: https://eval.in/845075 另请参阅此处的示例: https : //eval.in/845075

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM