[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.29,1.30

Tim Peters tim_one@users.sourceforge.net
Sun, 03 Mar 2002 16:53:03 -0800


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

Modified Files:
	datetime.py 
Log Message:
tmxxx.__init__():  Optimize for common cases.  This got more important
when __hash__ started using tmxxx.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** datetime.py	4 Mar 2002 00:14:54 -0000	1.29
--- datetime.py	4 Mar 2002 00:53:01 -0000	1.30
***************
*** 147,158 ****
                   microsecond=0):
          # Normalize all the inputs, and store the normalized values.
!         carry, microsecond = divmod(microsecond, 1000000)
!         second += carry
!         carry, second = divmod(second, 60)
!         minute += carry
!         carry, minute = divmod(minute, 60)
!         hour += carry
!         carry, hour = divmod(hour, 24)
!         day += carry
          # That was easy.  Now it gets muddy:  the proper range for day
          # can't be determined without knowing the correct month and year,
--- 147,163 ----
                   microsecond=0):
          # Normalize all the inputs, and store the normalized values.
!         if not 0 <= microsecond <= 999999:
!             carry, microsecond = divmod(microsecond, 1000000)
!             second += carry
!         if not 0 <= second <= 59:
!             carry, second = divmod(second, 60)
!             minute += carry
!         if not 0 <= minute <= 59:
!             carry, minute = divmod(minute, 60)
!             hour += carry
!         if not 0 <= hour <= 23:
!             carry, hour = divmod(hour, 24)
!             day += carry
! 
          # That was easy.  Now it gets muddy:  the proper range for day
          # can't be determined without knowing the correct month and year,
***************
*** 161,176 ****
          # themselves).
          # Saying 12 months == 1 year should be non-controversial.
!         carry, month = divmod(month-1, 12)
!         year += carry
!         month += 1
!         assert 1 <= month <= 12
!         # Now only day can be out of bounds.  If it is, what to do is arguable,
!         # but at least the method here is principled and explainable.
!         if not 1 <= day <= _days_in_month(month, year):
!             # Note that the "if" test is for efficiency, not correctness:
!             # there's simply no need to do this dance if day is already in
!             # range, and it's an expensive dance.
!             self.ordinal = _ymd2ord(year, month, 1) + (day - 1)
!             year, month, day = _ord2ymd(self.ordinal)
          self.year, self.month, self.day = year, month, day
          self.hour, self.minute, self.second = hour, minute, second
--- 166,200 ----
          # themselves).
          # Saying 12 months == 1 year should be non-controversial.
!         if not 1 <= month <= 12:
!             carry, month = divmod(month-1, 12)
!             year += carry
!             month += 1
!             assert 1 <= month <= 12
! 
!         # Now only day can be out of bounds (year may also be out of bounds
!         # for a datetime object, but we don't care about that here).
!         # If day is out of bounds, what to do is arguable, but at least the
!         # method here is principled and explainable.
!         dim = _days_in_month(month, year)
!         if not 1 <= day <= dim:
!             # Move day-1 days from the first of the month.  First try to
!             # get off cheap if we're only one day out of range (adjustments
!             # for timezone alone can't be worse than that).
!             if day == 0:    # move back a day
!                 month -= 1
!                 if month > 0:
!                     day = _days_in_month(month, year)
!                 else:
!                     year, month, day = year-1, 12, 31
!             elif day == dim + 1:    # move forward a day
!                 month += 1
!                 day = 1
!                 if month > 12:
!                     month = 1
!                     year += 1
!             else:
!                 self.ordinal = _ymd2ord(year, month, 1) + (day - 1)
!                 year, month, day = _ord2ymd(self.ordinal)
! 
          self.year, self.month, self.day = year, month, day
          self.hour, self.minute, self.second = hour, minute, second