[Tutor] timedelta, difference calculation

Timo timomlists at gmail.com
Tue Dec 13 12:10:46 CET 2011


Op 12-12-11 20:46, rail shafigulin schreef:
> i found something interesting during the timedate difference calculation
>
> import datetime
> import time
>
> def main():
>   mydatetime = datetime.datetime.now()
>   time.sleep(1)
>   mydatetime2 = datetime.datetime.now()
>   diff = mydatetime - mydatetime2
Lie Ryan explains how to read the output below, but you made a little 
error here.
Let's have a little example:
 >>> x1 = 1
 >>> x2 = x1 + 1
 >>> print(x1 - x2)
-1
 >>> print(x2 - x1)
1

So you should substract mydatetime from mydatetime2, not the other way 
around.

Cheers,
Timo

>
>   print(diff)
>
> if __name__ == '__main__':
>   main()
>
> if you run this code the result you get will be
> -1 day, 23:59:59
>
> at least that is what i'm getting.
>
> i was expecting to get -1 second. diff object is of type timedelta. 
> this kind of objects represent duration, at least that is what i 
> understood from the documentation 
> (http://docs.python.org/release/3.1.3/library/datetime.html#timedelta-objects). 
> so, is this a bug in the timedelta implementation or is it me not 
> understanding documentation properly?
>
> any help is appreciated.
>
> ps. i'm using python 3.1 on a windows xp machine.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list