cpython icon indicating copy to clipboard operation
cpython copied to clipboard

datetime.strptime's %z should support +HH

Open rubenbaer opened this issue 1 year ago • 3 comments

Feature or enhancement

Proposal:

The %z directive supports the UTC offset in the form ±HHMM[SS[.ffffff]]. Other parsing functions like fromisoformat on the other hand support the full format ±HH[MM[SS[.ffffff]]]. So there is no strptime format that supports the fromisoformat.

from datetime import datetime
datetime.fromisoformat("2023-05-25T15:35:05.666+01")  # ok
datetime.strptime("2023-05-25T15:35:05.666+01", "%Y-%m-%dT%H:%M:%S.%f%z")  # nok

The same may apply to %:z (https://github.com/python/cpython/issues/121237)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-130390

rubenbaer avatar Dec 18 '24 17:12 rubenbaer

I'm ready to start working on solving this issue if no one else is against it. Also the second argument (format) in the third line has problems with the placement of :. I think you meant:

datetime.strptime("2023-05-25T15:35:05.666+01", "%Y-%m-%dT%H:%M:%S.%f%z")  # nok

And another example that %z will work in the form of HHMM. As I understand it we want to achieve the same result, but without using the HHMM format:

datetime.strptime("2023-05-25T15:35:05.666+0100", "%Y-%m-%dT%H:%M:%S.%f%z")  # ok

donbarbos avatar Feb 20 '25 21:02 donbarbos

Thank you for the corrections and your work on this issue. I updated the issue with the correct string.

rubenbaer avatar Mar 04 '25 08:03 rubenbaer

Implementing this for strptime is one thing, but what about strftime? This change does not conform to the 1989 C standard.

StanFromIreland avatar Jun 29 '25 11:06 StanFromIreland