In Python 3.11+ datetime.datetime.fromisoformat( accepts any number of decimal places in the seconds component, extra digits are truncated (not rounded)

>>> datetime.datetime.fromisoformat('2011-11-04T00:05:23.1234567Z')
datetime.datetime(2011, 11, 4, 0, 5, 23, 123456, tzinfo=datetime.timezone.utc)

gist.github.com/moreati/85de3f #TIL #Python

Follow

@alex Yes, prior to 3.11, we were only accepting formats that could be generated from `datetime.isoformat`. In 3.11, we added the ability to parse most valid ISO 8601 formats, including many that are acceptable only "by agreement".

I don't recall if we had a principled reason for choosing truncation rather than rounding.

@pganssle TBC it's an "oo nice" TIL. The truncation bit was more a footnote. Thank you for adding it.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.