[Python-checkins] python/nondist/sandbox/datetime doc.txt,1.11,1.12 test_both.py,1.37,1.38
tim_one@users.sourceforge.net
tim_one@users.sourceforge.net
Thu, 05 Dec 2002 12:28:43 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv19855
Modified Files:
doc.txt test_both.py
Log Message:
Added some timedelta tests for claims made in the docs that weren't
otherwise being tested directly, + clarified the docs.
Index: doc.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/doc.txt,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** doc.txt 5 Dec 2002 17:11:52 -0000 1.11
--- doc.txt 5 Dec 2002 20:28:41 -0000 1.12
***************
*** 66,70 ****
A week is converted to 7 days.
! and days, seconds and microseconds are normalized so that
0 <= microseconds < 1000000
--- 66,71 ----
A week is converted to 7 days.
! and days, seconds and microseconds are then normalized so that the
! representation is unique, with
0 <= microseconds < 1000000
***************
*** 73,80 ****
If any argument is a float, and there are fractional microseconds,
! the value retained is rounded to the nearest microsecond.
If the normalized value of days lies outside the indicated range,
OverflowError is raised.
Class attributes:
--- 74,91 ----
If any argument is a float, and there are fractional microseconds,
! the fractional microseconds left over from all arguments are combined
! and their sum is rounded to the nearest microsecond.
If the normalized value of days lies outside the indicated range,
OverflowError is raised.
+
+ Note that normalization of negative values may be surprising at first.
+ For example,
+
+ >>> d = timedelta(microseconds=-1)
+ >>> (d.days, d.seconds, d.microseconds)
+ (-1, 86399, 999999)
+ >>>
+
Class attributes:
Index: test_both.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_both.py,v
retrieving revision 1.37
retrieving revision 1.38
diff -C2 -d -r1.37 -r1.38
*** test_both.py 5 Dec 2002 19:45:04 -0000 1.37
--- test_both.py 5 Dec 2002 20:28:41 -0000 1.38
***************
*** 294,297 ****
--- 294,323 ----
self.assertRaises(OverflowError, lambda: -timedelta.max)
+ def test_microsecond_rounding(self):
+ td = timedelta
+ eq = self.assertEqual
+
+ # Single-field rounding.
+ eq(td(milliseconds=0.4/1000), td(0)) # rounds to 0
+ eq(td(milliseconds=-0.4/1000), td(0)) # rounds to 0
+ eq(td(milliseconds=0.6/1000), td(microseconds=1))
+ eq(td(milliseconds=-0.6/1000), td(microseconds=-1))
+
+ # Rounding due to contributions from more than one field.
+ us_per_hour = 3600e6
+ us_per_day = us_per_hour * 24
+ eq(td(days=.4/us_per_day), td(0))
+ eq(td(hours=.2/us_per_hour), td(0))
+ eq(td(days=.4/us_per_day, hours=.2/us_per_hour), td(microseconds=1))
+
+ eq(td(days=-.4/us_per_day), td(0))
+ eq(td(hours=-.2/us_per_hour), td(0))
+ eq(td(days=-.4/us_per_day, hours=-.2/us_per_hour), td(microseconds=-1))
+
+ def test_massive_normalization(self):
+ td = timedelta(microseconds=-1)
+ self.assertEqual((td.days, td.seconds, td.microseconds),
+ (-1, 24*3600-1, 999999))
+
#############################################################################
# date tests