Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for datetime substract operation incorrect? #74701

Closed
RenHernndezRemedios mannequin opened this issue May 30, 2017 · 6 comments
Closed

Documentation for datetime substract operation incorrect? #74701

RenHernndezRemedios mannequin opened this issue May 30, 2017 · 6 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes docs Documentation in the Doc dir easy type-feature A feature request or enhancement

Comments

@RenHernndezRemedios
Copy link
Mannequin

RenHernndezRemedios mannequin commented May 30, 2017

BPO 30516
Nosy @abalkin, @vadmium
PRs
  • bpo-30516: Fix documentation issue with -timedelta in datetime #7348
  • [3.6] bpo-30516: Fix documentation issue with -timedelta in datetime (GH-7348) #8092
  • [3.7] bpo-30516: Fix documentation issue with -timedelta in datetime (GH-7348) #8093
  • 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 2018-07-04.23:05:50.649>
    created_at = <Date 2017-05-30.21:22:13.435>
    labels = ['easy', '3.8', 'type-feature', '3.7', 'docs']
    title = 'Documentation for datetime substract operation incorrect?'
    updated_at = <Date 2018-07-04.23:05:50.649>
    user = 'https://bugs.python.org/RenHernndezRemedios'

    bugs.python.org fields:

    activity = <Date 2018-07-04.23:05:50.649>
    actor = 'belopolsky'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2018-07-04.23:05:50.649>
    closer = 'belopolsky'
    components = ['Documentation']
    creation = <Date 2017-05-30.21:22:13.435>
    creator = 'Ren\xc3\xa9 Hern\xc3\xa1ndez Remedios'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30516
    keywords = ['patch', 'easy']
    message_count = 6.0
    messages = ['294787', '294802', '298963', '321061', '321066', '321067']
    nosy_count = 4.0
    nosy_names = ['belopolsky', 'docs@python', 'martin.panter', 'Ren\xc3\xa9 Hern\xc3\xa1ndez Remedios']
    pr_nums = ['7348', '8092', '8093']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue30516'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    @RenHernndezRemedios
    Copy link
    Mannequin Author

    RenHernndezRemedios mannequin commented May 30, 2017

    In the documentation for the supported arithmetic operations for a datetime object, there is the following note, among other:

    datetime2 = datetime1 - timedelta

    Comment:
    Computes the datetime2 such that datetime2 + timedelta == datetime1. As for addition, the result has the same tzinfo attribute as the input datetime, and no time zone adjustments are done even if the input is aware. This isn’t quite equivalent to datetime1 + (-timedelta), because -timedelta in isolation can overflow in cases where datetime1 - timedelta does not.

    While reading the source code for __sub__ operation I found in the first few lines:

    Line 1885:
    def __sub__(self, other):
    "Subtract two datetimes, or a datetime and a timedelta."
    if not isinstance(other, datetime):
    if isinstance(other, timedelta):
    return self + -other
    return NotImplemented

    Is the documentation in contradiction with the actual implementation?

    @vadmium
    Copy link
    Member

    vadmium commented May 31, 2017

    The C "_datetime" implementation seems to handle this as documented. But either way, the "timedelta" range is greater than the "datetime" range, so it seems to be just a difference in OverflowError messages, not a big practical problem:

    Python "datetime" implementation:
    >>> import sys
    >>> sys.modules["_datetime"] = None
    >>> from datetime import *
    >>> datetime.max - timedelta.max
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\Martin\AppData\Local\Programs\Python\Python35-32\lib\datetime.py", line 1741, in __sub__
        return self + -other
      File "C:\Users\Martin\AppData\Local\Programs\Python\Python35-32\lib\datetime.py", line 518, in __neg__
        -self._microseconds)
      File "C:\Users\Martin\AppData\Local\Programs\Python\Python35-32\lib\datetime.py", line 430, in __new__
        raise OverflowError("timedelta # of days is too large: %d" % d)
    OverflowError: timedelta # of days is too large: -1000000000
    
    C "_datetime" implementation:
    >>> from datetime import *
    >>> datetime.max - timedelta.max
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: date value out of range

    @abalkin
    Copy link
    Member

    abalkin commented Jul 24, 2017

    I agree. The documentation can be improved here. The note about x - y not being quite the same as x + (-y) belongs to the timedelta - timedelta operation. It should be removed from both date - timedelta and datetime-timedelta footnotes.

    @abalkin abalkin added docs Documentation in the Doc dir 3.7 (EOL) end of life labels Jul 24, 2017
    @csabella csabella added easy 3.8 only security fixes type-feature A feature request or enhancement labels Feb 2, 2018
    @abalkin
    Copy link
    Member

    abalkin commented Jul 4, 2018

    New changeset 5b6e49a by Alexander Belopolsky (Farhaan Bukhsh) in branch 'master':
    bpo-30516: Fix documentation issue with -timedelta in datetime (GH-7348)
    5b6e49a

    @abalkin
    Copy link
    Member

    abalkin commented Jul 4, 2018

    New changeset 55f39bd by Alexander Belopolsky (Miss Islington (bot)) in branch '3.6':
    bpo-30516: Fix documentation issue with -timedelta in datetime (GH-7348) (GH-8092)
    55f39bd

    @abalkin
    Copy link
    Member

    abalkin commented Jul 4, 2018

    New changeset a8bb18b by Alexander Belopolsky (Miss Islington (bot)) in branch '3.7':
    bpo-30516: Fix documentation issue with -timedelta in datetime (GH-7348) (GH-8093)
    a8bb18b

    @abalkin abalkin closed this as completed Jul 4, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes docs Documentation in the Doc dir easy type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants