[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.11,1.12 test_datetime.py,1.7,1.8
Guido van Rossum
gvanrossum@users.sourceforge.net
Sat, 02 Mar 2002 13:09:52 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory usw-pr-cvs1:/tmp/cvs-serv23789
Modified Files:
datetime.py test_datetime.py
Log Message:
Add timedelta class, with unit tests. This doesn't yet interoperate
with the datetime class, but will be on the next iteration.
Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** datetime.py 2 Mar 2002 20:42:47 -0000 1.11
--- datetime.py 2 Mar 2002 21:09:50 -0000 1.12
***************
*** 101,105 ****
! # XXX deltaobject?
class datetime(basetime):
--- 101,207 ----
! class timedelta(object):
! """Represent the difference between two datetime objects.
!
! Supported operators:
!
! - add, subtract timedelta
! - unary plus, minus, abs
! - compare to timedelta
! - multiply, divide by int/long
!
! In addition, datetime supports subtraction of two datetime objects
! returning a timedelta, and addition or subtraction of a datetime
! and a timedelta giving a datetime.
!
! Representation: (days, seconds, microseconds). Why? Because I
! felt like it.
! """
!
! def __init__(self, days=0, seconds=0, microseconds=0):
! s, us = divmod(microseconds, 1000000)
! assert us >= 0
! d, s = divmod(s + seconds, 24*3600)
! assert s >= 0
! 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)
! if self.__seconds:
! return "timedelta(%d, %d)" % (self.__days, self.__seconds)
! return "timedelta(%d)" % self.__days
!
! days = property(lambda self: self.__days, doc="days")
! seconds = property(lambda self: self.__seconds, doc="seconds")
! microseconds = property(lambda self: self.__microseconds,
! doc="microseconds")
!
! def __add__(self, other):
! if isinstance(other, timedelta):
! return timedelta(self.__days + other.__days,
! self.__seconds + other.__seconds,
! self.__microseconds + other.__microseconds)
! return NotImplemented
!
! __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
!
! def __neg__(self):
! return timedelta(-self.__days, -self.__seconds, -self.__microseconds)
!
! def __pos__(self):
! return self
!
! def __abs__(self):
! if self.__days < 0:
! return -self
! else:
! return self
!
! def __mul__(self, other):
! if isinstance(other, (int, long)):
! return timedelta(self.__days * other,
! self.__seconds * other,
! self.__microseconds * other)
! return NotImplemented
!
! __rmul__ = __mul__
!
! def __div__(self, other):
! if isinstance(other, (int, long)):
! return timedelta(self.__days // other,
! self.__seconds // other,
! self.__microseconds // other)
! return NotImplemented
!
! __floordiv__ = __div__
!
! def __cmp__(self, other):
! if not isinstance(other, timedelta):
! raise TypeError, ("can't compare timedelta to %s instance" %
! type(other).__name__)
! diff = self - other
! if diff.__days < 0:
! return -1
! if diff.__days == 0 == diff.__seconds == diff.__microseconds:
! return 0
! return 1
!
class datetime(basetime):
Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_datetime.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** test_datetime.py 2 Mar 2002 20:42:47 -0000 1.7
--- test_datetime.py 2 Mar 2002 21:09:50 -0000 1.8
***************
*** 7,11 ****
import unittest
! from datetime import datetime
class TestDateTime(unittest.TestCase):
--- 7,11 ----
import unittest
! from datetime import datetime, timedelta
class TestDateTime(unittest.TestCase):
***************
*** 101,104 ****
--- 101,135 ----
self.assertEqual(d, e)
self.assertEqual(hash(d), hash(e))
+
+ def test_timedelta(self):
+ a = timedelta(7) # One week
+ b = timedelta(0, 60) # One minute
+ c = timedelta(0, 0, 1000) # One millisecond
+ self.assertEqual(a+b+c, timedelta(7, 60, 1000))
+ self.assertEqual(a-b, timedelta(6, 24*3600 - 60))
+ self.assertEqual(-a, timedelta(-7))
+ self.assertEqual(+a, timedelta(7))
+ self.assertEqual(-b, timedelta(-1, 24*3600 - 60))
+ self.assertEqual(-c, timedelta(-1, 24*3600 - 1, 999000))
+ self.assertEqual(abs(a), a)
+ self.assertEqual(abs(-a), a)
+ self.assertEqual(timedelta(6, 24*3600), a)
+ self.assertEqual(timedelta(0, 0, 60*1000000), b)
+ self.assertEqual(a*10, timedelta(70))
+ self.assertEqual(a*10, 10*a)
+ self.assertEqual(a*10L, 10*a)
+ self.assertEqual(b*10, timedelta(0, 600))
+ self.assertEqual(10*b, timedelta(0, 600))
+ self.assertEqual(b*10L, timedelta(0, 600))
+ self.assertEqual(c*10, timedelta(0, 0, 10000))
+ self.assertEqual(10*c, timedelta(0, 0, 10000))
+ self.assertEqual(c*10L, timedelta(0, 0, 10000))
+ self.assertEqual(a*-1, -a)
+ self.assertEqual(b*-2, -b-b)
+ self.assertEqual(c*-2, -c+-c)
+ self.assertEqual(b*(60*24), (b*60)*24)
+ self.assertEqual(b*(60*24), (60*b)*24)
+ self.assertEqual(c*1000, timedelta(0, 1))
+ self.assertEqual(1000*c, timedelta(0, 1))
def test_suite():