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

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

Issue 15873: "datetime" cannot parse ISO 8601 dates and times
Patch Set: Created 4 years 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:
View unified diff | Download patch
« Lib/datetime.py ('K') | « Lib/datetime.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 copy 6 import copy
7 import decimal 7 import decimal
8 import sys 8 import sys
9 import pickle 9 import pickle
10 import random 10 import random
(...skipping 1828 matching lines...) Expand 10 before | Expand all | Expand 10 after
1839 1839
1840 1840
1841 # A helper for timestamp constructor tests. 1841 # A helper for timestamp constructor tests.
1842 def verify_field_equality(self, expected, got): 1842 def verify_field_equality(self, expected, got):
1843 self.assertEqual(expected.tm_year, got.year) 1843 self.assertEqual(expected.tm_year, got.year)
1844 self.assertEqual(expected.tm_mon, got.month) 1844 self.assertEqual(expected.tm_mon, got.month)
1845 self.assertEqual(expected.tm_mday, got.day) 1845 self.assertEqual(expected.tm_mday, got.day)
1846 self.assertEqual(expected.tm_hour, got.hour) 1846 self.assertEqual(expected.tm_hour, got.hour)
1847 self.assertEqual(expected.tm_min, got.minute) 1847 self.assertEqual(expected.tm_min, got.minute)
1848 self.assertEqual(expected.tm_sec, got.second) 1848 self.assertEqual(expected.tm_sec, got.second)
1849
1850 def test_fromisoformat(self):
1851 # Valid inputs
1852 parse_datetime = self.theclass.fromisoformat
1853 self.assertEqual(parse_datetime('2012-04-23T09:15:00'),
1854 datetime(2012, 4, 23, 9, 15))
1855 self.assertEqual(parse_datetime('2012-4-9 4:8:16'),
1856 datetime(2012, 4, 9, 4, 8, 16))
1857 self.assertEqual(parse_datetime('2012-04-23T09:15:00Z'),
1858 datetime(2012, 4, 23, 9, 15, 0, 0, timezone(timedelta(minutes=0))))
Martin Panter 2016/02/16 04:21:50 I think you can just use timezone.utc here
1859 self.assertEqual(parse_datetime('2012-4-9 4:8:16-0320'),
1860 datetime(2012, 4, 9, 4, 8, 16, 0, timezone(timedelta(hours=-3, minut es=-20))))
1861 self.assertEqual(parse_datetime('2012-04-23T10:20:30.400+02:30'),
1862 datetime(2012, 4, 23, 10, 20, 30, 400000, timezone(timedelta(hours=2 , minutes=30))))
1863 self.assertEqual(parse_datetime('2012-04-23T10:20:30.400+02'),
1864 datetime(2012, 4, 23, 10, 20, 30, 400000, timezone(timedelta(hours=2 ))))
1865 self.assertEqual(parse_datetime('2012-04-23T10:20:30.400-02'),
1866 datetime(2012, 4, 23, 10, 20, 30, 400000, timezone(timedelta(hours=- 2))))
1867 # rounding high
1868 self.assertEqual(parse_datetime('2012-04-23T10:20:30.4000059'),
1869 datetime(2012, 4, 23, 10, 20, 30, 400006))
1870 # rounding low + testing long usec decimal we don't care about
1871 self.assertEqual(parse_datetime('2012-04-23T10:20:30.400003434'),
1872 datetime(2012, 4, 23, 10, 20, 30, 400003))
Martin Panter 2016/02/16 04:21:50 Also test .00000059, because round(0.5) == 0
1873 # Invalid inputs
1874 with self.assertRaises(ValueError):
Martin Panter 2016/02/16 04:21:50 Test that all invalid dates each raise ValueError,
1875 parse_datetime('20120423091500')
1876 parse_datetime('2012-04-56T09:15:90')
haypo 2016/02/15 17:29:59 You may add tests for extreme timezones: +99:99 -
Martin Panter 2016/02/16 04:21:50 Neither of these are allowed by the RFC 3339 synta
1877
1849 1878
1850 def test_fromtimestamp(self): 1879 def test_fromtimestamp(self):
1851 import time 1880 import time
1852 1881
1853 ts = time.time() 1882 ts = time.time()
1854 expected = time.localtime(ts) 1883 expected = time.localtime(ts)
1855 got = self.theclass.fromtimestamp(ts) 1884 got = self.theclass.fromtimestamp(ts)
1856 self.verify_field_equality(expected, got) 1885 self.verify_field_equality(expected, got)
1857 1886
1858 def test_utcfromtimestamp(self): 1887 def test_utcfromtimestamp(self):
(...skipping 2083 matching lines...) Expand 10 before | Expand all | Expand 10 after
3942 datetime(10, 10, 10, 10.) 3971 datetime(10, 10, 10, 10.)
3943 with self.assertRaises(TypeError): 3972 with self.assertRaises(TypeError):
3944 datetime(10, 10, 10, 10, 10.) 3973 datetime(10, 10, 10, 10, 10.)
3945 with self.assertRaises(TypeError): 3974 with self.assertRaises(TypeError):
3946 datetime(10, 10, 10, 10, 10, 10.) 3975 datetime(10, 10, 10, 10, 10, 10.)
3947 with self.assertRaises(TypeError): 3976 with self.assertRaises(TypeError):
3948 datetime(10, 10, 10, 10, 10, 10, 10.) 3977 datetime(10, 10, 10, 10, 10, 10, 10.)
3949 3978
3950 if __name__ == "__main__": 3979 if __name__ == "__main__":
3951 unittest.main() 3980 unittest.main()
OLDNEW
« Lib/datetime.py ('K') | « Lib/datetime.py ('k') | no next file » | no next file with comments »

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