Skip to content

Adding a timedelta to an aware datetime object not resulting in the correct time being calculated #123487

Closed as not planned

Description

@ben-nour

Bug report

Bug description:

I am trying to understand why the time of the sydney_aedt object is not being adjusted to reflect the change in timezone offset:

import datetime
import zoneinfo

sydney_aest = datetime.datetime(2024, 8, 30, 15, 0, 0)
sydney_aest = sydney_aest.astimezone(zoneinfo.ZoneInfo("Australia/Sydney"))
print(sydney_aest)
sydney_aedt = sydney_aest + datetime.timedelta(days=90)
print(sydney_aedt)

Expected output:

2024-08-30 15:00:00+10:00
2024-11-28 16:00:00+11:00 # Clock moves forward an hour.

Actual output:

2024-08-30 15:00:00+10:00
2024-11-28 15:00:00+11:00 # Time is the same.

I would expected that with the change from AEST to AEDT the hour should move forward an hour however as you can see this is not the result.

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Activity

  1. added
    type-bug
    on Aug 30, 2024
  2. added
    stdlib
    extension-modules
    on Aug 30, 2024
  3. rruuaanng commented on Sep 8, 2024

    @rruuaanng
    Contributor

    I'm sorry that I don't have a mac, but I will test it on win.

  4. zuo commented on Oct 3, 2024

    @zuo
    Contributor

    The described behavior may be non-intuitive for some people (including me, at least at first sight), but it is intended and documented (see: Supported operations below https://docs.python.org/3/library/datetime.html#datetime.datetime.fold).

    See also: https://blog.ganssle.io/articles/2018/02/aware-datetime-arithmetic.html

  5. StanFromIreland commented on Mar 14, 2025

    @StanFromIreland
    Member

    "The result has the same tzinfo attribute as the input datetime, and datetime2 - datetime1 == timedelta after."

    This is the documented behavior as mentioned above. This issue should be closed.

    Duplicate where improvement of docs is under consideration: #116111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesstdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions