danielrichman / strict-rfc3339

Strict, simple, lightweight RFC3339 functions
GNU General Public License v3.0
31 stars 13 forks source link

Assertion error in strict_rfc3339.timestamp_to_rfc3339_utcoffset #3

Closed jbryan closed 8 years ago

jbryan commented 8 years ago

On certain floating point timestamps, I observe the following (tested in python 3.4 on 64bit ubuntu):

t = 1460691032.9999998
strict_rfc3339.timestamp_to_rfc3339_utcoffset(t)

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-15-6eaf25ac9712> in <module>()
----> 1 strict_rfc3339.timestamp_to_rfc3339_utcoffset(t)

/home/jbryan/workspace/fx/build/venv/lib/python3.4/site-packages/strict_rfc3339.py in timestamp_to_rfc3339_utcoffset(timestamp)
    124     datestring += "Z"
    125 
--> 126     assert abs(rfc3339_to_timestamp(datestring) - timestamp) < 0.000001
    127     return datestring
    128 

Other values of t that produce the exception are:

danielrichman commented 8 years ago

:D This is terrible. Thank you for the bug report!

It looks like with probability 1 in 2 million (making some assumptions)---i.e., if the fractional seconds part is > 999999.5us---then some dodgy rounding happens and it incorrectly produces "...HH:MM:SS.1". I rewrote the bit that splits a timestamp into seconds and microseconds.

I believe I've fixed it in 78afdfc346293cfe934c3fc80c347e69691ffd05, and I've released version 0.7 with that change.