Skip to content

datetime calculations with zoneinfo don't work #91618

Closed
@jribbens

Description

@jribbens

Bug report

Doing calculations with datetime objects with zoneinfo timezones doesn't work - they completely ignore the timezone. For example:

>>> LONDON = zoneinfo.ZoneInfo('Europe/London')
>>> d0 = datetime.datetime(2022, 3, 27, 0, tzinfo=LONDON)
>>> print(d0)
2022-03-27 00:00:00+00:00
>>> print(d0 + datetime.timedelta(seconds=3600+1800))
2022-03-27 01:30:00+00:00

2022-03-27 01:30 is a time that doesn't exist in the Europe/London timezone.

Similarly:

>>> d1 = datetime.datetime(2022, 3, 27, 3, tzinfo=LONDON)
>>> print(d1 - d0)
3:00:00

The difference between d1 and d0 is 2 hours, not 3 hours.

I guess this might not be fixable, in which case at the very least the datetime module documentation needs to be updated to say that you simply cannot do arithmetic with datetime objects unless they are timezone-naive or using the UTC timezone.

Your environment

  • CPython versions tested on: 3.9
  • Operating system and architecture: Ubuntu 20.04.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions