Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(31)

Delta Between Two Patch Sets: Lib/test/datetimetester.py

Issue 23517: datetime.utcfromtimestamp rounds results incorrectly
Left Patch Set: Created 4 years, 5 months ago
Right Patch Set: Created 4 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 """Test date/time type. 1 """Test date/time type.
2 2
3 See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases 3 See http://www.zope.org/Members/fdrake/DateTimeWiki/TestCases
4 """ 4 """
5 5
6 import sys 6 import sys
7 import pickle 7 import pickle
8 import random 8 import random
9 import unittest 9 import unittest
10 10
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 self.assertRaises(OverflowError, day.__mul__, INF) 643 self.assertRaises(OverflowError, day.__mul__, INF)
644 self.assertRaises(OverflowError, day.__mul__, -INF) 644 self.assertRaises(OverflowError, day.__mul__, -INF)
645 645
646 def test_microsecond_rounding(self): 646 def test_microsecond_rounding(self):
647 td = timedelta 647 td = timedelta
648 eq = self.assertEqual 648 eq = self.assertEqual
649 649
650 # Single-field rounding. 650 # Single-field rounding.
651 eq(td(milliseconds=0.4/1000), td(0)) # rounds to 0 651 eq(td(milliseconds=0.4/1000), td(0)) # rounds to 0
652 eq(td(milliseconds=-0.4/1000), td(0)) # rounds to 0 652 eq(td(milliseconds=-0.4/1000), td(0)) # rounds to 0
653 eq(td(milliseconds=0.5/1000), td(microseconds=1)) 653 eq(td(milliseconds=0.5/1000), td(microseconds=0))
654 eq(td(milliseconds=-0.5/1000), td(microseconds=-1)) 654 eq(td(milliseconds=-0.5/1000), td(microseconds=-0))
655 eq(td(milliseconds=0.6/1000), td(microseconds=1)) 655 eq(td(milliseconds=0.6/1000), td(microseconds=1))
656 eq(td(milliseconds=-0.6/1000), td(microseconds=-1)) 656 eq(td(milliseconds=-0.6/1000), td(microseconds=-1))
657 eq(td(seconds=0.5/10**6), td(microseconds=1)) 657 eq(td(milliseconds=1.5/1000), td(microseconds=2))
658 eq(td(seconds=-0.5/10**6), td(microseconds=-1)) 658 eq(td(milliseconds=-1.5/1000), td(microseconds=-2))
659 eq(td(seconds=1/2**7), td(microseconds=7813)) 659 eq(td(seconds=0.5/10**6), td(microseconds=0))
660 eq(td(seconds=-1/2**7), td(microseconds=-7813)) 660 eq(td(seconds=-0.5/10**6), td(microseconds=-0))
661 eq(td(seconds=1/2**7), td(microseconds=7812))
662 eq(td(seconds=-1/2**7), td(microseconds=-7812))
661 663
662 # Rounding due to contributions from more than one field. 664 # Rounding due to contributions from more than one field.
663 us_per_hour = 3600e6 665 us_per_hour = 3600e6
664 us_per_day = us_per_hour * 24 666 us_per_day = us_per_hour * 24
665 eq(td(days=.4/us_per_day), td(0)) 667 eq(td(days=.4/us_per_day), td(0))
666 eq(td(hours=.2/us_per_hour), td(0)) 668 eq(td(hours=.2/us_per_hour), td(0))
667 eq(td(days=.4/us_per_day, hours=.2/us_per_hour), td(microseconds=1), td) 669 eq(td(days=.4/us_per_day, hours=.2/us_per_hour), td(microseconds=1))
668 670
669 eq(td(days=-.4/us_per_day), td(0)) 671 eq(td(days=-.4/us_per_day), td(0))
670 eq(td(hours=-.2/us_per_hour), td(0)) 672 eq(td(hours=-.2/us_per_hour), td(0))
671 eq(td(days=-.4/us_per_day, hours=-.2/us_per_hour), td(microseconds=-1)) 673 eq(td(days=-.4/us_per_day, hours=-.2/us_per_hour), td(microseconds=-1))
674
675 # Test for a patch in Issue 8860
676 eq(td(microseconds=0.5), 0.5*td(microseconds=1.0))
677 eq(td(microseconds=0.5)//td.resolution, 0.5*td.resolution//td.resolution )
672 678
673 def test_massive_normalization(self): 679 def test_massive_normalization(self):
674 td = timedelta(microseconds=-1) 680 td = timedelta(microseconds=-1)
675 self.assertEqual((td.days, td.seconds, td.microseconds), 681 self.assertEqual((td.days, td.seconds, td.microseconds),
676 (-1, 24*3600-1, 999999)) 682 (-1, 24*3600-1, 999999))
677 683
678 def test_bool(self): 684 def test_bool(self):
679 self.assertTrue(timedelta(1)) 685 self.assertTrue(timedelta(1))
680 self.assertTrue(timedelta(0, 1)) 686 self.assertTrue(timedelta(0, 1))
681 self.assertTrue(timedelta(0, 0, 1)) 687 self.assertTrue(timedelta(0, 0, 1))
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1819 def test_timestamp_aware(self): 1825 def test_timestamp_aware(self):
1820 t = self.theclass(1970, 1, 1, tzinfo=timezone.utc) 1826 t = self.theclass(1970, 1, 1, tzinfo=timezone.utc)
1821 self.assertEqual(t.timestamp(), 0.0) 1827 self.assertEqual(t.timestamp(), 0.0)
1822 t = self.theclass(1970, 1, 1, 1, 2, 3, 4, tzinfo=timezone.utc) 1828 t = self.theclass(1970, 1, 1, 1, 2, 3, 4, tzinfo=timezone.utc)
1823 self.assertEqual(t.timestamp(), 1829 self.assertEqual(t.timestamp(),
1824 3600 + 2*60 + 3 + 4*1e-6) 1830 3600 + 2*60 + 3 + 4*1e-6)
1825 t = self.theclass(1970, 1, 1, 1, 2, 3, 4, 1831 t = self.theclass(1970, 1, 1, 1, 2, 3, 4,
1826 tzinfo=timezone(timedelta(hours=-5), 'EST')) 1832 tzinfo=timezone(timedelta(hours=-5), 'EST'))
1827 self.assertEqual(t.timestamp(), 1833 self.assertEqual(t.timestamp(),
1828 18000 + 3600 + 2*60 + 3 + 4*1e-6) 1834 18000 + 3600 + 2*60 + 3 + 4*1e-6)
1835
1829 def test_microsecond_rounding(self): 1836 def test_microsecond_rounding(self):
1830 for fts in [self.theclass.fromtimestamp, 1837 for fts in [self.theclass.fromtimestamp,
1831 self.theclass.utcfromtimestamp]: 1838 self.theclass.utcfromtimestamp]:
1832 zero = fts(0) 1839 zero = fts(0)
1833 self.assertEqual(zero.second, 0) 1840 self.assertEqual(zero.second, 0)
1834 self.assertEqual(zero.microsecond, 0) 1841 self.assertEqual(zero.microsecond, 0)
1835 one = fts(1e-6) 1842 one = fts(1e-6)
1836 try: 1843 try:
1837 minus_one = fts(-1e-6) 1844 minus_one = fts(-1e-6)
1838 except OSError: 1845 except OSError:
1839 # localtime(-1) and gmtime(-1) is not supported on Windows 1846 # localtime(-1) and gmtime(-1) is not supported on Windows
1840 pass 1847 pass
1841 else: 1848 else:
1842 self.assertEqual(minus_one.second, 59) 1849 self.assertEqual(minus_one.second, 59)
1843 self.assertEqual(minus_one.microsecond, 999999) 1850 self.assertEqual(minus_one.microsecond, 999999)
1844 1851
1845 t = fts(-1e-8) 1852 t = fts(-1e-8)
1846 self.assertEqual(t, zero) 1853 self.assertEqual(t, zero)
1847 t = fts(-9e-7) 1854 t = fts(-9e-7)
1848 self.assertEqual(t, minus_one) 1855 self.assertEqual(t, minus_one)
1849 t = fts(-1e-7) 1856 t = fts(-1e-7)
1850 self.assertEqual(t, zero) 1857 self.assertEqual(t, zero)
1858 t = fts(-1/2**7)
1859 self.assertEqual(t.second, 59)
1860 self.assertEqual(t.microsecond, 992188)
1851 1861
1852 t = fts(1e-7) 1862 t = fts(1e-7)
1853 self.assertEqual(t, zero) 1863 self.assertEqual(t, zero)
1854 t = fts(9e-7) 1864 t = fts(9e-7)
storchaka 2015/09/10 22:27:10 It would be interesting to test also 5e-7 and 15e-
1855 self.assertEqual(t, one) 1865 self.assertEqual(t, one)
1856 t = fts(0.99999949) 1866 t = fts(0.99999949)
1857 self.assertEqual(t.second, 0) 1867 self.assertEqual(t.second, 0)
1858 self.assertEqual(t.microsecond, 999999) 1868 self.assertEqual(t.microsecond, 999999)
1859 t = fts(0.9999999) 1869 t = fts(0.9999999)
1860 self.assertEqual(t.second, 1) 1870 self.assertEqual(t.second, 1)
1861 self.assertEqual(t.microsecond, 0) 1871 self.assertEqual(t.microsecond, 0)
1862 t = fts(1/2**7) 1872 t = fts(1/2**7)
1863 self.assertEqual(t.second, 0) 1873 self.assertEqual(t.second, 0)
1864 self.assertEqual(t.microsecond, 7813) 1874 self.assertEqual(t.microsecond, 7812)
1865 t = fts(-1/2**7)
1866 self.assertEqual(t.second, 59)
1867 self.assertEqual(t.microsecond, 992187)
1868 1875
1869 def test_insane_fromtimestamp(self): 1876 def test_insane_fromtimestamp(self):
1870 # It's possible that some platform maps time_t to double, 1877 # It's possible that some platform maps time_t to double,
1871 # and that this test will fail there. This test should 1878 # and that this test will fail there. This test should
1872 # exempt such platforms (provided they return reasonable 1879 # exempt such platforms (provided they return reasonable
1873 # results!). 1880 # results!).
1874 for insane in -1e200, 1e200: 1881 for insane in -1e200, 1e200:
1875 self.assertRaises(OverflowError, self.theclass.fromtimestamp, 1882 self.assertRaises(OverflowError, self.theclass.fromtimestamp,
1876 insane) 1883 insane)
1877 1884
(...skipping 1943 matching lines...) Expand 10 before | Expand all | Expand 10 after
3821 datetime_sc = SubclassDatetime(as_datetime.year, as_datetime.month, 3828 datetime_sc = SubclassDatetime(as_datetime.year, as_datetime.month,
3822 as_date.day, 0, 0, 0) 3829 as_date.day, 0, 0, 0)
3823 self.assertEqual(as_datetime, datetime_sc) 3830 self.assertEqual(as_datetime, datetime_sc)
3824 self.assertEqual(datetime_sc, as_datetime) 3831 self.assertEqual(datetime_sc, as_datetime)
3825 3832
3826 def test_main(): 3833 def test_main():
3827 support.run_unittest(__name__) 3834 support.run_unittest(__name__)
3828 3835
3829 if __name__ == "__main__": 3836 if __name__ == "__main__":
3830 test_main() 3837 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+