Skip to content

incorrect timedelta yielded by two on-the-fly nows subtraction #52889

Closed

Description

@mykhal
mannequin
BPO 8643
Nosy @mdickinson

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 2010-05-07.11:33:24.666>
created_at = <Date 2010-05-07.11:11:16.101>
labels = ['invalid', 'type-bug', 'library']
title = 'incorrect timedelta yielded by two on-the-fly nows subtraction'
updated_at = <Date 2010-05-07.12:15:43.140>
user = 'https://bugs.python.org/mykhal'

bugs.python.org fields:

activity = <Date 2010-05-07.12:15:43.140>
actor = 'mark.dickinson'
assignee = 'none'
closed = True
closed_date = <Date 2010-05-07.11:33:24.666>
closer = 'mark.dickinson'
components = ['Library (Lib)']
creation = <Date 2010-05-07.11:11:16.101>
creator = 'mykhal'
dependencies = []
files = []
hgrepos = []
issue_num = 8643
keywords = []
message_count = 4.0
messages = ['105190', '105192', '105194', '105196']
nosy_count = 2.0
nosy_names = ['mark.dickinson', 'mykhal']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue8643'
versions = ['Python 2.5', 'Python 3.1']

Activity

  1. mykhal commented on May 7, 2010

    mykhalmannequin
    MannequinAuthor

    now() - now() from datetime.datetime produces not-nearly-zero timedelta:

    >>> import datetime
    >>> (datetime.datetime.now() - datetime.datetime.now()).seconds
    86399

    (i can't in the moment figure out why this is happening, sice the datetime library is written in C)

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on May 7, 2010
  3. changed the title [-]incorrect timedelta yielded by two on-the-fly nows[/-] [+]incorrect timedelta yielded by two on-the-fly nows subtraction[/+] on May 7, 2010
  4. mdickinson commented on May 7, 2010

    @mdickinson
    Member

    This is correct behaviour. Note that .seconds is only one of the three attributes that contribute to the value of a timedelta: the others are .microseconds and .days. A timedelta is normalised in such a way that td.seconds and td.microseconds are always nonnegative; this means that td.days will be strictly negative for negative timedeltas. In your case the timedelta will have a .days attribute of -1, and the total time represented (e.g., in seconds, 86400.0 * td.days + td.seconds + 0.000001 * td.microseconds) will be small and negative.

    Here's what I get on my system (in the py3k branch):

    Python 3.2a0 (py3k:80840:80842, May  7 2010, 12:29:35) 
    [GCC 4.2.1 (Apple Inc. build 5659)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import datetime
    >>> td = datetime.datetime.now() - datetime.datetime.now()
    >>> td.microseconds, td.seconds, td.days
    (998977, 86399, -1)
    >>> print(td)
    -1 day, 23:59:59.998977
    >>> 1e-6 * td.microseconds + td.seconds + 86400 * td.days
    -0.0010230000043520704
    >>> td.total_seconds() # new in Python 2.7, 3.2
    -0.0010230000043520704
  5. mykhal commented on May 7, 2010

    mykhalmannequin
    MannequinAuthor

    ok, my fault, i should have tried

    >>> (abs(datetime.datetime.now() - datetime.datetime.now())).seconds
    0

    sorry :)

  6. mdickinson commented on May 7, 2010

    @mdickinson
    Member

    Hmm. My example did make me realize that the total_seconds() method isn't as accurate as it could be. I'll open another issue for this.

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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions