[ python-Bugs-1646728 ] datetime.fromtimestamp fails with negative fractional times

SourceForge.net noreply at sourceforge.net
Tue Mar 6 18:59:03 CET 2007


Bugs item #1646728, was opened at 2007-01-28 21:21
Message generated for change (Settings changed) made by gvanrossum
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1646728&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
>Status: Closed
>Resolution: Fixed
Priority: 5
Private: No
Submitted By: James Henstridge (jhenstridge)
Assigned to: Nobody/Anonymous (nobody)
Summary: datetime.fromtimestamp fails with negative fractional times

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2007-03-06 12:59

Message:
Logged In: YES 
user_id=6380
Originator: NO

Georgbot backported this to 2.5.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-03-06 10:50

Message:
Logged In: YES 
user_id=6380
Originator: NO

Committed revision 54167.

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

----------------------------------------------------------------------

Comment By: James Henstridge (jhenstridge)
Date: 2007-03-05 19:03

Message:
Logged In: YES 
user_id=146903
Originator: YES

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.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-03-05 13:38

Message:
Logged In: YES 
user_id=6380
Originator: NO

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

----------------------------------------------------------------------

Comment By: James Henstridge (jhenstridge)
Date: 2007-03-05 05:23

Message:
Logged In: YES 
user_id=146903
Originator: YES

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.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2007-03-02 11:05

Message:
Logged In: YES 
user_id=6380
Originator: NO

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

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1646728&group_id=5470


More information about the Python-bugs-list mailing list