[issue35535] time.strptime() unexpectedly gives the same result for %U and %W for 2018
Paul Ganssle
report at bugs.python.org
Wed Dec 19 12:06:58 EST 2018
Paul Ganssle <p.ganssle at gmail.com> added the comment:
I don't really know what Python was doing in version 2.3, and I don't have immediate access to a Python 2.3 interpreter, but at least for %U and %W, datetime is calling the platform's `strftime` under the hood, so presumably if this is a bug it's a bug in glibc and the other providers of `strftime`.
Digging a bit more, %U and %W appear to be the the same for all Sundays if (and only if) the year starts on a Monday:
import calendar
from datetime import datetime
from dateutil import rrule
rr = rrule.rrule(freq=rrule.WEEKLY,
byweekday=rrule.SU,
dtstart=datetime(1900, 1, 1),
until=datetime(2100, 1, 1))
for dt in rr:
is_same = dt.strftime("%U") == dt.strftime("%W")
year_starts_monday = calendar.weekday(dt.year, 1, 1) == 0
assert is_same == year_starts_monday
This seems to be the right behavior, because %U and %W count all days before their respective "first day of the week" as "week 0", and week 1 starts with the relevant day of the week. If the year starts with Monday, week 1 is 1 January - 7 January according to %W (year starts on Monday), and week 1 is 7 January - 13 January according to %U (year starts on Sunday), thus all Sundays will be in the same "week number" in both systems.
> %U is supposed to work with the week numbering system common (as I understand it) in North America, where (according to Wikipedia) week 1 begins on a Sunday, and contains both 1 January and the first Saturday of the year. While I am not familiar with that system, Excel 2016 is, and it reports
The documentation for %U says:
> Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
This means that week 1 would only contain the first Saturday of the month and January 1st on years that start on Sunday. The Python documentation is consistent with the man page for strftime(3): http://man7.org/linux/man-pages/man3/strftime.3.html
----------
versions: +Python 3.6, Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35535>
_______________________________________
More information about the Python-bugs-list
mailing list