My favorites | Sign in
Project Home Issues
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 17: Float Error When Calculating Fractional Seconds [Patch]
1 person starred this issue and may be notified of changes. Back to list
Status:  Fixed
Owner:  ----
Closed:  Oct 2013


Sign in to add a comment
 
Reported by bobbysmi...@gmail.com, Jul 22, 2010
In [38]: dt = iso8601.parse_date('2010-07-20 15:25:52.520701+00:00')
In [39]: str(dt)
Out[39]: '2010-07-20 15:25:52.520700+00:00'

Oh hey where did my usec go? 
The error has to do with float rounding.  A better solution is to transform the fractional seconds by a string -> int rather than a string -> float -> int transform.  

Here is how I corrected it, HTH, Russ
----
Index: iso8601.py
===================================================================
--- iso8601.py  (revision 8282)
+++ iso8601.py  (working copy)
@@ -96,7 +96,8 @@
     if groups["fraction"] is None:
         groups["fraction"] = 0
     else:
-        groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6)
+        #Ensure we get a single 6 digit integer to represent usec
+        groups["fraction"] = int(groups["fraction"].ljust(6,"0")[0:6])
     return datetime(int(groups["year"]), int(groups["month"]), int(groups["day"]),
         int(groups["hour"]), int(groups["minute"]), int(groups["second"]),
         int(groups["fraction"]), tz)



Jul 22, 2010
#1 bobbysmi...@gmail.com
sorry the revision number in the diff above is for a different repo (a trac plugin)
Apr 20, 2012
#2 timymc...@gmail.com
This does not round sub-microseconds properly (e.g. 1234567 becomes 123456, not 123457).  What about this?

-        groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6)
+        groups["fraction"] = int(round(float("0.%s" % groups["fraction"]) * 1e6))
Oct 18, 2013
Project Member #3 micktwomey
This is fixed in 0.1.5, though I'll probably be rewriting the fractional second handling at some point any way.

Thanks for the issue and the patch (I went a slightly different route with decimal).
Status: Fixed
Sign in to add a comment

Powered by Google Project Hosting