top | item 18763138

(no title)

ubernostrum | 7 years ago

A lot of languages take their date/time formatting from C strftime (and quite a few simply use light wrappers around actual strftime), where the format code for ISO year is %G.

And FWIW, Python's (strftime-based) datetime library won't let you mix ISO and non-ISO format codes. Trying to use %G with %m, for example, raises an exception, as does trying to use %Y with %V (%V is the ISO week number format code).

discuss

order

ubernostrum|7 years ago

And just to clarify a bit: the specific restriction Python imposes is that if a strptime() format string contains one of %G (ISO year) or %V (ISO week number), it must also contain the other one, and must contain a day-of-week format code (%A, %a, %u, or %w).

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

What is the rationale for forcing the presence of the day of week? It seems plausible that a weekly report, generated every Sunday for the previous week, would have %GW%V as the title. Seems more correct than using %V together with %w at least.