Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 423feea

Browse files
Issue #23136: _strptime now uniformly handles all days in week 0, including
Jan 30 of previous year. Based on patch by Jim Carroll.
1 parent 56cefa6 commit 423feea

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

Lib/_strptime.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,9 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
348348
# though
349349
week_of_year = -1
350350
week_of_year_start = -1
351-
# weekday and julian defaulted to -1 so as to signal need to calculate
351+
# weekday and julian defaulted to None so as to signal need to calculate
352352
# values
353-
weekday = julian = -1
353+
weekday = julian = None
354354
found_dict = found.groupdict()
355355
for group_key in found_dict.keys():
356356
# Directives not explicitly handled below:
@@ -452,14 +452,14 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
452452
year = 1900
453453
# If we know the week of the year and what day of that week, we can figure
454454
# out the Julian day of the year.
455-
if julian == -1 and week_of_year != -1 and weekday != -1:
455+
if julian is None and week_of_year != -1 and weekday is not None:
456456
week_starts_Mon = True if week_of_year_start == 0 else False
457457
julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
458458
week_starts_Mon)
459459
# Cannot pre-calculate datetime_date() since can change in Julian
460460
# calculation and thus could have different value for the day of the week
461461
# calculation.
462-
if julian == -1:
462+
if julian is None:
463463
# Need to add 1 to result since first day of the year is 1, not 0.
464464
julian = datetime_date(year, month, day).toordinal() - \
465465
datetime_date(year, 1, 1).toordinal() + 1
@@ -469,7 +469,7 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
469469
year = datetime_result.year
470470
month = datetime_result.month
471471
day = datetime_result.day
472-
if weekday == -1:
472+
if weekday is None:
473473
weekday = datetime_date(year, month, day).weekday()
474474
# Add timezone info
475475
tzname = found_dict.get("Z")

Lib/test/test_strptime.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,24 @@ def test_helper(ymd_tuple, test_reason):
494494
test_helper((2006, 12, 31), "Last Sunday of 2006")
495495
test_helper((2006, 12, 24), "Second to last Sunday of 2006")
496496

497+
def test_week_0(self):
498+
def check(value, format, *expected):
499+
self.assertEqual(_strptime._strptime_time(value, format)[:-1], expected)
500+
check('2015 0 0', '%Y %U %w', 2014, 12, 28, 0, 0, 0, 6, -3)
501+
check('2015 0 0', '%Y %W %w', 2015, 1, 4, 0, 0, 0, 6, 4)
502+
check('2015 0 1', '%Y %U %w', 2014, 12, 29, 0, 0, 0, 0, -2)
503+
check('2015 0 1', '%Y %W %w', 2014, 12, 29, 0, 0, 0, 0, -2)
504+
check('2015 0 2', '%Y %U %w', 2014, 12, 30, 0, 0, 0, 1, -1)
505+
check('2015 0 2', '%Y %W %w', 2014, 12, 30, 0, 0, 0, 1, -1)
506+
check('2015 0 3', '%Y %U %w', 2014, 12, 31, 0, 0, 0, 2, 0)
507+
check('2015 0 3', '%Y %W %w', 2014, 12, 31, 0, 0, 0, 2, 0)
508+
check('2015 0 4', '%Y %U %w', 2015, 1, 1, 0, 0, 0, 3, 1)
509+
check('2015 0 4', '%Y %W %w', 2015, 1, 1, 0, 0, 0, 3, 1)
510+
check('2015 0 5', '%Y %U %w', 2015, 1, 2, 0, 0, 0, 4, 2)
511+
check('2015 0 5', '%Y %W %w', 2015, 1, 2, 0, 0, 0, 4, 2)
512+
check('2015 0 6', '%Y %U %w', 2015, 1, 3, 0, 0, 0, 5, 3)
513+
check('2015 0 6', '%Y %W %w', 2015, 1, 3, 0, 0, 0, 5, 3)
514+
497515

498516
class CacheTests(unittest.TestCase):
499517
"""Test that caching works properly."""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #23136: _strptime now uniformly handles all days in week 0, including
22+
Jan 30 of previous year. Based on patch by Jim Carroll.
23+
2124
- Issue #23700: Iterator of NamedTemporaryFile now keeps a reference to
2225
NamedTemporaryFile instance. Patch by Bohuslav Kabrda.
2326

0 commit comments

Comments
 (0)