Skip to content

Problem with timezone conversion when using pytz as tzinfo #107

@marinellirubens

Description

@marinellirubens

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions