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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/130074/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:32:25  来源:igfitidea点击:

Is there an inverse function for time.gmtime() that parses a UTC tuple to seconds since the epoch?

python

提问by Thomas Vander Stichele

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

python的时间模块似乎有点随意。例如,这里是文档字符串中的方法列表:

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 ?

额外的问题:你会给这个方法起什么名字?你将如何实施它?

回答by Tom

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

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

回答by Mark Roddy

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

我一直认为 time 和 datetime 模块有点不连贯。无论如何,这是 mktime 的倒数

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

回答by Chris Jester-Young

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 dispevery 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.gmtimesets 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.

time.gmtime太糟糕了,因为总是将 DST 标志设置为 false。不过,我讨厌代码。必须有更好的方法来做到这一点。可能还有一些我还没有得到的极端情况。

回答by Ilialuk

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。