How do you do reverse gmtime()
, where you put the time + date and get the number of seconds?
如何进行反向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.
我有像“2009年7月9日@ 20:02:58 UTC”这样的字符串,我想要返回从2009年7月9日开始的秒数。
I have tried time.strftime
but I don't know how to use it properly, or if it is the correct command to use.
我尝试过时间。但是我不知道如何正确地使用它,或者它是否是正确的命令。
6 个解决方案
#1
100
You want calendar.timegm()
.
你想要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
更多有关日历模块的信息请参见这里
#3
9
Note that time.gmtime
maps timestamp 0
to 1970-1-1 00:00:00
.
注意时间。gmtime地图时间戳0到1970-1-1 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.
mktime(time.gmtime(0)))为您提供一个时间戳,该时间戳的偏移量取决于您的语言环境,通常可能不是0。
In [64]: time.mktime(time.gmtime(0))
Out[64]: 18000.0
The inverse of time.gmtime
is calendar.timegm
:
时间的倒数。gmtime calendar.timegm:
In [62]: import calendar
In [65]: calendar.timegm(time.gmtime(0))
Out[65]: 0
#4
3
t = datetime.strptime('Jul 9, 2009 @ 20:02:58 UTC',"%b %d, %Y @ %H:%M:%S %Z")
#5
2
There are two ways, depending on your original timestamp:
根据您的原始时间戳,有两种方法:
mktime()
and timegm()
mktime()和timegm()
http://docs.python.org/library/time.html
http://docs.python.org/library/time.html
#6
2
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模块不支持闰秒。
#1
100
You want calendar.timegm()
.
你想要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
更多有关日历模块的信息请参见这里
#2
#3
9
Note that time.gmtime
maps timestamp 0
to 1970-1-1 00:00:00
.
注意时间。gmtime地图时间戳0到1970-1-1 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.
mktime(time.gmtime(0)))为您提供一个时间戳,该时间戳的偏移量取决于您的语言环境,通常可能不是0。
In [64]: time.mktime(time.gmtime(0))
Out[64]: 18000.0
The inverse of time.gmtime
is calendar.timegm
:
时间的倒数。gmtime calendar.timegm:
In [62]: import calendar
In [65]: calendar.timegm(time.gmtime(0))
Out[65]: 0
#4
3
t = datetime.strptime('Jul 9, 2009 @ 20:02:58 UTC',"%b %d, %Y @ %H:%M:%S %Z")
#5
2
There are two ways, depending on your original timestamp:
根据您的原始时间戳,有两种方法:
mktime()
and timegm()
mktime()和timegm()
http://docs.python.org/library/time.html
http://docs.python.org/library/time.html
#6
2
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模块不支持闰秒。