Tomáš Šíma Tomáš Šíma - 1 year ago 53
Python Question

Time difference in python not working for some dates

I have this function to count difference between two timestamps in seconds.

def diffdates(d1, d2):
diff = (time.mktime(time.strptime(d2,"%Y-%m-%dT%H:%M:%S")) -
time.mktime(time.strptime(d1, "%Y-%m-%dT%H:%M:%S")))
return diff


When I call it with given parameters, I expect the result to be

4*3600-10*60 = 13800

but I get

17400 = 5*3600 - 10*60

I was checking, if I have some problem with the formatting strings in documentation, but I can not find any. Also there is no changes in time like summer/winter time in that date.

Answer Source

Your math is wrong. The delta between 16:00 and 11:10 is 4 hours 50 minutes. Your computation of 4*3600-10*60 is suggesting 3 hours 50 minutes. You're off by an hour.

16:00 is 60*16 minutes into the day: 960

11:10 is 60*11+10 minutes into the day 670

960 - 670 is 290 minutes

290 minutes * 60 is 17400 seconds.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download