4 ms·
Can you elaborate on this? Parsing ISO 8601 formatted dates doesn't result in naive Python datetimes: >>> datetime.fromisoformat("2021-10-24T20:17:41+02:0
by Dunedan 5y ago
Can you elaborate on this?
Parsing ISO 8601 formatted dates doesn't result in naive Python datetimes:
>>> datetime.fromisoformat("2021-10-24T20:17:41+02:00")
datetime.datetime(2021, 10, 24, 20, 17, 41, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200)))
- NelsonMinar 5y agoYour example demonstrates I'm wrong with isoformat not supporting any timezone info. Sorry about that. Given it can handle +02:00 I wonder why they don't just add support for z and Z and instantly cover 95% of the world's JSON data?
- macintux 5y agoSorry, was on mobile and didn't elaborate. Was referring to datetime.datetime.now(); why can't it incorporate the local timezone?
- Dunedan 5y agoWell, it can, it's just not the default: >>> from datetime import datetime, timezone >>> datetime.now(tz=timezone.utc).astimezone() datetime.datetime(2021, 10, 25, 19, 58, 34, 51668, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200), 'CEST')) Or if you want to use a specific timezone: >>> from datetime import datetime >>> from zoneinfo import ZoneInfo >>> datetime.now(tz=ZoneInfo("Europe/Berlin")) datetime.datetime(2021, 10, 25, 19, 58, 34, 51668, tzinfo=zoneinfo.ZoneInfo(key='Europe/Berlin'))
- macintux 5y agoYes, but who’s more likely to get the code right to determine the system’s timezone: the programmer or the library? datetime.datetime.now() is by definition local to the system. What motivation is there to not include a timezone with the return value?