[ python-Bugs-1478429 ] datetime.datetime.fromtimestamp ValueError. Rounding error

SourceForge.net noreply at sourceforge.net
Fri Apr 28 20:11:31 CEST 2006


Bugs item #1478429, was opened at 2006-04-28 10:37
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1478429&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.4
Status: Open
>Resolution: Accepted
Priority: 5
Submitted By: Erwin Bonsma (eriban)
>Assigned to: Georg Brandl (gbrandl)
Summary: datetime.datetime.fromtimestamp ValueError. Rounding error

Initial Comment:
The function datetime.datetime.fromtimestamp() can 
throw a ValueError when the timestamp is close to an 
integer value but not quite due to rounding errors. It 
then gives the following error: microsecond must be in 
0..999999

This can be seen by running the attached code (the 
values are taken from an actual event log), which 
gives the following output:


1146227423.0 -> 2006-04-28 14:30:23
1146227448.7 -> 2006-04-28 14:30:48.702000
1146227459.95 -> 2006-04-28 14:30:59.947000
1146227468.41 -> 2006-04-28 14:31:08.409000
1146227501.4 -> 2006-04-28 14:31:41.399000
1146227523.0 -> Error converting 1146227522.99999976
microsecond must be in 0..999999


Admittedly, I can work around the bug in this case, by 
summing the durations first, and calculating all times 
from "starttime" directly. Nevertheless, I think this 
is a bug in datetime, as it should work as long as the 
input time any floating point value within a given 
range (based on the date range that is supported).

Details of my Python environment:
Python 2.4.2 (#1, Feb  6 2006, 13:53:18) 
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-53)] on linux2


Cheers,

Erwin

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

>Comment By: Tim Peters (tim_one)
Date: 2006-04-28 14:11

Message:
Logged In: YES 
user_id=31435

Huh!  My comment got lost.  The patch looks good, but add 1
to `timet` instead of 1.0.  We don't know whether the C
time_t type is an integral or floating type, and using an
integer literal works smoothly for both.

For that matter, we don't know that time_t counts number of
seconds either (e.g., perhaps it counts number of
nanoseconds), but other code in Python assumes that it does,
so there's no special sin in assuming it does here too.

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

Comment By: Georg Brandl (gbrandl)
Date: 2006-04-28 13:32

Message:
Logged In: YES 
user_id=849994

Really attaching a patch now. ;)

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

Comment By: Georg Brandl (gbrandl)
Date: 2006-04-28 13:32

Message:
Logged In: YES 
user_id=849994

Attaching a patch correcting this issue. Please review.

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

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


More information about the Python-bugs-list mailing list