Search code examples
pythondatetimepython-dateutil

dateutil not parsing my timezone correctly


I'm trying to parse a string to get the datetime object. I'm using the dateutil library to parse my input string.

Tried this code

from dateutil.parser import parse

datetime_str = '2023-06-29T00:15:53Z'      #Z implies UTC timezone
tzinfos = {"Z": -18000}                    #I want the EST timezone which is -5. Hence, -180000
parse(datetime_str, ignoretz=False, tzinfos=tzinfos)

But this gives me the output-

datetime.datetime(2023, 6, 29, 0, 15, 53, tzinfo=tzlocal())

Why is it not following my timezone instructions? My tzinfos dictionary specifically maps 'Z' to an offset of -18000. But it still shows me tzlocal().

**** UPDATE ****

Okay, I think I have zeroed in on the problem. If I replace 'Z' with something like 'BRST' or 'CST', and correspondingly update the tzinfos dictionary, the tzinfos are updated successfully. So why is it giving me a problem with 'Z'. Is it because dateutil.parser doesn't like single chars timezones?


Solution

  • UTC timezone abbreviations are special cased in the source code (UTC, GMT, Z, z): See here and here

    You can subclass parserinfo and override UTCZONE if you really need to make Z mean something other than UTC, but I would probably suggest to not do that.