Issue 17:  Float Error When Calculating Fractional Seconds [Patch]  
1 person starred this issue and may be notified of changes.  Back to list 
In [38]: dt = iso8601.parse_date('20100720 15:25:52.520701+00:00') In [39]: str(dt) Out[39]: '20100720 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
Apr 20, 2012
This does not round submicroseconds 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
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 