26

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_isdst=-1)

time.struct_time(tm_year=2010, tm_mon=9, tm_mday=25, tm_hour=13, tm_min=7, tm_sec=25, tm_wday=5, tm_yday=268, tm_isdst=-1)

I want to have the difference of those two. How could I do that? I need minutes and seconds only, as well as the duration of those two.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user469652
  • 48,855
  • 59
  • 128
  • 165
  • 1
    Look at: http://stackoverflow.com/questions/1697815/how-do-you-convert-a-python-time-struct-time-object-into-a-datetime-object – shahjapan Oct 13 '10 at 04:55
  • timezone-aware answer: [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/a/26313848/4279) – jfs Feb 09 '15 at 14:24

4 Answers4

28

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
  • 5
    In python 3 the mktime function returns the number of seconds since the Epoch and therefore no need to divide by 60. – Adam Oren Apr 18 '13 at 15:47
  • local time may be ambiguous therefore `mktime()` may return a wrong result (50% chance during end-of-DST transitions, or just wrong for past dates if historical tz database is not used). It is incorrect to pass it UTC time (the result of `gmtime()`) unless the local timezone is UTC. – jfs Feb 09 '15 at 14:28
  • 1
    @AdamOren: `mktime()` *always* returns "seconds sicne the epoch". `/60` returns number of minutes as OP asks. I would use `minutes, seconds = divmod(seconds, 60)` to get minutes, seconds separately or use `timedelta` as [@jweyrich suggested](http://stackoverflow.com/a/3920862/4279). – jfs Feb 09 '15 at 14:31
17
>>> t1 = time.mktime(time.strptime("10 Oct 10", "%d %b %y"))
>>> t2 = time.mktime(time.strptime("15 Oct 10", "%d %b %y"))
>>> print(datetime.timedelta(seconds=t2-t1))
5 days, 0:00:00
jweyrich
  • 31,198
  • 5
  • 66
  • 97
  • Doesn't work for Python 3.6: `errorMessage: "unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time` – Suncatcher Aug 17 '17 at 14:07
  • @Suncatcher huh? It works fine with python3 - see http://imgur.com/a/3rbhW – jweyrich Aug 17 '17 at 14:15
  • 1
    Yeah, mea culpa. I passed the wrong arguments. BTW, is there simpler way directly with `time.struct_time` type? Without converting to `mktime` – Suncatcher Aug 17 '17 at 14:17
  • @J.F.Sebastian your point being that it would return a wrong result when you provide 2 timestamps that comprehend a transition from DST to non-DST or vice-versa. Agreed. – jweyrich Aug 17 '17 at 14:20
  • I don't understand the phrase "comprehend a transition". Click the link in my comment above. It describes explicitly when mktime fails. – jfs Aug 17 '17 at 14:23
  • Let's say your TZ is currently in DST, and at midnight it will transition to non-DST. Assume you're working with timestamp A from today, and timestamp B from tomorrow - `mktime` may not be aware of the DST transition, and would therefore return a time struct that is off by 1 hour, which will propagate the error to the `B - A` subtraction as well - The subtraction result will be off-by-1 hour. Is that it or I didn't understand your point (from your answer)? – jweyrich Aug 17 '17 at 14:29
3

There is another way to find the time difference between any two dates (no better than the previous solution, I guess):

 >>> import datetime
 >>> dt1 = datetime.datetime.strptime("10 Oct 10", "%d %b %y")
 >>> dt2 = datetime.datetime.strptime("15 Oct 10", "%d %b %y")
 >>> (dt2 - dt1).days
 5
 >>> (dt2 - dt1).seconds
 0
 >>>

It will give the difference in days or seconds or combination of that. The type for (dt2 - dt1) is datetime.timedelta. Look in the library for further details.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Somum
  • 2,382
  • 26
  • 15
3

You can use time.mktime(t) with the struct_time object (passed as "t") to convert it to a "seconds since epoch" floating point value. Then you can subtract those to get difference in seconds, and divide by 60 to get difference in minutes.

DerfK
  • 273
  • 2
  • 14