(no title)
ubernostrum | 7 years ago
Examples:
'%G/%m' is illegal; it contains %G without %V, and does not contain a weekday format code. Attempting to call strptime() with this format raises ValueError.
'%V/%u' is illegal; it contains a weekday format, but has %V without %G. Raises ValueError.
'%G/%V' is illegal; it contains both %G and %V, but does not contain a weekday format code. Raises ValueError.
'%G/%V/%u' is legal; it contains both %G and %V, and contains a weekday format code.
'%G/%V/%w' is legal; it contains %G and %V and a weekday format code. It's a bad idea, though, because %w numbers days 0-6 starting Sunday, while ISO (%u) numbers them 1-7 starting Monday.
If you need to work with ISO week date formats for some reason, you should stick to one of these two format strings:
'%G-W%V-%u'
or
%GW%V%u
The date of this comment (December 26, 2018) comes out as either '2018-W52-3' or '2018W523' using those format strings.
bonzini|7 years ago
ubernostrum|7 years ago
strptime() produces a datetime object, which consists of year, month, day, hour, minute, second, microsecond, time zone, fold. If you do something like "2008-12" with format "%Y-%m", strptime() will fill in the remaining arguments with day=1 and all time components set to zero, so what you get is datetime(year=2018, month=12, day=1, hour=0, minute=0, second=0, microsecond=0).
That works because it's unambiguous -- there aren't multiple possible numbering schemes for the day of the month in the strptime() formatting options.
But there are multiple possible numbering schemes for the day of the week, which means a year + week with no day-of-week format code is ambiguous. Worse, the two options don't even share a start: one of them begins numbering at 0 (Sunday) and the other at 1 (Monday).
So I'd guess the insistence on a day-of-week format code is to force you to indicate which day-numbering scheme you want, in order to avoid the possible ambiguity.
(and you might think it's reasonable to assume if someone uses ISO year + ISO week number, they'd also want ISO day-of-week number, but we're talking about dates and times here, and "reasonable" left the building a long time ago)