Skip to content

Commit 6cabc24

Browse files
nineteendopganssle
andauthored
Apply suggestions from code review
Co-authored-by: Paul Ganssle <[email protected]>
1 parent 3f6b729 commit 6cabc24

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

Lib/test/datetimetester.py

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,25 +1107,45 @@ def test_delta_non_days_ignored(self):
11071107
self.assertEqual(dt2, dt - days)
11081108

11091109
def test_strptime(self):
1110-
string = '2004-12-01'
1111-
format = '%Y-%m-%d'
1112-
expected = _strptime._strptime_datetime_date(date, string, format)
1113-
got = date.strptime(string, format)
1114-
self.assertEqual(expected, got)
1115-
self.assertIs(type(expected), date)
1116-
self.assertIs(type(got), date)
1117-
1118-
# bpo-34482: Check that surrogates are handled properly.
11191110
inputs = [
1120-
('2004-12\ud80001', '%Y-%m\ud800%d'),
1121-
('2004\ud80012-01', '%Y\ud800%m-%d'),
1111+
# Basic valid cases
1112+
(date(1998, 2, 3), '1998-02-03', '%Y-%m-%d'),
1113+
(date(2004, 12, 2), '2004-12-02', '%Y-%m-%d'),
1114+
1115+
# Edge cases: Leap year
1116+
(date(2020, 2, 29), '2020-02-29', '%Y-%m-%d'), # Valid leap year date
1117+
1118+
# bpo-34482: Handle surrogate pairs
1119+
(date(2004, 12, 2), '2004-12\ud80002', '%Y-%m\ud800%d'),
1120+
(date(2004, 12, 2), '2004\ud80012-02', '%Y\ud800%m-%d'),
1121+
1122+
# Month/day variations
1123+
(date(2004, 2, 1), '2004-02', '%Y-%m'), # No day provided
1124+
(date(2004, 2, 1), '02-2004', '%m-%Y'), # Month and year swapped
1125+
1126+
# Different day-month-year formats
1127+
(date(2004, 12, 2), '01/12/2004', '%d/%m/%Y'), # Day/Month/Year
1128+
(date(2004, 12, 2), '12/01/2004', '%m/%d/%Y'), # Month/Day/Year
1129+
1130+
# Different separators
1131+
(date(2023, 9, 24), '24.09.2023', '%d.%m.%Y'), # Dots as separators
1132+
(date(2023, 9, 24), '24-09-2023', '%d-%m-%Y'), # Dashes
1133+
(date(2023, 9, 24), '2023/09/24', '%Y/%m/%d'), # Slashes
1134+
1135+
# Handling years with fewer digits
1136+
(date(127, 2, 3), '0127-02-03', '%Y-%m-%d'),
1137+
(date(99, 2, 3), '0099-02-03', '%Y-%m-%d'),
1138+
(date(5, 2, 3), '0005-02-03', '%Y-%m-%d'),
1139+
1140+
# Variations on ISO 8601 format
1141+
(date(2023, 9, 24), '2023-W39-1', '%G-W%V-%u'), # ISO week date (Week 39, Monday)
1142+
(date(2023, 9, 24), '2023-267', '%Y-%j'), # Year and day of the year (Julian)
11221143
]
1123-
for string, format in inputs:
1144+
for expected, string, format in inputs:
11241145
with self.subTest(string=string, format=format):
1125-
expected = _strptime._strptime_datetime_date(date, string,
1126-
format)
11271146
got = date.strptime(string, format)
11281147
self.assertEqual(expected, got)
1148+
self.assertIs(type(got), date)
11291149

11301150
def test_strptime_single_digit(self):
11311151
# bpo-34903: Check that single digit dates are allowed.
@@ -3801,27 +3821,18 @@ def test_compat_unpickle(self):
38013821
self.assertEqual(derived, expected)
38023822

38033823
def test_strptime(self):
3804-
string = '13:02:47.197'
3805-
format = '%H:%M:%S.%f'
3806-
expected = _strptime._strptime_datetime_time(self.theclass, string,
3807-
format)
3808-
got = self.theclass.strptime(string, format)
3809-
self.assertEqual(expected, got)
3810-
self.assertIs(type(expected), self.theclass)
3811-
self.assertIs(type(got), self.theclass)
3812-
38133824
# bpo-34482: Check that surrogates are handled properly.
38143825
inputs = [
3815-
('13:02\ud80047.197', '%H:%M\ud800%S.%f'),
3816-
('13\ud80002:47.197', '%H\ud800%M:%S.%f'),
3826+
(self.theclass(13, 2, 47, 197000), '13:02:47.197', '%H:%M:%S.%f'),
3827+
(self.theclass(13, 2, 47, 197000), '13:02\ud80047.197', '%H:%M\ud800%S.%f'),
3828+
(self.theclass(13, 2, 47, 197000), '13\ud80002:47.197', '%H\ud800%M:%S.%f'),
38173829
]
3818-
for string, format in inputs:
3830+
for expected, string, format in inputs:
38193831
with self.subTest(string=string, format=format):
3820-
expected = _strptime._strptime_datetime_time(self.theclass,
3821-
string, format)
3822-
got = self.theclass.strptime(string, format)
38233832
self.assertEqual(expected, got)
3833+
self.assertIs(type(got), self.theclass)
38243834

3835+
def test_strptime_tz(self):
38253836
strptime = self.theclass.strptime
38263837
self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
38273838
self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
@@ -3840,9 +3851,11 @@ def test_strptime(self):
38403851
seconds = tzseconds
38413852
hours, minutes = divmod(seconds//60, 60)
38423853
tstr = "{}{:02d}{:02d} {}".format(sign, hours, minutes, tzname)
3843-
t = strptime(tstr, "%z %Z")
3844-
self.assertEqual(t.utcoffset(), timedelta(seconds=tzseconds))
3845-
self.assertEqual(t.tzname(), tzname)
3854+
with self.subTest(tstr=tstr):
3855+
t = strptime(tstr, "%z %Z")
3856+
self.assertEqual(t.utcoffset(), timedelta(seconds=tzseconds))
3857+
self.assertEqual(t.tzname(), tzname)
3858+
self.assertIs(type(t), self.theclass)
38463859

38473860
# Can produce inconsistent time
38483861
tstr, fmt = "+1234 UTC", "%z %Z"
@@ -3855,9 +3868,10 @@ def test_strptime(self):
38553868
# Produce naive time if no %z is provided
38563869
self.assertEqual(strptime("UTC", "%Z").tzinfo, None)
38573870

3858-
with self.assertRaises(ValueError): strptime("-2400", "%z")
3859-
with self.assertRaises(ValueError): strptime("-000", "%z")
3860-
with self.assertRaises(ValueError): strptime("z", "%z")
3871+
def test_strptime_errors(self):
3872+
for tzstr in ("-2400", "-000", "z"):
3873+
with self.assertRaises(ValueError):
3874+
self.theclass.strptime(tzstr, "%z")
38613875

38623876
def test_strptime_single_digit(self):
38633877
# bpo-34903: Check that single digit times are allowed.

0 commit comments

Comments
 (0)