[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.22,1.23 test_datetime.py,1.13,1.14

Guido van Rossum gvanrossum@users.sourceforge.net
Sun, 03 Mar 2002 11:58:43 -0800


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

Modified Files:
	datetime.py test_datetime.py 
Log Message:
- Add isoweekday(), which has Monday-Sunday == 1-7.
- Fix timedelta.__div__, and add testcases for it.


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** datetime.py	3 Mar 2002 06:11:54 -0000	1.22
--- datetime.py	3 Mar 2002 19:58:41 -0000	1.23
***************
*** 273,281 ****
      def __div__(self, other):
          if isinstance(other, (int, long)):
!             # XXX If, e.g., this is a delta of 1 day, dividing by 2 and
!             # XXX getting back 0 is pretty surprising.
!             return timedelta(self.__days // other,
!                              self.__seconds // other,
!                              self.__microseconds // other)
          return NotImplemented
  
--- 273,279 ----
      def __div__(self, other):
          if isinstance(other, (int, long)):
!             usec = ((self.__days * (24*3600L) + self.__seconds) * 1000000 +
!                     self.__microseconds)
!             return timedelta(0, 0, usec // other)
          return NotImplemented
  
***************
*** 615,625 ****
  
      def weekday(self):
!         "Return day of the week, where Monday == 0 (according to ISO)."
!         # The constant 6 was obtained experimentally :-)
!         # XXX I (tim) believe this is incorrect:  ISO defines day ordinals
!         # XXX as ranging from 1 (Monday) through 7 (Sunday).  The next line
!         # XXX (commented out) implements that:
!         # return self.toordinal() % 7 or 7
!         return (_ymd2ord(self.__year, self.__month, self.__day) + 6) % 7
  
      def isocalendar(self):
--- 613,623 ----
  
      def weekday(self):
!         "Return day of the week, where Monday == 0 ... Sunday == 6."
!         return (self.toordinal() + 6) % 7
! 
!     def isoweekday(self):
!         "Return day of the week, where Monday == 1 ... Sunday == 7."
!         # 1-Jan-0001 is a Monday
!         return self.toordinal() % 7 or 7
  
      def isocalendar(self):

Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_datetime.py,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** test_datetime.py	3 Mar 2002 06:11:54 -0000	1.13
--- test_datetime.py	3 Mar 2002 19:58:41 -0000	1.14
***************
*** 145,148 ****
--- 145,153 ----
          self.assertEqual(c*1000, timedelta(0, 1))
          self.assertEqual(1000*c, timedelta(0, 1))
+         self.assertEqual(a//7, timedelta(1))
+         self.assertEqual(b//10, timedelta(0, 6))
+         self.assertEqual(c//1000, timedelta(0, 0, 1))
+         self.assertEqual(a//10, timedelta(0, 7*24*360))
+         self.assertEqual(a//3600000, timedelta(0, 0, 7*24*1000))
  
      def test_computations(self):
***************
*** 198,203 ****
--- 203,210 ----
              # March 4, 2002 is a Monday
              self.assertEqual(datetime(2002, 3, 4+i).weekday(), i)
+             self.assertEqual(datetime(2002, 3, 4+i).isoweekday(), i+1)
              # January 2, 1956 is a Monday
              self.assertEqual(datetime(1956, 1, 2+i).weekday(), i)
+             self.assertEqual(datetime(1956, 1, 2+i).isoweekday(), i+1)
  
      def test_isocalendar(self):