是否有time.gmtime()的反函数将UTC元组解析为自纪元以来的秒数?

时间:2022-06-05 16:04:26

python's time module seems a little haphazard. For example, here is a list of methods in there, from the docstring:

python的时间模块似乎有点杂乱无章。例如,以下是docstring中的方法列表:

time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
tzset() -- change the local timezone

Looking at localtime() and its inverse mktime(), why is there no inverse for gmtime() ?

查看localtime()及其反mktime(),为什么gmtime()没有反转?

Bonus questions: what would you name the method ? How would you implement it ?

奖金问题:你怎么称呼这个方法?你会如何实现它?

4 个解决方案

#1


31  

There is actually an inverse function, but for some bizarre reason, it's in the calendar module: calendar.timegm(). I listed the functions in this answer.

实际上有一个反函数,但由于一些奇怪的原因,它在日历模块中:calendar.timegm()。我列出了这个答案中的功能。

#2


4  

I always thought the time and datetime modules were a little incoherent. Anyways, here's the inverse of mktime

我一直认为时间和日期时间模块有点不连贯。无论如何,这是mktime的反转

import time
def mkgmtime(t):
    """Convert UTC tuple to seconds since Epoch"""
    return time.mktime(t)-time.timezone

#3


0  

I'm only a newbie to Python, but here's my approach.

我只是Python的新手,但这是我的方法。

def mkgmtime(fields):
    now = int(time.time())
    gmt = list(time.gmtime(now))
    gmt[8] = time.localtime(now).tm_isdst
    disp = now - time.mktime(tuple(gmt))
    return disp + time.mktime(fields)

There, my proposed name for the function too. :-) It's important to recalculate disp every time, in case the daylight-savings value changes or the like. (The conversion back to tuple is required for Jython. CPython doesn't seem to require it.)

在那里,我提出了这个功能的名称。 :-)重要的是每次重新计算disp,以防日光节约值发生变化等。 (Jython需要转换回元组.CPython似乎不需要它。)

This is super ick, because time.gmtime sets the DST flag to false, always. I hate the code, though. There's got to be a better way to do it. And there are probably some corner cases that I haven't got, yet.

这是超级ick,因为time.gmtime总是将DST标志设置为false。但是,我讨厌这些代码。必须有一个更好的方法来做到这一点。而且可能还有一些我尚未得到的角落案例。

#4


0  

mktime documentation is a bit misleading here, there is no meaning saying it's calculated as a local time, rather it's calculating the seconds from Epoch according to the supplied tuple - regardless of your computer locality.

mktime文档在这里有点误导,没有任何意义说它计算为本地时间,而是根据提供的元组计算Epoch的秒数 - 无论您的计算机位置如何。

If you do want to do a conversion from a utc_tuple to local time you can do the following:

如果您确实想要从utc_tuple转换为本地时间,您可以执行以下操作:

>>> time.ctime(time.time())
'Fri Sep 13 12:40:08 2013'

>>> utc_tuple = time.gmtime()
>>> time.ctime(time.mktime(utc_tuple))
'Fri Sep 13 10:40:11 2013'

>>> time.ctime(time.mktime(utc_tuple) - time.timezone)
'Fri Sep 13 12:40:11 2013'


Perhaps a more accurate question would be how to convert a utc_tuple to a local_tuple. I would call it gm_tuple_to_local_tuple (I prefer long and descriptive names):

也许更准确的问题是如何将utc_tuple转换为local_tuple。我会称之为gm_tuple_to_local_tuple(我更喜欢长而具有描述性的名字):

>>> time.localtime(time.mktime(utc_tuple) - time.timezone)
time.struct_time(tm_year=2013, tm_mon=9, tm_mday=13, tm_hour=12, tm_min=40, tm_sec=11, tm_wday=4, tm_yday=256, tm_isdst=1)


Validatation:

>>> time.ctime(time.mktime(time.localtime(time.mktime(utc_tuple) - time.timezone)))
'Fri Sep 13 12:40:11 2013'    

Hope this helps, ilia.

希望这有帮助,ilia。

#1


31  

There is actually an inverse function, but for some bizarre reason, it's in the calendar module: calendar.timegm(). I listed the functions in this answer.

实际上有一个反函数,但由于一些奇怪的原因,它在日历模块中:calendar.timegm()。我列出了这个答案中的功能。

#2


4  

I always thought the time and datetime modules were a little incoherent. Anyways, here's the inverse of mktime

我一直认为时间和日期时间模块有点不连贯。无论如何,这是mktime的反转

import time
def mkgmtime(t):
    """Convert UTC tuple to seconds since Epoch"""
    return time.mktime(t)-time.timezone

#3


0  

I'm only a newbie to Python, but here's my approach.

我只是Python的新手,但这是我的方法。

def mkgmtime(fields):
    now = int(time.time())
    gmt = list(time.gmtime(now))
    gmt[8] = time.localtime(now).tm_isdst
    disp = now - time.mktime(tuple(gmt))
    return disp + time.mktime(fields)

There, my proposed name for the function too. :-) It's important to recalculate disp every time, in case the daylight-savings value changes or the like. (The conversion back to tuple is required for Jython. CPython doesn't seem to require it.)

在那里,我提出了这个功能的名称。 :-)重要的是每次重新计算disp,以防日光节约值发生变化等。 (Jython需要转换回元组.CPython似乎不需要它。)

This is super ick, because time.gmtime sets the DST flag to false, always. I hate the code, though. There's got to be a better way to do it. And there are probably some corner cases that I haven't got, yet.

这是超级ick,因为time.gmtime总是将DST标志设置为false。但是,我讨厌这些代码。必须有一个更好的方法来做到这一点。而且可能还有一些我尚未得到的角落案例。

#4


0  

mktime documentation is a bit misleading here, there is no meaning saying it's calculated as a local time, rather it's calculating the seconds from Epoch according to the supplied tuple - regardless of your computer locality.

mktime文档在这里有点误导,没有任何意义说它计算为本地时间,而是根据提供的元组计算Epoch的秒数 - 无论您的计算机位置如何。

If you do want to do a conversion from a utc_tuple to local time you can do the following:

如果您确实想要从utc_tuple转换为本地时间,您可以执行以下操作:

>>> time.ctime(time.time())
'Fri Sep 13 12:40:08 2013'

>>> utc_tuple = time.gmtime()
>>> time.ctime(time.mktime(utc_tuple))
'Fri Sep 13 10:40:11 2013'

>>> time.ctime(time.mktime(utc_tuple) - time.timezone)
'Fri Sep 13 12:40:11 2013'


Perhaps a more accurate question would be how to convert a utc_tuple to a local_tuple. I would call it gm_tuple_to_local_tuple (I prefer long and descriptive names):

也许更准确的问题是如何将utc_tuple转换为local_tuple。我会称之为gm_tuple_to_local_tuple(我更喜欢长而具有描述性的名字):

>>> time.localtime(time.mktime(utc_tuple) - time.timezone)
time.struct_time(tm_year=2013, tm_mon=9, tm_mday=13, tm_hour=12, tm_min=40, tm_sec=11, tm_wday=4, tm_yday=256, tm_isdst=1)


Validatation:

>>> time.ctime(time.mktime(time.localtime(time.mktime(utc_tuple) - time.timezone)))
'Fri Sep 13 12:40:11 2013'    

Hope this helps, ilia.

希望这有帮助,ilia。