[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.39,1.40 test_datetime.py,1.24,1.25

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 04 Mar 2002 07:34:31 -0800


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

Modified Files:
	datetime.py test_datetime.py 
Log Message:
Disallow adding or sutracting plain numbers; there's no agreement whether
the number should be interpreted as days or seconds.

Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.39
retrieving revision 1.40
diff -C2 -d -r1.39 -r1.40
*** datetime.py	4 Mar 2002 10:17:41 -0000	1.39
--- datetime.py	4 Mar 2002 15:34:29 -0000	1.40
***************
*** 352,381 ****
                               self.__seconds + other.__seconds,
                               self.__microseconds + other.__microseconds)
!         if isinstance(other, (int, long)):
!             return timedelta(self.__days,
!                              self.__seconds + other,
!                              self.__microseconds)
!         if isinstance(other, float):
!             sign = 1
!             if other < 0:
!                 sign, other = -1, -other
!             frac, whole = _math.modf(other)
!             us = int(frac * 1e6 + 0.5)
!             assert 0 <= us <= 1000000   # if 1e6, constructor will normalize
!             days, seconds = divmod(whole, 24*3600)
!             return timedelta(self.__days + sign * int(days),
!                              self.__seconds + sign * int(seconds),
!                              self.__microseconds + sign * us)
!         return NotImplemented
  
      __radd__ = __add__
  
      def __sub__(self, other):
!         if isinstance(other, (timedelta, int, long, float)):
              return self + -other
          return NotImplemented
  
      def __rsub__(self, other):
!         if isinstance(other, (timedelta, int, long, float)):
              return -self + other
          return NotImplemented
--- 352,367 ----
                               self.__seconds + other.__seconds,
                               self.__microseconds + other.__microseconds)
!         raise TypeError
!         # XXX Should be 'return NotImplemented', but there's a bug in 2.2...
  
      __radd__ = __add__
  
      def __sub__(self, other):
!         if isinstance(other, timedelta):
              return self + -other
          return NotImplemented
  
      def __rsub__(self, other):
!         if isinstance(other, timedelta):
              return -self + other
          return NotImplemented
***************
*** 437,441 ****
      __repr__, __str__
      __cmp__, __hash__
!     __add__, __radd__, __sub__ (add/radd only with timedelta or numeric arg)
  
      Methods:
--- 423,427 ----
      __repr__, __str__
      __cmp__, __hash__
!     __add__, __radd__, __sub__ (add/radd only with timedelta arg)
  
      Methods:
***************
*** 670,677 ****
  
      def __add__(self, other):
!         """Add a datetime to a timedelta.
! 
!         An int/long/float argument is also allowed, interpreted as seconds.
!         """
          if isinstance(other, timedelta):
              t = tmxxx(self.__year,
--- 656,660 ----
  
      def __add__(self, other):
!         "Add a datetime to a timedelta."
          if isinstance(other, timedelta):
              t = tmxxx(self.__year,
***************
*** 686,694 ****
                                    t.microsecond, self.__tzoffset)
              return result
!         elif isinstance(other, (int, long)):
!             return self + timedelta(0, other)
!         elif isinstance(other, float):
!             return self + (timedelta(0) + other)
!         return NotImplemented
  
      __radd__ = __add__
--- 669,674 ----
                                    t.microsecond, self.__tzoffset)
              return result
!         raise TypeError
!         # XXX Should be 'return NotImplemented', but there's a bug in 2.2...
  
      __radd__ = __add__
***************
*** 699,703 ****
          An int/long/float argument is also allowed, interpreted as seconds.
          """
!         if isinstance(other, (timedelta, int, long, float)):
              return self + -other
          if isinstance(other, datetime):
--- 679,683 ----
          An int/long/float argument is also allowed, interpreted as seconds.
          """
!         if isinstance(other, timedelta):
              return self + -other
          if isinstance(other, datetime):

Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_datetime.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** test_datetime.py	4 Mar 2002 06:10:15 -0000	1.24
--- test_datetime.py	4 Mar 2002 15:34:29 -0000	1.25
***************
*** 192,216 ****
          self.assertEqual(a//10, timedelta(0, 7*24*360))
          self.assertEqual(a//3600000, timedelta(0, 0, 7*24*1000))
!         # Add/sub ints, longs, floats
!         self.assertEqual(a + 7*24*3600, timedelta(14))
!         self.assertEqual(a - 24*3600, timedelta(6))
!         self.assertEqual(a + 100, timedelta(7, 100))
!         self.assertEqual(a - 100, timedelta(6, 24*3600-100))
!         self.assertEqual(100 + a, a + 100)
!         self.assertEqual(100 - a, 100 + -a)
!         self.assertEqual(a + 7*24*3600L, timedelta(14))
!         self.assertEqual(a - 24*3600L, timedelta(6))
!         self.assertEqual(a + 100L, timedelta(7, 100))
!         self.assertEqual(a - 100L, timedelta(6, 24*3600-100))
!         self.assertEqual(100L + a, a + 100L)
!         self.assertEqual(100L - a, 100L + -a)
!         self.assertEqual(c + 0.001, timedelta(0, 0, 2000))
!         self.assertEqual(c - 0.0001, timedelta(0, 0, 900))
!         self.assertEqual(b + 1.1, timedelta(0, 61, 100000))
!         self.assertEqual(b - 1.1, timedelta(0, 58, 900000))
!         self.assertEqual(a + (1e6 + 1e-6), timedelta(7, 1000000, 1))
!         self.assertEqual(a - (1e6 + 1e-6), timedelta(7, -1000000, -1))
!         self.assertEqual(1.1 + a, a + 1.1)
!         self.assertEqual(1.1 - a, 1.1 + -a)
  
      def test_computations(self):
--- 192,201 ----
          self.assertEqual(a//10, timedelta(0, 7*24*360))
          self.assertEqual(a//3600000, timedelta(0, 0, 7*24*1000))
!         # Add/sub ints, longs, floats should be illegal
!         for i in 1, 1L, 1.0:
!             self.assertRaises(TypeError, lambda: a+i)
!             self.assertRaises(TypeError, lambda: a-i)
!             self.assertRaises(TypeError, lambda: i+a)
!             self.assertRaises(TypeError, lambda: i-a)
  
      def test_computations(self):
***************
*** 261,274 ****
          self.assertEqual(a - (week + day + hour + millisec),
                           (((a - week) - day) - hour) - millisec)
!         # Add/sub ints, longs, floats
!         self.assertEqual(a + 12, a + timedelta(0, 12))
!         self.assertEqual(a - 12, a - timedelta(0, 12))
!         self.assertEqual(100 + a, a + 100)
!         self.assertEqual(a + 12L, a + timedelta(0, 12))
!         self.assertEqual(a - 12L, a - timedelta(0, 12))
!         self.assertEqual(100L + a, a + 100L)
!         self.assertEqual(a + 0.001, a + timedelta(0, 0, 1000))
!         self.assertEqual(a - 0.001, a - timedelta(0, 0, 1000))
!         self.assertEqual(0.1 + a, a + 0.1)
  
      def test_weekday(self):
--- 246,255 ----
          self.assertEqual(a - (week + day + hour + millisec),
                           (((a - week) - day) - hour) - millisec)
!         # Add/sub ints, longs, floats should be illegal
!         for i in 1, 1L, 1.0:
!             self.assertRaises(TypeError, lambda: a+i)
!             self.assertRaises(TypeError, lambda: a-i)
!             self.assertRaises(TypeError, lambda: i+a)
!             self.assertRaises(TypeError, lambda: i-a)
  
      def test_weekday(self):