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

datetime.fromtimestamp fails with negative fractional times #44515

Closed
jamesh mannequin opened this issue Jan 29, 2007 · 13 comments
Closed

datetime.fromtimestamp fails with negative fractional times #44515

jamesh mannequin opened this issue Jan 29, 2007 · 13 comments
Labels
stdlib Python modules in the Lib dir

Comments

@jamesh
Copy link
Mannequin

jamesh mannequin commented Jan 29, 2007

BPO 1646728
Nosy @gvanrossum, @birkenfeld
Files
  • datetime.patch: patch + unittest
  • 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 2007-03-07.16:13:08.000>
    created_at = <Date 2007-01-29.02:21:31.000>
    labels = ['library']
    title = 'datetime.fromtimestamp fails with negative fractional times'
    updated_at = <Date 2007-03-07.16:13:08.000>
    user = 'https://bugs.python.org/jamesh'

    bugs.python.org fields:

    activity = <Date 2007-03-07.16:13:08.000>
    actor = 'georg.brandl'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2007-01-29.02:21:31.000>
    creator = 'jamesh'
    dependencies = []
    files = ['2287']
    hgrepos = []
    issue_num = 1646728
    keywords = []
    message_count = 13.0
    messages = ['31113', '31114', '31115', '31116', '31117', '31118', '31119', '31120', '31121', '31122', '31123', '31124', '31125']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'georg.brandl', 'jamesh', 'ocean-city']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1646728'
    versions = ['Python 2.5']

    @jamesh
    Copy link
    Mannequin Author

    jamesh mannequin commented Jan 29, 2007

    The datetime.fromtimestamp() function works fine with integer timestamps and positive fractional timestamps, but fails if I pass a negative fractional timestamp. For example:

    >>> import datetime
    >>> datetime.datetime.fromtimestamp(-1.05)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: microsecond must be in 0..999999

    It should return the same result as datetime.fromtimestamp(-1) - timedelta(seconds=.5).

    The same bug can be triggered in datetime.utcfromtimestamp().

    I have been able to reproduce this bug in Python 2.4.4 and Python 2.5 on Linux.

    @jamesh jamesh mannequin closed this as completed Jan 29, 2007
    @jamesh jamesh mannequin added the stdlib Python modules in the Lib dir label Jan 29, 2007
    @jamesh jamesh mannequin closed this as completed Jan 29, 2007
    @jamesh jamesh mannequin added the stdlib Python modules in the Lib dir label Jan 29, 2007
    @gvanrossum
    Copy link
    Member

    Looks like a bug in the conversion from floats to ints. Anyone care to track it down more precisely?

    @jamesh
    Copy link
    Mannequin Author

    jamesh mannequin commented Mar 5, 2007

    The problem seems to be in datetime_from_timestamp() from datetimemodule.c. It should probably be checking to see whether the microseconds value it calculates is negative, and adjust "timet" and "us" accordingly if so.

    @gvanrossum
    Copy link
    Member

    Attached is a fix. If this is to your liking I'll check it in.
    File Added: datetime.patch

    @jamesh
    Copy link
    Mannequin Author

    jamesh mannequin commented Mar 6, 2007

    I just tried the patch, and can confirm that it fixes the problem with datetime.fromtimestamp() and datetime.utcfromtimestamp(). The logic in the patch looks correct.

    @gvanrossum
    Copy link
    Member

    Committed revision 54167.

    I'm leaving this open until it's been backported to the 2.5 branch.

    @gvanrossum
    Copy link
    Member

    Georgbot backported this to 2.5.

    @birkenfeld
    Copy link
    Member

    Though, the new tests seem to fail on Windows (I noticed that only after backporting, since most other buildbot failures were due to the cmp/key problem in setup.py).

    @gvanrossum
    Copy link
    Member

    That's too bad. More details?

    @birkenfeld
    Copy link
    Member

    Not from me, no Windows around.

    @ocean-city
    Copy link
    Mannequin

    ocean-city mannequin commented Mar 7, 2007

    Hello, I'm user of Windows (Now building Python2.5 with VC6)
    I heard localtime() can only handle positive time_t on windows,
    so datetime.fromtimestamp() also fails for negative value.

    >>> datetime.datetime.fromtimestamp(-1.05)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: timestamp out of range for platform localtime()/gmtime() function
    >>> datetime.datetime.fromtimestamp(-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: timestamp out of range for platform localtime()/gmtime() function

    I'll attach workaround for unittest. Probably there is better way
    skip this test on non-negative platform though :-)

    Index: Lib/test/test_datetime.py
    ===================================================================

    --- Lib/test/test_datetime.py	(revision 54194)
    +++ Lib/test/test_datetime.py	(working copy)
    @@ -1428,9 +1428,17 @@
         def test_negative_float_fromtimestamp(self):
             # The result is tz-dependent; at least test that this doesn't
             # fail (like it did before bug 1646728 was fixed).
    +        try:
    +            self.theclass.fromtimestamp(-1)
    +        except ValueError: # cannot handle negative value
    +            return
             self.theclass.fromtimestamp(-1.05)
     
         def test_negative_float_utcfromtimestamp(self):
    +        try:
    +            self.theclass.utcfromtimestamp(-1)
    +        except ValueError: # cannot handle negative value
    +            return
             d = self.theclass.utcfromtimestamp(-1.05)
             self.assertEquals(d, self.theclass(1969, 12, 31, 23, 59, 58, 950000))

    @gvanrossum
    Copy link
    Member

    Thanks! I'm skipping these tests on Windows now.
    Committed revision 54209.

    Georgbot, would you be so kind... :-)

    @birkenfeld
    Copy link
    Member

    Certainly. Backported in rev. 54211.

    @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
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants