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

## Side by Side Diff: Lib/test/datetimetester.py

Issue 23517: datetime.utcfromtimestamp rounds results incorrectly
Patch Set: Created 4 years, 6 months ago
 Left: Base Patch Set 1: None Patch Set 2: None Patch Set 3: None Right: Patch Set 1: None Patch Set 2: None Patch Set 3: None
OLDNEW
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...)
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=0))
654 eq(td(milliseconds=-0.5/1000), td(microseconds=-0))
653 eq(td(milliseconds=0.6/1000), td(microseconds=1)) 655 eq(td(milliseconds=0.6/1000), td(microseconds=1))
654 eq(td(milliseconds=-0.6/1000), td(microseconds=-1)) 656 eq(td(milliseconds=-0.6/1000), td(microseconds=-1))
657 eq(td(milliseconds=1.5/1000), td(microseconds=2))
658 eq(td(milliseconds=-1.5/1000), td(microseconds=-2))
659 eq(td(seconds=0.5/10**6), td(microseconds=0))
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))
655 663
656 # Rounding due to contributions from more than one field. 664 # Rounding due to contributions from more than one field.
657 us_per_hour = 3600e6 665 us_per_hour = 3600e6
658 us_per_day = us_per_hour * 24 666 us_per_day = us_per_hour * 24
659 eq(td(days=.4/us_per_day), td(0)) 667 eq(td(days=.4/us_per_day), td(0))
660 eq(td(hours=.2/us_per_hour), td(0)) 668 eq(td(hours=.2/us_per_hour), td(0))
661 eq(td(days=.4/us_per_day, hours=.2/us_per_hour), td(microseconds=1)) 669 eq(td(days=.4/us_per_day, hours=.2/us_per_hour), td(microseconds=1))
662 670
663 eq(td(days=-.4/us_per_day), td(0)) 671 eq(td(days=-.4/us_per_day), td(0))
664 eq(td(hours=-.2/us_per_hour), td(0)) 672 eq(td(hours=-.2/us_per_hour), td(0))
(...skipping 1152 matching lines...)
1817 def test_timestamp_aware(self): 1825 def test_timestamp_aware(self):
1818 t = self.theclass(1970, 1, 1, tzinfo=timezone.utc) 1826 t = self.theclass(1970, 1, 1, tzinfo=timezone.utc)
1819 self.assertEqual(t.timestamp(), 0.0) 1827 self.assertEqual(t.timestamp(), 0.0)
1820 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)
1821 self.assertEqual(t.timestamp(), 1829 self.assertEqual(t.timestamp(),
1822 3600 + 2*60 + 3 + 4*1e-6) 1830 3600 + 2*60 + 3 + 4*1e-6)
1823 t = self.theclass(1970, 1, 1, 1, 2, 3, 4, 1831 t = self.theclass(1970, 1, 1, 1, 2, 3, 4,
1824 tzinfo=timezone(timedelta(hours=-5), 'EST')) 1832 tzinfo=timezone(timedelta(hours=-5), 'EST'))
1825 self.assertEqual(t.timestamp(), 1833 self.assertEqual(t.timestamp(),
1826 18000 + 3600 + 2*60 + 3 + 4*1e-6) 1834 18000 + 3600 + 2*60 + 3 + 4*1e-6)
1835
1827 def test_microsecond_rounding(self): 1836 def test_microsecond_rounding(self):
1828 for fts in [self.theclass.fromtimestamp, 1837 for fts in [self.theclass.fromtimestamp,
1829 self.theclass.utcfromtimestamp]: 1838 self.theclass.utcfromtimestamp]:
1830 zero = fts(0) 1839 zero = fts(0)
1831 self.assertEqual(zero.second, 0) 1840 self.assertEqual(zero.second, 0)
1832 self.assertEqual(zero.microsecond, 0) 1841 self.assertEqual(zero.microsecond, 0)
1842 one = fts(1e-6)
1833 try: 1843 try:
1834 minus_one = fts(-1e-6) 1844 minus_one = fts(-1e-6)
1835 except OSError: 1845 except OSError:
1836 # localtime(-1) and gmtime(-1) is not supported on Windows 1846 # localtime(-1) and gmtime(-1) is not supported on Windows
1837 pass 1847 pass
1838 else: 1848 else:
1839 self.assertEqual(minus_one.second, 59) 1849 self.assertEqual(minus_one.second, 59)
1840 self.assertEqual(minus_one.microsecond, 999999) 1850 self.assertEqual(minus_one.microsecond, 999999)
1841 1851
1842 t = fts(-1e-8) 1852 t = fts(-1e-8)
1843 self.assertEqual(t, minus_one) 1853 self.assertEqual(t, zero)
1844 t = fts(-9e-7) 1854 t = fts(-9e-7)
1845 self.assertEqual(t, minus_one) 1855 self.assertEqual(t, minus_one)
1846 t = fts(-1e-7) 1856 t = fts(-1e-7)
1847 self.assertEqual(t, minus_one) 1857 self.assertEqual(t, zero)
1858 t = fts(-1/2**7)
1859 self.assertEqual(t.second, 59)
1860 self.assertEqual(t.microsecond, 992188)
1848 1861
1849 t = fts(1e-7) 1862 t = fts(1e-7)
1850 self.assertEqual(t, zero) 1863 self.assertEqual(t, zero)
1851 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-
1852 self.assertEqual(t, zero) 1865 self.assertEqual(t, one)
1853 t = fts(0.99999949) 1866 t = fts(0.99999949)
1854 self.assertEqual(t.second, 0) 1867 self.assertEqual(t.second, 0)
1855 self.assertEqual(t.microsecond, 999999) 1868 self.assertEqual(t.microsecond, 999999)
1856 t = fts(0.9999999) 1869 t = fts(0.9999999)
1870 self.assertEqual(t.second, 1)
1871 self.assertEqual(t.microsecond, 0)
1872 t = fts(1/2**7)
1857 self.assertEqual(t.second, 0) 1873 self.assertEqual(t.second, 0)
1858 self.assertEqual(t.microsecond, 999999) 1874 self.assertEqual(t.microsecond, 7812)
1859 1875
1860 def test_insane_fromtimestamp(self): 1876 def test_insane_fromtimestamp(self):
1861 # It's possible that some platform maps time_t to double, 1877 # It's possible that some platform maps time_t to double,
1862 # and that this test will fail there. This test should 1878 # and that this test will fail there. This test should
1863 # exempt such platforms (provided they return reasonable 1879 # exempt such platforms (provided they return reasonable
1864 # results!). 1880 # results!).
1865 for insane in -1e200, 1e200: 1881 for insane in -1e200, 1e200:
1866 self.assertRaises(OverflowError, self.theclass.fromtimestamp, 1882 self.assertRaises(OverflowError, self.theclass.fromtimestamp,
1867 insane) 1883 insane)
1868 1884
(...skipping 1943 matching lines...)
3812 datetime_sc = SubclassDatetime(as_datetime.year, as_datetime.month, 3828 datetime_sc = SubclassDatetime(as_datetime.year, as_datetime.month,
3813 as_date.day, 0, 0, 0) 3829 as_date.day, 0, 0, 0)
3814 self.assertEqual(as_datetime, datetime_sc) 3830 self.assertEqual(as_datetime, datetime_sc)
3815 self.assertEqual(datetime_sc, as_datetime) 3831 self.assertEqual(datetime_sc, as_datetime)
3816 3832
3817 def test_main(): 3833 def test_main():
3818 support.run_unittest(__name__) 3834 support.run_unittest(__name__)
3819 3835
3820 if __name__ == "__main__": 3836 if __name__ == "__main__":
3821 test_main() 3837 test_main()
OLDNEW

This is Rietveld 894c83f36cb7+