Skip to content

bpo-30802: strptime() directives %W and %U -- Make weekday optional and assume the first day of the week #11594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2251,11 +2251,12 @@ Notes:

(7)
When used with the :meth:`strptime` method, ``%U`` and ``%W`` are only used
in calculations when the day of the week and the calendar year (``%Y``)
are specified.
in calculations when the calendar year (``%Y``) is specified. Optionally the
weekday can also be specified, but if it is omitted then the first day of
the week is assumed.

(8)
Similar to ``%U`` and ``%W``, ``%V`` is only used in calculations when the
``%V`` is only used in calculations when the
day of the week and the ISO year (``%G``) are specified in a
:meth:`strptime` format string. Also note that ``%G`` and ``%Y`` are not
interchangeable.
Expand Down
10 changes: 6 additions & 4 deletions Lib/_strptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,14 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
year = 1900


# If we know the week of the year and what day of that week, we can figure
# out the Julian day of the year.
if julian is None and weekday is not None:
# If we know the year, the week, and the day of the week, we can figure out
# the Julian day of the year. If day of the week is not specified for the
# directives %U and %W we assume it to be the first day of the week.
if julian is None:
if week_of_year is not None:
week_starts_Mon = True if week_of_year_start == 0 else False
julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,
day_of_week = week_of_year_start if weekday is None else weekday
julian = _calc_julian_from_U_or_W(year, week_of_year, day_of_week,
week_starts_Mon)
elif iso_year is not None and iso_week is not None:
year, julian = _calc_julian_from_V(iso_year, iso_week, weekday + 1)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Wouter Bolsterlee
Gawain Bolton
Forest Bond
Gregory Bond
Sergey Bondarchuk
Médéric Boquien
Matias Bordese
Jonas Borgström
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`datetime.strptime` directives ``%U`` and ``%W`` no longer require a
weekday to be specified in the format string and will assume the first day
of the week if it is omitted (this is not the same as specifying the
weekday).