问题:如何从gmtime()的时间+日期输出获取自纪元以来的秒数?

你如何做相反gmtime(),你把时间+日期,并获得秒数?

我有类似的字符串'Jul 9, 2009 @ 20:02:58 UTC',并且我想获取从纪元到2009年7月9日之间的秒数。

我已经尝试过,time.strftime但是我不知道如何正确使用它,或者它是否是正确的命令。

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.


回答 0

如果您是由于搜索引擎告诉您这是获取Unix时间戳的方法而到达此处的,请停止阅读此答案。向下滚动一个。

如果想扭转time.gmtime(),那就想calendar.timegm()

>>> calendar.timegm(time.gmtime())
1293581619.0

您可以使用将字符串转换为时间元组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

有关日历模块的更多信息,请点击此处

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.

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


回答 1

使用时间模块:

epoch_time = int(time.time())

Use the time module:

epoch_time = int(time.time())

回答 2

请注意,time.gmtime将时间戳映射01970-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)) 给您一个时间戳,偏移的时间取决于您的语言环境,通常可能不为0。

In [64]: time.mktime(time.gmtime(0))
Out[64]: 18000.0

与之相反的time.gmtimecalendar.timegm

In [62]: import calendar    
In [65]: calendar.timegm(time.gmtime(0))
Out[65]: 0

Note that time.gmtime maps timestamp 0 to 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.

In [64]: time.mktime(time.gmtime(0))
Out[64]: 18000.0

The inverse of time.gmtime is calendar.timegm:

In [62]: import calendar    
In [65]: calendar.timegm(time.gmtime(0))
Out[65]: 0

回答 3

ep = datetime.datetime(1970,1,1,0,0,0)
x = (datetime.datetime.utcnow()- ep).total_seconds()

这应该与有所不同int(time.time()),但是可以安全地使用类似x % (60*60*24)

datetime-基本日期和时间类型:

与时间模块不同,日期时间模块不支持leap秒。

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)

datetime — Basic date and time types:

Unlike the time module, the datetime module does not support leap seconds.


回答 4

t = datetime.strptime('Jul 9, 2009 @ 20:02:58 UTC',"%b %d, %Y @ %H:%M:%S %Z")
t = datetime.strptime('Jul 9, 2009 @ 20:02:58 UTC',"%b %d, %Y @ %H:%M:%S %Z")

回答 5

有两种方法,具体取决于您的原始时间戳记:

mktime()timegm()

http://docs.python.org/library/time.html

There are two ways, depending on your original timestamp:

mktime() and timegm()

http://docs.python.org/library/time.html


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。