gukoff / dtparse

Fast datetime parser for Python written in Rust
MIT License
73 stars 2 forks source link

Error parsing timezones other than "Z" or "+00:00" #15

Open sscherfke opened 2 years ago

sscherfke commented 2 years ago

I am not able to parse timezones other an Z/+00:00 with dtparse 1.3.2.

>>> dtparse.parse("2021-05-04 13:37:00+00:00", "%Y-%m-%d %H:%M:%S%:z")
datetime.datetime(2021, 5, 4, 13, 37)
>>> dtparse.parse("2021-05-04 13:37:00+10:00", "%Y-%m-%d %H:%M:%S%:z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: no possible date and time matching input

>>> dtparse.parse("2021-05-04T13:37:00Z", "%+")
datetime.datetime(2021, 5, 4, 13, 37)
>>> dtparse.parse("2021-05-04T13:37:00+00:00", "%+")
datetime.datetime(2021, 5, 4, 13, 37)
>>> dtparse.parse("2021-05-04T13:37:00+01:00", "%+")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: no possible date and time matching input

I'd also expect that tzinfo is attached to the datetime for Z/+00:00:

>>> dtparse.parse("2021-05-04 13:37:00+00:00", "%Y-%m-%d %H:%M:%S%:z")
datetime.datetime(2021, 5, 4, 13, 37, tzinfo=datetime.timezone.utc)
>>> dtparse.parse("2021-05-04T13:37:00Z", "%+")
datetime.datetime(2021, 5, 4, 13, 37, tzinfo=datetime.timezone.utc)
>>> dtparse.parse("2021-05-04T13:37:00+00:00", "%+")
datetime.datetime(2021, 5, 4, 13, 37, tzinfo=datetime.timezone.utc)

Did I miss something or is this a bug or even expected behavior?

gukoff commented 2 years ago

Hi! You're right, it only parses UTC. See the call to the Rust library: https://github.com/gukoff/dtparse/blob/master/src/lib.rs#L20

We could make parsing timezone-aware. I think fixed offsets should be easy to support.

It's harder to support real-world timezones (PST, EST, Europe/Berlin, etc), as their definitions change over the time, and one must think how to bring chrono-tz and pytz together.

Is your usecase strictly fixed offsets?

sscherfke commented 2 years ago

Fixed offsets would be a huge benefit and cover most use cases. :)