Skip to content

Compatability with datetime.astimezone() #570

@tahaum

Description

@tahaum

Experiencing something similar to what's mentioned in #472:

import datetime
now = datetime.datetime.now(datetime.timezone.utc)
now.astimezone().tzinfo

Outputs: datetime.timezone(datetime.timedelta(seconds=7200), 'CEST')

But:

import freezegun
with freezegun.freeze_time("2025-04-01T00:00"):
    frozen_now = datetime.datetime.now(datetime.timezone.utc)
frozen_now.astimezone().tzinfo

Outputs: tzlocal()

I would expect the behaviour to be equal here.

Edit: The reason we stumbled across this was attempting to do now.astimezone().tzinfo.tzname(None). When using freezegun this fails with the following traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".venv/lib/python3.12/site-packages/dateutil/tz/tz.py", line 238, in tzname
    return self._tznames[self._isdst(dt)]
                         ^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/dateutil/tz/tz.py", line 291, in _isdst
    dstval = self._naive_is_dst(dt)
             ^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/dateutil/tz/tz.py", line 259, in _naive_is_dst
    timestamp = _datetime_to_timestamp(dt)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/dateutil/tz/tz.py", line 1814, in _datetime_to_timestamp
    return (dt.replace(tzinfo=None) - EPOCH).total_seconds()
            ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'

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