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

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

Issue 15873: "datetime" cannot parse ISO 8601 dates and times
Patch Set: Created 3 years, 8 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:
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 from test.support import is_resource_enabled 5 from test.support import is_resource_enabled
6 6
7 import itertools 7 import itertools
8 import bisect 8 import bisect
9 9
10 import copy 10 import copy
(...skipping 1086 matching lines...) Expand 10 before | Expand all | Expand 10 after
1097 def test_fromtimestamp(self): 1097 def test_fromtimestamp(self):
1098 import time 1098 import time
1099 1099
1100 # Try an arbitrary fixed value. 1100 # Try an arbitrary fixed value.
1101 year, month, day = 1999, 9, 19 1101 year, month, day = 1999, 9, 19
1102 ts = time.mktime((year, month, day, 0, 0, 0, 0, 0, -1)) 1102 ts = time.mktime((year, month, day, 0, 0, 0, 0, 0, -1))
1103 d = self.theclass.fromtimestamp(ts) 1103 d = self.theclass.fromtimestamp(ts)
1104 self.assertEqual(d.year, year) 1104 self.assertEqual(d.year, year)
1105 self.assertEqual(d.month, month) 1105 self.assertEqual(d.month, month)
1106 self.assertEqual(d.day, day) 1106 self.assertEqual(d.day, day)
1107
1108 def test_fromisoformat(self):
1109 self.assertEqual(self.theclass.fromisoformat('2014-12-31'),
1110 self.theclass(2014, 12, 31))
1111 self.assertEqual(self.theclass.fromisoformat('4095-07-31'),
1112 self.theclass(4095, 7, 31))
1113
1114 with self.assertRaises(ValueError):
1115 self.theclass.fromisoformat('2014-12-011')
1116 with self.assertRaises(ValueError):
1117 self.theclass.fromisoformat('20141211')
1118 with self.assertRaises(ValueError):
1119 self.theclass.fromisoformat('043-12-01')
1107 1120
1108 def test_insane_fromtimestamp(self): 1121 def test_insane_fromtimestamp(self):
1109 # It's possible that some platform maps time_t to double, 1122 # It's possible that some platform maps time_t to double,
1110 # and that this test will fail there. This test should 1123 # and that this test will fail there. This test should
1111 # exempt such platforms (provided they return reasonable 1124 # exempt such platforms (provided they return reasonable
1112 # results!). 1125 # results!).
1113 for insane in -1e200, 1e200: 1126 for insane in -1e200, 1e200:
1114 self.assertRaises(OverflowError, self.theclass.fromtimestamp, 1127 self.assertRaises(OverflowError, self.theclass.fromtimestamp,
1115 insane) 1128 insane)
1116 1129
(...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after
1893 got = self.theclass.fromtimestamp(ts) 1906 got = self.theclass.fromtimestamp(ts)
1894 self.verify_field_equality(expected, got) 1907 self.verify_field_equality(expected, got)
1895 1908
1896 def test_utcfromtimestamp(self): 1909 def test_utcfromtimestamp(self):
1897 import time 1910 import time
1898 1911
1899 ts = time.time() 1912 ts = time.time()
1900 expected = time.gmtime(ts) 1913 expected = time.gmtime(ts)
1901 got = self.theclass.utcfromtimestamp(ts) 1914 got = self.theclass.utcfromtimestamp(ts)
1902 self.verify_field_equality(expected, got) 1915 self.verify_field_equality(expected, got)
1916
1917 def test_fromisoformat(self):
1918 self.assertEqual(self.theclass.fromisoformat('2015-12-31T14:27:00'),
1919 self.theclass(2015, 12, 31, 14, 27, 0))
1920 self.assertEqual(self.theclass.fromisoformat('2015-12-31 14:27:00'),
1921 self.theclass(2015, 12, 31, 14, 27, 0))
1922 # lowercase 'T' date-time separator. Uncommon but tolerated (rfc 3339)
1923 self.assertEqual(self.theclass.fromisoformat('2015-12-31t14:27:00'),
1924 self.theclass(2015, 12, 31, 14, 27, 0))
1925
1926 with self.assertRaises(ValueError):
1927 self.theclass.fromisoformat('2015-01-07X00:00:00')
1903 1928
1904 # Run with US-style DST rules: DST begins 2 a.m. on second Sunday in 1929 # Run with US-style DST rules: DST begins 2 a.m. on second Sunday in
1905 # March (M3.2.0) and ends 2 a.m. on first Sunday in November (M11.1.0). 1930 # March (M3.2.0) and ends 2 a.m. on first Sunday in November (M11.1.0).
1906 @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0') 1931 @support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
1907 def test_timestamp_naive(self): 1932 def test_timestamp_naive(self):
1908 t = self.theclass(1970, 1, 1) 1933 t = self.theclass(1970, 1, 1)
1909 self.assertEqual(t.timestamp(), 18000.0) 1934 self.assertEqual(t.timestamp(), 18000.0)
1910 t = self.theclass(1970, 1, 1, 1, 2, 3, 4) 1935 t = self.theclass(1970, 1, 1, 1, 2, 3, 4)
1911 self.assertEqual(t.timestamp(), 1936 self.assertEqual(t.timestamp(),
1912 18000.0 + 3600 + 2*60 + 3 + 4*1e-6) 1937 18000.0 + 3600 + 2*60 + 3 + 4*1e-6)
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
2386 self.assertEqual(t.isoformat(timespec='auto'), "12:34:56.123456") 2411 self.assertEqual(t.isoformat(timespec='auto'), "12:34:56.123456")
2387 self.assertRaises(ValueError, t.isoformat, timespec='monkey') 2412 self.assertRaises(ValueError, t.isoformat, timespec='monkey')
2388 2413
2389 t = self.theclass(hour=12, minute=34, second=56, microsecond=999500) 2414 t = self.theclass(hour=12, minute=34, second=56, microsecond=999500)
2390 self.assertEqual(t.isoformat(timespec='milliseconds'), "12:34:56.999") 2415 self.assertEqual(t.isoformat(timespec='milliseconds'), "12:34:56.999")
2391 2416
2392 t = self.theclass(hour=12, minute=34, second=56, microsecond=0) 2417 t = self.theclass(hour=12, minute=34, second=56, microsecond=0)
2393 self.assertEqual(t.isoformat(timespec='milliseconds'), "12:34:56.000") 2418 self.assertEqual(t.isoformat(timespec='milliseconds'), "12:34:56.000")
2394 self.assertEqual(t.isoformat(timespec='microseconds'), "12:34:56.000000" ) 2419 self.assertEqual(t.isoformat(timespec='microseconds'), "12:34:56.000000" )
2395 self.assertEqual(t.isoformat(timespec='auto'), "12:34:56") 2420 self.assertEqual(t.isoformat(timespec='auto'), "12:34:56")
2421
2422 def test_fromisoformat(self):
2423 # basic
2424 self.assertEqual(self.theclass.fromisoformat('04:05:01.000123'),
2425 self.theclass(4, 5, 1, 123))
2426 self.assertEqual(self.theclass.fromisoformat('00:00:00'),
2427 self.theclass(0, 0, 0))
2428 # usec, rounding high
2429 self.assertEqual(self.theclass.fromisoformat('10:20:30.40000059'),
2430 self.theclass(10, 20, 30, 400001))
2431 # usec, rounding low + long digits we don't care about
2432 self.assertEqual(self.theclass.fromisoformat('10:20:30.400003434'),
2433 self.theclass(10, 20, 30, 400003))
2434 with self.assertRaises(ValueError):
2435 self.theclass.fromisoformat('12:00AM')
2436 with self.assertRaises(ValueError):
2437 self.theclass.fromisoformat('120000')
2438 with self.assertRaises(ValueError):
2439 self.theclass.fromisoformat('1:00')
2440 with self.assertRaises(ValueError):
2441 self.theclass.fromisoformat('17:54:43.')
2442
2443 def tz(h, m):
2444 return timezone(timedelta(hours=h, minutes=m))
2445
2446 self.assertEqual(self.theclass.fromisoformat('00:00:00Z'),
2447 self.theclass(0, 0, 0, tzinfo=timezone.utc))
2448 # lowercase UTC timezone. Uncommon but tolerated (rfc 3339)
2449 self.assertEqual(self.theclass.fromisoformat('00:00:00z'),
2450 self.theclass(0, 0, 0, tzinfo=timezone.utc))
2451 self.assertEqual(self.theclass.fromisoformat('00:00:00-00:00'),
2452 self.theclass(0, 0, 0, tzinfo=tz(0, 0)))
2453 self.assertEqual(self.theclass.fromisoformat('08:30:00.004255+02:30'),
2454 self.theclass(8, 30, 0, 4255, tz(2, 30)))
2455 self.assertEqual(self.theclass.fromisoformat('08:30:00.004255-02:30'),
2456 self.theclass(8, 30, 0, 4255, tz(-2, -30)))
2396 2457
2397 def test_1653736(self): 2458 def test_1653736(self):
2398 # verify it doesn't accept extra keyword arguments 2459 # verify it doesn't accept extra keyword arguments
2399 t = self.theclass(second=1) 2460 t = self.theclass(second=1)
2400 self.assertRaises(TypeError, t.isoformat, foo=3) 2461 self.assertRaises(TypeError, t.isoformat, foo=3)
2401 2462
2402 def test_strftime(self): 2463 def test_strftime(self):
2403 t = self.theclass(1, 2, 3, 4) 2464 t = self.theclass(1, 2, 3, 4)
2404 self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 000004") 2465 self.assertEqual(t.strftime('%H %M %S %f'), "01 02 03 000004")
2405 # A naive object replaces %z and %Z with empty strings. 2466 # A naive object replaces %z and %Z with empty strings.
(...skipping 2401 matching lines...) Expand 10 before | Expand all | Expand 10 after
4807 class IranTest(ZoneInfoTest): 4868 class IranTest(ZoneInfoTest):
4808 zonename = 'Asia/Tehran' 4869 zonename = 'Asia/Tehran'
4809 4870
4810 def load_tests(loader, standard_tests, pattern): 4871 def load_tests(loader, standard_tests, pattern):
4811 standard_tests.addTest(ZoneInfoCompleteTest()) 4872 standard_tests.addTest(ZoneInfoCompleteTest())
4812 return standard_tests 4873 return standard_tests
4813 4874
4814 4875
4815 if __name__ == "__main__": 4876 if __name__ == "__main__":
4816 unittest.main() 4877 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+