Skip to content

issue from subtracting two datetime objects using the same timezone object, but the timezone object varies across Daylight Saving Time #105254

Closed

Description

@xwangphy

I open this issue thanks to the answer to my stackoverflow question:
https://stackoverflow.com/questions/76392906/why-does-timedelta-calculation-across-daylight-saving-time-differ-when-using-dat

The answer points out the issue:
"This seems to be unintended behavior due to the implementation of the sub() method of datetime (link to github). When you subtract two datetime objects, it checks if the time zones are the same object.

if self._tzinfo is other._tzinfo:
return base
Since the tz objects passed into t1 and t2 from dateutil is the same object, the datetime object assumes the timezone stays the same and does not take into account daylight savings.

This seems to be a bug and you should definitely open a github issue about it."

My question on stackoverflow is below:

I am learning the timezone and Daylight Saving Time in python. When I calculate the time difference around Daylight Saving time using two methods, they are supposed to give the same time difference, however, they give different results.

from datetime import datetime, timedelta, timezone
from dateutil import tz
  1. First method using dateutil.tz
eastern = tz.gettz('America/New_York')
t1 = datetime(2017, 3, 12, 1, 59, 59, tzinfo=eastern)
t2 = datetime(2017, 3, 12, 3, 0, 0, tzinfo=eastern)
print(t1.isoformat())
print(t2.isoformat())
print((t2-t1).seconds)

The outputs are:

2017-03-12T01:59:59-05:00

2017-03-12T03:00:00-04:00

3601

  1. Second method setting the timezones manually
EST = timezone(timedelta(hours=-5))
EDT = timezone(timedelta(hours=-4))
spring_ahead_159am = datetime(2017, 3, 12, 1, 59, 59, tzinfo = EST)
spring_ahead_3am = datetime(2017, 3, 12, 3, 0, 0, tzinfo = EDT)

print(spring_ahead_159am.isoformat())
print(spring_ahead_3am.isoformat())
print((spring_ahead_3am - spring_ahead_159am).seconds)

The outputs are:

2017-03-12T01:59:59-05:00

2017-03-12T03:00:00-04:00

1

As you can see t1 is equal to spring_ahead_159am, and t2 is equal to spring_ahead_3am. So how come (t2 - t1 ) != (spring_ahead_3am - spring_ahead_159am)?

I compared them and they are equal:

print(t1 == spring_ahead_159am)
print(t2 == spring_ahead_3am )

The outputs are:

True

True

Activity

  1. gaogaotiantian commented on Jun 3, 2023

    @gaogaotiantian
    Member

    This seems like a bug in dateutil rather than CPython. The timezone info it returns(eastern) has an incorrect implementation for utcoffset.

    print(t2.tzinfo.utcoffset(t2))
    print(spring_ahead_3am.tzinfo.utcoffset(t2))

    These two should not be equal because dst is set in t2.tzinfo (or eastern in this case).

  2. FObersteiner commented on Jun 4, 2023

    @FObersteiner

    I think this is a case of expected elapsed duration vs. what you get from Python's timedelta arithmetic is time elapsed on a wall clock. @pganssle has a blog post on this, Semantics of timezone-aware datetime arithmetic.

    EX:

    from datetime import datetime, timedelta, timezone
    from dateutil import tz
    
    def abs_delta(dt1, dt2):
        return dt2.astimezone(tz.UTC)-dt1.astimezone(tz.UTC)
    
    eastern = tz.gettz('America/New_York')
    t1 = datetime(2017, 3, 12, 1, 59, 59, tzinfo=eastern)
    t2 = datetime(2017, 3, 12, 3, 0, 0, tzinfo=eastern)
    print(t1.isoformat())
    print(t2.isoformat())
    # 2017-03-12T01:59:59-05:00
    # 2017-03-12T03:00:00-04:00
    
    print((t2-t1).seconds) # wall time difference !
    print(abs_delta(t1, t2).seconds)
    # 3601
    # 1

    --> not a bug, it's a feature.

  3. pganssle commented on Jun 4, 2023

    @pganssle
    Member

    Yes, this is not unintended behavior. For better or for worse, as @FObersteiner suggests, this is deliberate.

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

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions