[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.43,1.44

Tim Peters tim_one@users.sourceforge.net
Mon, 04 Mar 2002 11:51:35 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory usw-pr-cvs1:/tmp/cvs-serv19454

Modified Files:
	datetime.py 
Log Message:
timedelta.__init__():  Extensive rework for fp accuracy, and to ease the
overflow-checking burdens when recoded in C.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.43
retrieving revision 1.44
diff -C2 -d -r1.43 -r1.44
*** datetime.py	4 Mar 2002 19:39:38 -0000	1.43
--- datetime.py	4 Mar 2002 19:51:32 -0000	1.44
***************
*** 305,332 ****
                   # XXX The following should only be used as keyword args:
                   milliseconds=0, minutes=0, hours=0, weeks=0):
!         # Normalize everything to days, seconds, microseconds
!         days += weeks*7
!         seconds += minutes*60 + hours*3600
!         microseconds += milliseconds*1000
!         # Deal with floats
!         # XXX Tim may rewrite this for accuracy :-)
!         if isinstance(days, float):
!             days, fraction = divmod(days, 1.0)
!             if fraction:
!                 seconds += fraction*(24*3600)
!         if isinstance(seconds, float):
!             seconds, fraction = divmod(seconds, 1.0)
!             if fraction:
!                 microseconds += fraction*1e6
          # Propagate carry from us to s, from s to d
          s, us = divmod(microseconds, 1000000)
!         assert us >= 0
          d, s = divmod(s + seconds, 24*3600)
!         assert s >= 0
          d += days
          # d may be < 0
          self.__days = int(d)
          self.__seconds = int(s)
!         self.__microseconds = int(round(us))
  
      def __repr__(self):
--- 305,336 ----
                   # XXX The following should only be used as keyword args:
                   milliseconds=0, minutes=0, hours=0, weeks=0):
!         # Normalize everything to days, seconds, microseconds.
!         # Convert everything to float first, else overflow-checking in C
!         # is going to be a nightmare.
!         days += weeks*7.
!         seconds += minutes*60. + hours*3600.
!         microseconds += milliseconds*1000.
!         # Get rid of all fractions.
!         dayfrac, days = _math.modf(days)
!         daysecondsfrac, daysecondswhole = _math.modf(dayfrac * (24.*3600.))
!         secondsfrac, seconds = _math.modf(seconds)
!         seconds += daysecondswhole
!         secondsfrac += daysecondsfrac
!         microseconds += secondsfrac*1e6
!         microseconds = round(microseconds)
!         assert _math.modf(days)[0] == 0.0
!         assert _math.modf(seconds)[0] == 0.0
!         assert _math.modf(microseconds)[0] == 0.0
          # Propagate carry from us to s, from s to d
          s, us = divmod(microseconds, 1000000)
!         assert us == int(us) and 0 <= us < 1000000
          d, s = divmod(s + seconds, 24*3600)
!         assert s == int(s) and 0 <= s < 24*3600
          d += days
+         assert d == long(d)
          # d may be < 0
          self.__days = int(d)
          self.__seconds = int(s)
!         self.__microseconds = int(us)
  
      def __repr__(self):