Skip to content

timedelta produced by datetime.__sub__ does not take Daylight Savings Time into account #75350

Closed

Description

@joshuarli
mannequin
BPO 31167
Nosy @bitdancer, @joshuarli

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-08-10.04:02:18.754>
created_at = <Date 2017-08-10.02:04:40.861>
labels = ['3.7', 'invalid', 'type-bug', 'library']
title = 'timedelta produced by datetime.__sub__ does not take Daylight Savings Time into account'
updated_at = <Date 2017-08-10.04:02:18.745>
user = 'https://github.com/JoshuaRLi'

bugs.python.org fields:

activity = <Date 2017-08-10.04:02:18.745>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2017-08-10.04:02:18.754>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2017-08-10.02:04:40.861>
creator = 'JoshuaRLi'
dependencies = []
files = []
hgrepos = []
issue_num = 31167
keywords = []
message_count = 2.0
messages = ['300035', '300040']
nosy_count = 2.0
nosy_names = ['r.david.murray', 'JoshuaRLi']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue31167'
versions = ['Python 3.3', 'Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7']

Activity

  1. joshuarli commented on Aug 10, 2017

    joshuarlimannequin
    MannequinAuthor

    See my SO answer and the corresponding question for detail: https://stackoverflow.com/a/45602760/5348393

    Essentially, given two datetime.datetime instances t1 and t2, the following two syntactically different lines of code should be logically equivalent, but in fact differ by plus or minus one hour on Daylight Savings Time dates because datetime.datetime.__sub__ does not appear to take DST into account.

    t1.timestamp()-t2.timestamp()
    (t1-t2).total_seconds()

    I am not sure if this is by intentional design, or a behavioral bug.

  2. added
    3.7 (EOL)
    stdlib
    type-bug
    on Aug 10, 2017
  3. bitdancer commented on Aug 10, 2017

    @bitdancer
    Member

    It is by design. Read the footnote associated with the subtraction opertion on datetimes: after subtraction date2 + timedelta = date1, which implies that the subtraction ignores daylight savings transitions, since the addition does ("Note that no time zone adjustments are done even if the input is an aware object").

    Perhaps the reason why it is designed this way is menioned in the PEP; I haven't looked.

  4. transferred this issue fromon Apr 10, 2022
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

    3.7 (EOL)stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions