[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.41,1.42

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 04 Mar 2002 10:56:12 -0800


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

Modified Files:
	datetime.py 
Log Message:
Changes to timedelta() constructor:
- allow floats
- add keyword arguments milliseconds, minutes, hours, and weeks

Also remove an outdated item from a comment


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** datetime.py	4 Mar 2002 18:49:07 -0000	1.41
--- datetime.py	4 Mar 2002 18:56:10 -0000	1.42
***************
*** 290,297 ****
  
      - add, subtract timedelta
-     - add, subtract int/long/float seconds
      - unary plus, minus, abs
      - compare to timedelta
!     - multiply, divide by int/long
  
      In addition, datetime supports subtraction of two datetime objects
--- 290,296 ----
  
      - add, subtract timedelta
      - unary plus, minus, abs
      - compare to timedelta
!     - multiply, divide by int/long/float
  
      In addition, datetime supports subtraction of two datetime objects
***************
*** 303,307 ****
      """
  
!     def __init__(self, days=0, seconds=0, microseconds=0):
          s, us = divmod(microseconds, 1000000)
          assert us >= 0
--- 302,323 ----
      """
  
!     def __init__(self, days=0, seconds=0, microseconds=0,
!                  # 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
***************
*** 310,320 ****
          d += days
          # d may be < 0
!         self.__days = d
!         self.__seconds = s
!         self.__microseconds = us
  
      def __repr__(self):
          if self.__microseconds:
!             return "timedelta(%d, %d, %d)" % (self.__days,
                                                self.__seconds,
                                                self.__microseconds)
--- 326,336 ----
          d += days
          # d may be < 0
!         self.__days = int(d)
!         self.__seconds = int(s)
!         self.__microseconds = int(round(us))
  
      def __repr__(self):
          if self.__microseconds:
!             return "timedelta(days=%d, %d, %d)" % (self.__days,
                                                self.__seconds,
                                                self.__microseconds)