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

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

Issue 15873: "datetime" cannot parse ISO 8601 dates and times
Patch Set: Created 4 years, 1 month 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
« no previous file with comments | « 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 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 def test_fromtimestamp(self): 1085 def test_fromtimestamp(self):
1086 import time 1086 import time
1087 1087
1088 # Try an arbitrary fixed value. 1088 # Try an arbitrary fixed value.
1089 year, month, day = 1999, 9, 19 1089 year, month, day = 1999, 9, 19
1090 ts = time.mktime((year, month, day, 0, 0, 0, 0, 0, -1)) 1090 ts = time.mktime((year, month, day, 0, 0, 0, 0, 0, -1))
1091 d = self.theclass.fromtimestamp(ts) 1091 d = self.theclass.fromtimestamp(ts)
1092 self.assertEqual(d.year, year) 1092 self.assertEqual(d.year, year)
1093 self.assertEqual(d.month, month) 1093 self.assertEqual(d.month, month)
1094 self.assertEqual(d.day, day) 1094 self.assertEqual(d.day, day)
1095
1096 def test_fromisoformat(self):
1097 self.assertEqual(self.theclass.fromisoformat('2014-12-31'),
1098 self.theclass(2014, 12, 31))
1099 self.assertEqual(self.theclass.fromisoformat('4095-07-31'),
1100 self.theclass(4095, 7, 31))
1101
1102 with self.assertRaises(ValueError):
1103 self.theclass.fromisoformat('2014-12-011')
1104 self.theclass.fromisoformat('20141211')
1105 self.theclass.fromisoformat('043-12-01')
1095 1106
1096 def test_insane_fromtimestamp(self): 1107 def test_insane_fromtimestamp(self):
1097 # It's possible that some platform maps time_t to double, 1108 # It's possible that some platform maps time_t to double,
1098 # and that this test will fail there. This test should 1109 # and that this test will fail there. This test should
1099 # exempt such platforms (provided they return reasonable 1110 # exempt such platforms (provided they return reasonable
1100 # results!). 1111 # results!).
1101 for insane in -1e200, 1e200: 1112 for insane in -1e200, 1e200:
1102 self.assertRaises(OverflowError, self.theclass.fromtimestamp, 1113 self.assertRaises(OverflowError, self.theclass.fromtimestamp,
1103 insane) 1114 insane)
1104 1115
(...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after
1855 got = self.theclass.fromtimestamp(ts) 1866 got = self.theclass.fromtimestamp(ts)
1856 self.verify_field_equality(expected, got) 1867 self.verify_field_equality(expected, got)
1857 1868
1858 def test_utcfromtimestamp(self): 1869 def test_utcfromtimestamp(self):
1859 import time 1870 import time
1860 1871
1861 ts = time.time() 1872 ts = time.time()
1862 expected = time.gmtime(ts) 1873 expected = time.gmtime(ts)
1863 got = self.theclass.utcfromtimestamp(ts) 1874 got = self.theclass.utcfromtimestamp(ts)
1864 self.verify_field_equality(expected, got) 1875 self.verify_field_equality(expected, got)
1876
1877 def test_fromisoformat(self):
1878 self.assertEqual(self.theclass.fromisoformat('2015-12-31T14:27:00'),
1879 self.theclass(2015, 12, 31, 14, 27, 0))
1880 self.assertEqual(self.theclass.fromisoformat('2015-12-31 14:27:00'),
1881 self.theclass(2015, 12, 31, 14, 27, 0))
1882 # lowercase 'T' date-time separator. Uncommon but tolerated (rfc 3339)
1883 self.assertEqual(self.theclass.fromisoformat('2015-12-31t14:27:00'),
1884 self.theclass(2015, 12, 31, 14, 27, 0))
1885
1886 with self.assertRaises(ValueError):
1887 self.theclass.fromisoformat('2015-01-07X00:00:00')
1865 1888
1866 # Run with US-style DST rules: DST begins 2 a.m. on second Sunday in 1889 # Run with US-style DST rules: DST begins 2 a.m. on second Sunday in
1867 # March (M3.2.0) and ends 2 a.m. on first Sunday in November (M11.1.0). 1890 # March (M3.2.0) and ends 2 a.m. on first Sunday in November (M11.1.0).
1868 @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0') 1891 @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
1869 def test_timestamp_naive(self): 1892 def test_timestamp_naive(self):
1870 t = self.theclass(1970, 1, 1) 1893 t = self.theclass(1970, 1, 1)
1871 self.assertEqual(t.timestamp(), 18000.0) 1894 self.assertEqual(t.timestamp(), 18000.0)
1872 t = self.theclass(1970, 1, 1, 1, 2, 3, 4) 1895 t = self.theclass(1970, 1, 1, 1, 2, 3, 4)
1873 self.assertEqual(t.timestamp(), 1896 self.assertEqual(t.timestamp(),
1874 18000.0 + 3600 + 2*60 + 3 + 4*1e-6) 1897 18000.0 + 3600 + 2*60 + 3 + 4*1e-6)
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
2314 self.assertEqual(t.isoformat(), "00:00:00.001000") 2337 self.assertEqual(t.isoformat(), "00:00:00.001000")
2315 self.assertEqual(t.isoformat(), str(t)) 2338 self.assertEqual(t.isoformat(), str(t))
2316 2339
2317 t = self.theclass(microsecond=10000) 2340 t = self.theclass(microsecond=10000)
2318 self.assertEqual(t.isoformat(), "00:00:00.010000") 2341 self.assertEqual(t.isoformat(), "00:00:00.010000")
2319 self.assertEqual(t.isoformat(), str(t)) 2342 self.assertEqual(t.isoformat(), str(t))
2320 2343
2321 t = self.theclass(microsecond=100000) 2344 t = self.theclass(microsecond=100000)
2322 self.assertEqual(t.isoformat(), "00:00:00.100000") 2345 self.assertEqual(t.isoformat(), "00:00:00.100000")
2323 self.assertEqual(t.isoformat(), str(t)) 2346 self.assertEqual(t.isoformat(), str(t))
2347
2348 def test_fromisoformat(self):
2349 # basic
2350 self.assertEqual(self.theclass.fromisoformat('04:05:01.000123'),
2351 self.theclass(4, 5, 1, 123))
2352 self.assertEqual(self.theclass.fromisoformat('00:00:00'),
2353 self.theclass(0, 0, 0))
2354 # usec, rounding high
2355 self.assertEqual(self.theclass.fromisoformat('10:20:30.40000059'),
2356 self.theclass(10, 20, 30, 400001))
2357 # usec, rounding low + long digits we don't care about
2358 self.assertEqual(self.theclass.fromisoformat('10:20:30.400003434'),
2359 self.theclass(10, 20, 30, 400003))
2360 with self.assertRaises(ValueError):
2361 self.theclass.fromisoformat('12:00AM')
2362 self.theclass.fromisoformat('120000')
2363 self.theclass.fromisoformat('1:00')
2364
2365 def tz(h, m):
2366 return timezone(timedelta(hours=h, minutes=m))
2367
2368 self.assertEqual(self.theclass.fromisoformat('00:00:00Z'),
2369 self.theclass(0, 0, 0, tzinfo=timezone.utc))
2370 # lowercase UTC timezone. Uncommon but tolerated (rfc 3339)
2371 self.assertEqual(self.theclass.fromisoformat('00:00:00z'),
2372 self.theclass(0, 0, 0, tzinfo=timezone.utc))
2373 self.assertEqual(self.theclass.fromisoformat('00:00:00-00:00'),
2374 self.theclass(0, 0, 0, tzinfo=tz(0, 0)))
2375 self.assertEqual(self.theclass.fromisoformat('08:30:00.004255+02:30'),
2376 self.theclass(8, 30, 0, 4255, tz(2, 30)))
2377 self.assertEqual(self.theclass.fromisoformat('08:30:00.004255-02:30'),
2378 self.theclass(8, 30, 0, 4255, tz(-2, -30)))
2324 2379
2325 def test_1653736(self): 2380 def test_1653736(self):
2326 # verify it doesn't accept extra keyword arguments 2381 # verify it doesn't accept extra keyword arguments
2327 t = self.theclass(second=1) 2382 t = self.theclass(second=1)
2328 self.assertRaises(TypeError, t.isoformat, foo=3) 2383 self.assertRaises(TypeError, t.isoformat, foo=3)
2329 2384
2330 def test_strftime(self): 2385 def test_strftime(self):
2331 t = self.theclass(1, 2, 3, 4) 2386 t = self.theclass(1, 2, 3, 4)
2332 self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 000004") 2387 self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 000004")
2333 # A naive object replaces %z and %Z with empty strings. 2388 # A naive object replaces %z and %Z with empty strings.
(...skipping 1608 matching lines...) Expand 10 before | Expand all | Expand 10 after
3942 datetime(10, 10, 10, 10.) 3997 datetime(10, 10, 10, 10.)
3943 with self.assertRaises(TypeError): 3998 with self.assertRaises(TypeError):
3944 datetime(10, 10, 10, 10, 10.) 3999 datetime(10, 10, 10, 10, 10.)
3945 with self.assertRaises(TypeError): 4000 with self.assertRaises(TypeError):
3946 datetime(10, 10, 10, 10, 10, 10.) 4001 datetime(10, 10, 10, 10, 10, 10.)
3947 with self.assertRaises(TypeError): 4002 with self.assertRaises(TypeError):
3948 datetime(10, 10, 10, 10, 10, 10, 10.) 4003 datetime(10, 10, 10, 10, 10, 10, 10.)
3949 4004
3950 if __name__ == "__main__": 4005 if __name__ == "__main__":
3951 unittest.main() 4006 unittest.main()
OLDNEW
« no previous file with comments | « Lib/datetime.py ('k') | no next file » | no next file with comments »

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