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

Problem with timezone conversion when using pytz as tzinfo #107

Open
marinellirubens opened this issue Jul 11, 2023 · 1 comment
Open

Problem with timezone conversion when using pytz as tzinfo #107

marinellirubens opened this issue Jul 11, 2023 · 1 comment

Comments

@marinellirubens
Copy link

Hello, I'm having some problems with timezone conversions from "US/Eastern" to "UTC" but only when using the pytz as tzinfo creating a datetime object.

here is an example:
When I create the date using the timezone as UTC then converted to US/Eastern the conversion is done correctly without any issue, giving me a difference of 4 hour as it should

>>> datetime.datetime(2023, 7, 10, 0, 0, 0, tzinfo=pytz.timezone("UTC")).astimezone(pytz.timezone("US/Eastern"))
datetime.datetime(2023, 7, 9, 20, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

But when I do the opposite conversion the calculation is incorrect, adding 56 minutes. as demonstrated below.

>>> datetime.datetime(2023, 7, 9, 20, 0, 0, tzinfo=pytz.timezone("US/Eastern")).astimezone(pytz.timezone("UTC"))
datetime.datetime(2023, 7, 10, 0, 56, tzinfo=<UTC>)

so when the conversion is done in one direction it consider the correct difference 4 hours, but on the other direction if 4:56.

On the other hand if I do the conversion using localize the conversion is done correctly, so there is a problem on the conversion when the object is created using the timezone from pytz (but just when converting to UTC not the opposite)

>>> pytz.timezone("UTC").localize(datetime.datetime(2023, 7, 10, 0, 0)).astimezone(pytz.timezone("US/Eastern"))
datetime.datetime(2023, 7, 9, 20, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
>>> pytz.timezone("US/Eastern").localize(datetime.datetime(2023, 7, 9, 20, 0, 0)).astimezone(pytz.timezone('UTC'))
datetime.datetime(2023, 7, 10, 0, 0, tzinfo=<UTC>)

I hope I have given enough information about the error, as I said is possible to do a workaround but would be interesting to correct that.

Thank you.

@krowbar
Copy link

krowbar commented Feb 28, 2024

See
https://blog.qax.io/pytz-and-strange-timezones/
and
https://stackoverflow.com/questions/24359540/why-doesnt-pytz-localize-produce-a-datetime-object-with-tzinfo-matching-the-t

If you look at the output of
>>> datetime.datetime(2023, 7, 9, 20, 0, 0, tzinfo=pytz.timezone("US/Eastern")) datetime.datetime(2023, 7, 9, 20, 0, tzinfo=<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>)

You notice the 'LMT-1 day, 19:04:00 STD' (leading to the missing minutes) compared to

>>> pytz.timezone("US/Eastern").localize(datetime.datetime(2023, 7, 9, 20, 0, 0)) datetime.datetime(2023, 7, 9, 20, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
which gives you the correct 'EDT-1 day, 20:00:00 DST'

Using python3.9+ you can use the base zoneinfo.ZoneInfo class as the tzinfo to datetimes.

>>> datetime.datetime(2023, 7, 9, 20, 0, 0, tzinfo=ZoneInfo("US/Eastern")).astimezone(ZoneInfo("UTC")) datetime.datetime(2023, 7, 10, 0, 0, tzinfo=zoneinfo.ZoneInfo(key="UTC"))
>>> datetime.datetime(2023, 7, 10, 0, 0, 0, tzinfo=ZoneInfo("UTC")).astimezone(ZoneInfo("US/Eastern")) datetime.datetime(2023, 7, 9, 20, 0, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern'))

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

2 participants