Tomáš Šíma - 5 months ago 10

Python Question

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

pprint(d2)

pprint(d1)

pprint(diff)

return diff

diffdates(diffdates('2016-10-11T11:10:00','2016-10-11T16:00:00')

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

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.