[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.121,1.122 doc.txt,1.68,1.69 test_datetime.py,1.78,1.79

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Mon, 23 Dec 2002 15:49:55 -0800


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

Modified Files:
	datetime.py doc.txt test_datetime.py 
Log Message:
Implemented datetime.replace().  Refactored to reduce tedious repetition
of range-checking code.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.121
retrieving revision 1.122
diff -C2 -d -r1.121 -r1.122
*** datetime.py	23 Dec 2002 23:39:05 -0000	1.121
--- datetime.py	23 Dec 2002 23:49:51 -0000	1.122
***************
*** 256,259 ****
--- 256,278 ----
      raise ValueError("%s()=%d, must be in -1439..1439" % (name, offset))
  
+ def _check_date_fields(year, month, day):
+     if not MINYEAR <= year <= MAXYEAR:
+         raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR),
+                          year)
+     if not 1 <= month <= 12:
+         raise ValueError('month must be in 1..12', month)
+     dim = _days_in_month(year, month)
+     if not 1 <= day <= dim:
+         raise ValueError('day must be in 1..%d' % dim, day)
+ 
+ def _check_time_fields(hour, minute, second, microsecond):
+     if not 0 <= hour <= 23:
+         raise ValueError('hour must be in 0..23', hour)
+     if not 0 <= minute <= 59:
+         raise ValueError('minute must be in 0..59', minute)
+     if not 0 <= second <= 59:
+         raise ValueError('second must be in 0..59', second)
+     if not 0 <= microsecond <= 999999:
+         raise ValueError('microsecond must be in 0..999999', microsecond)
  
  # This is a start at a struct tm workalike.  Goals:
***************
*** 631,642 ****
          year, month, day (required, base 1)
          """
!         if not MINYEAR <= year <= MAXYEAR:
!             raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR),
!                              year)
!         if not 1 <= month <= 12:
!             raise ValueError('month must be in 1..12', month)
!         dim = _days_in_month(year, month)
!         if not 1 <= day <= dim:
!             raise ValueError('day must be in 1..%d' % dim, day)
          self.__year = year
          self.__month = month
--- 650,654 ----
          year, month, day (required, base 1)
          """
!         _check_date_fields(year, month, day)
          self.__year = year
          self.__month = month
***************
*** 730,741 ****
          if day is None:
              day = self.__day
!         if not MINYEAR <= year <= MAXYEAR:
!             raise ValueError('year must be in %d..%d' % (MINYEAR, MAXYEAR),
!                              year)
!         if not 1 <= month <= 12:
!             raise ValueError('month must be in 1..12', month)
!         dim = _days_in_month(year, month)
!         if not 1 <= day <= dim:
!             raise ValueError('day must be in 1..%d' % dim, day)
          return date(year, month, day)
  
--- 742,746 ----
          if day is None:
              day = self.__day
!         _check_date_fields(year, month, day)
          return date(year, month, day)
  
***************
*** 868,879 ****
          second, microsecond (default to zero)
          """
!         if not 0 <= hour <= 23:
!             raise ValueError('hour must be in 0..23', hour)
!         if not 0 <= minute <= 59:
!             raise ValueError('minute must be in 0..59', minute)
!         if not 0 <= second <= 59:
!             raise ValueError('second must be in 0..59', second)
!         if not 0 <= microsecond <= 999999:
!             raise ValueError('microsecond must be in 0..999999', microsecond)
          self.__hour = hour
          self.__minute = minute
--- 873,877 ----
          second, microsecond (default to zero)
          """
!         _check_time_fields(hour, minute, second, microsecond)
          self.__hour = hour
          self.__minute = minute
***************
*** 1232,1243 ****
          """
          super(datetime, self).__init__(year, month, day)
!         if not 0 <= hour <= 23:
!             raise ValueError('hour must be in 0..23', hour)
!         if not 0 <= minute <= 59:
!             raise ValueError('minute must be in 0..59', minute)
!         if not 0 <= second <= 59:
!             raise ValueError('second must be in 0..59', second)
!         if not 0 <= microsecond <= 999999:
!             raise ValueError('microsecond must be in 0..999999', microsecond)
          # XXX This duplicates __year, __month, __day for convenience :-(
          self.__year = year
--- 1230,1234 ----
          """
          super(datetime, self).__init__(year, month, day)
!         _check_time_fields(hour, minute, second, microsecond)
          # XXX This duplicates __year, __month, __day for convenience :-(
          self.__year = year
***************
*** 1292,1295 ****
--- 1283,1307 ----
                     time.hour, time.minute, time.second, time.microsecond)
      combine = classmethod(combine)
+ 
+     def replace(self, year=None, month=None, day=None, hour=None,
+                 minute=None, second=None, microsecond=None):
+         """Return a new datetime with new values for the specified fields."""
+         if year is None:
+             year = self.year
+         if month is None:
+             month = self.month
+         if day is None:
+             day = self.day
+         if hour is None:
+             hour = self.hour
+         if minute is None:
+             minute = self.minute
+         if second is None:
+             second = self.second
+         if microsecond is None:
+             microsecond = self.microsecond
+         _check_date_fields(year, month, day)
+         _check_time_fields(hour, minute, second, microsecond)
+         return datetime(year, month, day, hour, minute, second, microsecond)
  
      # Conversions to string.

Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.68
retrieving revision 1.69
diff -C2 -d -r1.68 -r1.69
*** doc.txt	23 Dec 2002 23:39:05 -0000	1.68
--- doc.txt	23 Dec 2002 23:49:52 -0000	1.69
***************
*** 618,621 ****
--- 618,626 ----
      Return time object with same hour, minute, second and microsecond.
  
+   - replace(year=None, month=None, day=None, hour=None, minute=None,
+             second=None, microsecond=None)
+     Return a datetime with the same fields as self, except for those
+     members given in the argument list.
+ 
    - timetuple()
      Return a 9-element tuple of the form returned by time.localtime().

Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_datetime.py,v
retrieving revision 1.78
retrieving revision 1.79
diff -C2 -d -r1.78 -r1.79
*** test_datetime.py	23 Dec 2002 23:39:05 -0000	1.78
--- test_datetime.py	23 Dec 2002 23:49:52 -0000	1.79
***************
*** 904,908 ****
  
          # Out of bounds.
!         base = date(2000, 2, 29)
          self.assertRaises(ValueError, base.replace, year=2001)
  
--- 904,908 ----
  
          # Out of bounds.
!         base = cls(2000, 2, 29)
          self.assertRaises(ValueError, base.replace, year=2001)
  
***************
*** 1282,1286 ****
  
      def test_replace(self):
!         return
  
  
--- 1282,1308 ----
  
      def test_replace(self):
!         cls = self.theclass
!         args = [1, 2, 3, 4, 5, 6, 7]
!         base = cls(*args)
!         self.assertEqual(base, base.replace())
! 
!         i = 0
!         for name, newval in (("year", 2),
!                              ("month", 3),
!                              ("day", 4),
!                              ("hour", 5),
!                              ("minute", 6),
!                              ("second", 7),
!                              ("microsecond", 8)):
!             newargs = args[:]
!             newargs[i] = newval
!             expected = cls(*newargs)
!             got = base.replace(**{name: newval})
!             self.assertEqual(expected, got)
!             i += 1
! 
!         # Out of bounds.
!         base = date(2000, 2, 29)
!         self.assertRaises(ValueError, base.replace, year=2001)