[issue32497] datetime.strptime creates tz naive object from value containing a tzname

Paul Ganssle report at bugs.python.org
Fri Jan 5 11:55:21 EST 2018


Paul Ganssle <p.ganssle at gmail.com> added the comment:

This is essentially what the `tzinfos` argument to `dateutil.parser.parse` does. I do think something *like* this is the only reasonable way to handle %Z->tzinfo mappings.

In `dateutil` (https://dateutil.readthedocs.io/en/latest/parser.html#dateutil.parser.parse), you can either pass a mapping or callable. Most of the problems we have in dateutil relate to the fact that we're both inferring what should or should not be interpreted as a time zone *and* passing it to the mapping or callable. Given that the first problem is solved by the format specifier already having an option for %Z, the implementation of this would be much easier.

I think the options for how this could be implemented are:

1. Mapping only
2. Callable only
3. Mapping or callable

Callable-only will probably lead to plenty of problems, since there's *already* a problem in this bug report, which is that `pytz.timezone` evidently doesn't do what Arjan thinks it does, because that function only *happens* to work. It would not work with, say, `CST` or `PST`. That said, callable is the most versatile way to do it, and if we don't include it, then people will probably end up having to work around it by creating mappings whose `.get` calls arbitrary functions.

#1 is probably the least convenient and #3 is the most convenient. Either way, I'd say that the primary documented interface should be mappings, since that's least error-prone (these mappings could be curated by third party libraries for a given local context). An advantage of using mappings is that if we ever have a C implementation of strptime, it can have a fast evalution path for when the mapping is a `Dict`.

----------
nosy: +belopolsky, p-ganssle

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32497>
_______________________________________


More information about the Python-bugs-list mailing list