Skip to content

adding timedelta to datetime object is not timezone aware #67682

Closed

Description

@gilsho
mannequin
BPO 23494
Nosy @abalkin, @bitdancer, @4kir4

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 2015-02-27.20:11:35.748>
created_at = <Date 2015-02-20.19:46:14.444>
labels = ['type-bug', 'library']
title = 'adding timedelta to datetime object is not timezone aware'
updated_at = <Date 2015-03-02.23:53:20.458>
user = 'https://bugs.python.org/gilsho'

bugs.python.org fields:

activity = <Date 2015-03-02.23:53:20.458>
actor = 'akira'
assignee = 'none'
closed = True
closed_date = <Date 2015-02-27.20:11:35.748>
closer = 'belopolsky'
components = ['Library (Lib)']
creation = <Date 2015-02-20.19:46:14.444>
creator = 'gilsho'
dependencies = []
files = []
hgrepos = []
issue_num = 23494
keywords = []
message_count = 3.0
messages = ['236326', '236331', '237089']
nosy_count = 4.0
nosy_names = ['belopolsky', 'r.david.murray', 'akira', 'gilsho']
pr_nums = []
priority = 'normal'
resolution = 'third party'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue23494'
versions = ['Python 2.7']

Activity

  1. gilsho commented on Feb 20, 2015

    gilshomannequin
    MannequinAuthor

    I encountered a strange bug involving timezone aware datetime objects and timedelta objects.

    The crux of the matter is that daylight savings time is considered a different timezone, and therefore the timezone of a datetime object is date dependent. However, adding a timedelta object to a datetime object seems to do the simple thing which is preserve the timezone object. The bug manifests itself whenever adding a timedelta object crosses a daylight savings time boundary.

    The following code illustrates the problem. Note that the transition between PDT (Pacific daylight time) and PST (Pacific savings time) occurs on March 9th (ish)

    >>> from datetime import datetime, timedelta
    >>> import pytz
    >>> tz = pytz.timezone('America/Los_Angeles')
    >>> before = tz.localize(datetime(year=2015, month=3, day=8))
    >>> before
    datetime.datetime(2015, 3, 8, 0, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
    >>> # notice PST timezone
    >>> after_right = tz.localize(datetime(year=2015, month=3, day=10))
    >>> after_right
    datetime.datetime(2015, 3, 10, 0, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)
    >>> # notice PDT timezone
    >>> after_wrong = before + timedelta(days=2)
    >>> after_wrong
    datetime.datetime(2015, 3, 10, 0, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
    >>> # when calculated this way, the timezone remains at PST
  2. added
    stdlib
    type-bug
    on Feb 20, 2015
  3. bitdancer commented on Feb 20, 2015

    @bitdancer
    Member

    the tzinfo object is responsible for handling daylight savings time. This looks like a bug in pytz.

  4. added
    stdlib
    and removed
    stdlib
    on Feb 20, 2015
  5. 4kir4 commented on Mar 2, 2015

    4kir4mannequin
    Mannequin

    pytz explicitly documents this case (crossing DST boundary). There is
    tz.normalize() method.

    the tzinfo object is responsible for handling daylight savings time. This looks like a bug in pytz.

    Are any of tzinfo methods even called during before + timedelta(days=2)?

    Also a DST transition is not the only reason the utc offset or tzname may
    change.

  6. 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

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions