Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser fails to regonize timezones "A" and "P" #1246

Open
DB-37 opened this issue Dec 2, 2022 · 0 comments
Open

Parser fails to regonize timezones "A" and "P" #1246

DB-37 opened this issue Dec 2, 2022 · 0 comments

Comments

@DB-37
Copy link

DB-37 commented Dec 2, 2022

What is happening

For context, some timezones are named with a single letter (A,B,C,D,..,Z) (cf. https://www.timeanddate.com/time/zones/a or https://www.timeanddate.com/time/zones/p)
The parser generally recognizes them correctly as timezones:

In [1]: from dateutil import parser
In [2]: date = parser.parse("Thu Jun 02 11:56:53 B 2011", tzinfos={"B": 2*3600})
In [3]: date
Out[3]: datetime.datetime(2011, 6, 2, 11, 56, 53)
In [4]: date.isoformat()
Out[4]: '2011-06-02T11:56:53+02:00'

However, it seems that timezones A and P are recognized as aliases for AM and PM:

In [3]: parser.parse("Thu Jun 02 12:56:53 A 2011", tzinfos={"A": 3600}).isoformat() # Noon in UTC+1
Out[3]: '2011-06-02T00:56:53' # Midnight in UTC

In [4]: parser.parse("Thu Jun 02 12:56:53 P 2011", tzinfos={"P": -3*3600}).isoformat()
Out[4]: '2011-06-02T12:56:53'

This means that date using 24 hour format in these timezones are broken :

In [5]: parser.parse("Thu Jun 02 13:56:53 A 2011")
---------------------------------------------------------------------------
ParserError                               Traceback (most recent call last)
Cell In[5], line 1
----> 1 parser.parse("Thu Jun 02 13:56:53 A 2011")

File ~/.virtualenvs/tmp-9cee89e9c581522/lib/python3.10/site-packages/dateutil/parser/_parser.py:1368, in parse(timestr, parserinfo, **kwargs)
   1366     return parser(parserinfo).parse(timestr, **kwargs)
   1367 else:
-> 1368     return DEFAULTPARSER.parse(timestr, **kwargs)

File ~/.virtualenvs/tmp-9cee89e9c581522/lib/python3.10/site-packages/dateutil/parser/_parser.py:643, in parser.parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    640 res, skipped_tokens = self._parse(timestr, **kwargs)
    642 if res is None:
--> 643     raise ParserError("Unknown string format: %s", timestr)
    645 if len(res) == 0:
    646     raise ParserError("String does not contain a date: %s", timestr)

ParserError: Unknown string format: Thu Jun 02 13:56:53 A 2011

What is the result

Dates expressed in A and P timezones are not parsed correctly and can even raise exceptions

What is expected

Dates expressed in A and P timezones are parsed correctly and, if tzinfo is supplied, are offseted correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant