Python time differences

后端 未结 4 2092
忘掉有多难
忘掉有多难 2020-12-17 09:07

I have two time objects.

Example

time.struct_time(tm_year=2010, tm_mon=9, tm_mday=24, tm_hour=19, tm_min=13, tm_sec=37, tm_wday=4, tm_yday=267, tm_is         


        
4条回答
  •  离开以前
    2020-12-17 10:04

    Time instances do not support the subtraction operation. Given that one way to solve this would be to convert the time to seconds since epoch and then find the difference, use:

    >>> t1 = time.localtime()
    >>> t1
    time.struct_time(tm_year=2010, tm_mon=10, tm_mday=13, tm_hour=10, tm_min=12, tm_sec=27, tm_wday=2, tm_yday=286, tm_isdst=0)
    >>> t2 = time.gmtime()
    >>> t2
    time.struct_time(tm_year=2010, tm_mon=10, tm_mday=13, tm_hour=4, tm_min=42, tm_sec=37, tm_wday=2, tm_yday=286, tm_isdst=0)
    
    >>> (time.mktime(t1) - time.mktime(t2)) / 60
    329.83333333333331
    

提交回复
热议问题