如何从 gmtime() 的时间 + 日期输出中获取自纪元以来的秒数? [英] How to get the seconds since epoch from the time + date output of gmtime()?
问题描述
你如何反向gmtime()
,你把时间+日期放在那里并得到秒数?
我有像 'Jul 9, 2009 @ 20:02:58 UTC'
这样的字符串,我想找回从纪元到 2009 年 7 月 9 日之间的秒数.
我已经尝试过 time.strftime
但我不知道如何正确使用它,或者它是否是正确的使用命令.
如果您来到这里是因为搜索引擎告诉您这是获取 Unix 时间戳的方法,请停止阅读此答案.向上滚动一个.
如果你想反转time.gmtime()
,你需要calendar.timegm()
.
您可以使用 time.strptime()
将您的字符串转换为时间元组,它返回一个您可以传递给 calendar.timegm()
的时间元组:
有关日历模块的更多信息此处
How do you do reverse gmtime()
, where you put the time + date and get the number of seconds?
I have strings like 'Jul 9, 2009 @ 20:02:58 UTC'
, and I want to get back the number of seconds between the epoch and July 9, 2009.
I have tried time.strftime
but I don't know how to use it properly, or if it is the correct command to use.
If you got here because a search engine told you this is how to get the Unix timestamp, stop reading this answer. Scroll up one.
If you want to reverse time.gmtime()
, you want calendar.timegm()
.
>>> calendar.timegm(time.gmtime())
1293581619.0
You can turn your string into a time tuple with time.strptime()
, which returns a time tuple that you can pass to calendar.timegm()
:
>>> import calendar
>>> import time
>>> calendar.timegm(time.strptime('Jul 9, 2009 @ 20:02:58 UTC', '%b %d, %Y @ %H:%M:%S UTC'))
1247169778
More information about calendar module here
这篇关于如何从 gmtime() 的时间 + 日期输出中获取自纪元以来的秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!