Python 如何从 gmtime() 的时间 + 日期输出中获取自纪元以来的秒数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4548684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the seconds since epoch from the time + date output of gmtime()?
提问by calccrypto
How do you do reverse gmtime(), where you put the time + date and get the number of seconds?
你如何做 reverse gmtime(),你把时间+日期放在那里并获得秒数?
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.
我有类似 的字符串'Jul 9, 2009 @ 20:02:58 UTC',我想取回纪元和 2009 年 7 月 9 日之间的秒数。
I have tried time.strftimebut I don't know how to use it properly, or if it is the correct command to use.
我试过了,time.strftime但我不知道如何正确使用它,或者它是否是正确的使用命令。
采纳答案by nmichaels
If you got here because a search engine told you this is how to get the Unix timestamp, stop reading this answer. Scroll down one.
如果您来到这里是因为搜索引擎告诉您这是获取 Unix 时间戳的方法,请停止阅读此答案。向下滚动一个。
If you want to reverse time.gmtime(), you want calendar.timegm().
如果你想逆转time.gmtime(),你想要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():
您可以使用 将字符串转换为时间元组time.strptime(),它会返回一个您可以传递给的时间元组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
关于日历模块的更多信息在这里
回答by Falmarri
t = datetime.strptime('Jul 9, 2009 @ 20:02:58 UTC',"%b %d, %Y @ %H:%M:%S %Z")
回答by Evan Mulawski
There are two ways, depending on your original timestamp:
有两种方法,具体取决于您的原始时间戳:
mktime()and timegm()
mktime()和 timegm()
回答by unutbu
Note that time.gmtimemaps timestamp 0to 1970-1-1 00:00:00.
请注意,time.gmtime将时间戳映射0到1970-1-1 00:00:00.
In [61]: import time
In [63]: time.gmtime(0)
Out[63]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
time.mktime(time.gmtime(0))gives you a timestamp shifted by an amount that depends on your locale, which in general may not be 0.
time.mktime(time.gmtime(0))给你一个时间戳,偏移量取决于你的语言环境,通常可能不是 0。
In [64]: time.mktime(time.gmtime(0))
Out[64]: 18000.0
The inverse of time.gmtimeis calendar.timegm:
的倒数time.gmtime是calendar.timegm:
In [62]: import calendar
In [65]: calendar.timegm(time.gmtime(0))
Out[65]: 0
回答by user3226167
ep = datetime.datetime(1970,1,1,0,0,0)
x = (datetime.datetime.utcnow()- ep).total_seconds()
This should be different from int(time.time()), but it is safe to use something like x % (60*60*24)
这应该与 不同int(time.time()),但使用类似的东西是安全的x % (60*60*24)
datetime — Basic date and time types:
Unlike the time module, the datetime module does not support leap seconds.
与 time 模块不同,datetime 模块不支持闰秒。

