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

后端 未结 4 1180
抹茶落季
抹茶落季 2020-12-14 17:33

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

time() -- return current time in second         


        
相关标签:
4条回答
  • 2020-12-14 17:44

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

    import time
    def mkgmtime(t):
        """Convert UTC tuple to seconds since Epoch"""
        return time.mktime(t)-time.timezone
    
    0 讨论(0)
  • 2020-12-14 17:51

    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.

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

    >>> 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):

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

    0 讨论(0)
  • 2020-12-14 17:55

    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.

    0 讨论(0)
  • 2020-12-14 18:01

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

    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.)

    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.

    0 讨论(0)
提交回复
热议问题