Skip to content

Difference between two datetimes does not account for DST #39828

Closed

Description

@llirik
mannequin
BPO 878424
Nosy @tim-one

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 = 'https://github.com/tim-one'
closed_at = <Date 2004-01-16.22:02:22.000>
created_at = <Date 2004-01-16.17:58:49.000>
labels = ['extension-modules', 'type-feature']
title = 'Difference between two datetimes does not account for DST'
updated_at = <Date 2004-01-16.22:02:22.000>
user = 'https://bugs.python.org/llirik'

bugs.python.org fields:

activity = <Date 2004-01-16.22:02:22.000>
actor = 'tim.peters'
assignee = 'tim.peters'
closed = True
closed_date = None
closer = None
components = ['Extension Modules']
creation = <Date 2004-01-16.17:58:49.000>
creator = 'llirik'
dependencies = []
files = []
hgrepos = []
issue_num = 878424
keywords = []
message_count = 2.0
messages = ['19715', '19716']
nosy_count = 2.0
nosy_names = ['tim.peters', 'llirik']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue878424'
versions = []

Activity

  1. llirik commented on Jan 16, 2004

    llirikmannequin
    MannequinAuthor

    Hello,

    I am having problems with using timezone aware datetime
    objects across DST shift within the same timezone.

    Citing documentation:

    ------
    If both are naive, or both are aware and have the same
    tzinfo member, the tzinfo members are ignored, and the
    result is a timedelta object t such that datetime2 + t
    == datetime1. No time zone adjustments are done in this
    case.

    If both are aware and have different tzinfo members,
    a-b acts as if a and b were first converted to naive
    UTC datetimes first. The result is
    (a.replace(tzinfo=None) - a.utcoffset()) -
    (b.replace(tzinfo=None) - b.utcoffset()) except that
    the implementation never overflows.
    -------

    The problem is that if both datetimes have the same
    tzinfo, they still may be in different "timezones" --
    one in DST and the other in standard time.

    Suppose I am using US/Eastern timezone, then 4-Apr-04
    is the date when DST starts. Lets create two datetimes
    one (lets call it d1) for 00:00 and the other (d2) for
    05:00. Now lets print the

    print d1, d2, d2-d1, d2.astimezone(UTC)-d1.astimezone(UTC)

    The result is

    2004-04-04 00:00:00-05:00 2004-04-04 05:00:00-04:00
    5:00:00 4:00:00

    Which is _very_ confusing! I think even if both
    datetimes share the same tzinfo object you still have
    to calculate it in "aware", not "naive" way.

    Kind regards,
    Kirill Lapshin

  2. added
    extension-modules
    type-feature
    on Jan 16, 2004
  3. added
    extension-modules
    type-feature
    on Jan 16, 2004
  4. tim-one commented on Jan 16, 2004

    @tim-one
    Member

    Logged In: YES
    user_id=31435

    Since it's working as documented and as designed, this
    isn't "a bug", so I'm closing it. You may want to reopen it as
    a feature request, but since it's arguing for an incompatible
    change in design I don't expect you'll make progress with it.

    The original design of the module was debated here:

    http://zope.org/Members/fdrake/DateTimeWiki/

    Studying the example on the TimeZoneInfo page leaves no
    doubt that Guido intended this behavior: if you want to mess
    with DST, fine, but then you have to convert to UTC and
    back yourself.

    As the NaiveTime page says, there are (at least) three
    distinct behaviors different apps want for time arithmetic.
    Guido picked one as the default, but left it possible to get the
    others. Of course anyone who thinks exactly one of them is
    the obvious behavior finds all the others "_very_ confusing"
    <wink>.

    FWIW, I originally *implemented* datetime to work the way
    you expect. People complained about that too. For example,
    one early experimenter set up a 3pm weekly appointment, in
    their home time zone, and was baffled to see the hour shift
    when the week happened to cross a DST boundary (they
    expected that adding 7*24 hours would "move it a week" --
    or, IOW, they clearly wanted naive time in this case, but
    wanted to attach a time zone anyway so they could see the
    appointment as a local time in whatever timezone they
    happened to traveling in when the appointment came due).

    So one size doesn't fit all, and the default size doesn't fit you
    today. You have to do more work, then. I'm personally
    happy to convert to UTC by hand as needed, but it's also
    possible to define a datetime subclass and redefine
    subtraction to work the way you want it to work.

  5. transferred this issue fromon Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

extension-modulestype-feature

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions