[Zope3-dev] datetime objects

Guido van Rossum guido@python.org
Wed, 11 Dec 2002 10:59:10 -0500


> I'm using python-2.2.2/linux

With the version of datetime currently in Zope3 CVS, I presume.

> 1. is this a bug ??
> ----------------------------------
> >>> from datetime import datetime
> >>> def test_datetime():
> ...   d = datetime.now()
> ...   for i in range(1000):
> ...     xd = datetime.now()
> ...     print xd - d
> ...     d = xd
> ...
> >>> test_datetime()
> 
> sometimes prints (reproducable, but not
> on any linux-machine):
> 
> 0:00:00.000764
> 0:00:00.000762
> -1 days, 23:59:59.995242
> 0:00:00.000872
> 0:00:00.000778
> --------------------------------

Hmm...

This seems to mean that the time skipped backwards a fraction of a
millisecond.  If you repeatedly call time.time() and compare it with a
previous value, do you ever see it skip backwards?  If so, then the
problem is in time.time(); if not, we need to look for a problem in
either time.localtime() or the code in datetime.fromtimestamp() and
datetime.__init__().

> 2. another bug ??
> >>> from datetime import datetime
> >>> d1 = datetime.now()
> >>> d2 = datetime.now()
> >>> d1 - d2
> timedelta(-1, 86395, 113112)
> >>> abs(d1 - d2)
> timedelta(0, 4, 886888)
> >>> d1
> datetime(2002, 12, 11, 16, 21, 3, 920367)
> >>> d2
> datetime(2002, 12, 11, 16, 21, 8, 807255)
> >>> d1 - d2
> timedelta(-1, 86395, 113112)
> 
> the latter should be a negative timedelta of about 5 seconds !!!

Negative timedeltas are always expressed as a negative number of days
and a positive number of seconds and microseconds:

  >>> timedelta(seconds=5)
  timedelta(0, 5)
  >>> timedelta(seconds=-5)
  timedelta(-1, 86395)
  >>> 

> i've looked at the python-cvs sandbox and there is an implementation
> of datetime that is under development. is this implementation
> supposed to replace the current datetime module ??

Yes.

> what needs to be done to make the current implementation working ??

Depends on what you call working. :-)

--Guido van Rossum (home page: http://www.python.org/~guido/)