Rounding issues in py2/3

This commit is contained in:
Simeon Warner 2016-03-07 14:26:55 -05:00
parent d763d17cfc
commit c8d7ed9663
2 changed files with 12 additions and 0 deletions

View File

@ -32,6 +32,8 @@ def datetime_to_str(dt='now',no_fractions=False):
dt = time.time()
if (no_fractions):
dt = int(dt)
else:
dt += 0.0000001 # improve rounding to microseconds
return datetime.utcfromtimestamp(dt).isoformat() + 'Z'
def str_to_datetime(s, context='datetime'):

View File

@ -21,6 +21,16 @@ class TestW3cDatetime(unittest.TestCase):
self.assertEqual( datetime_to_str(60*60*24*365), "1971-01-01T00:00:00Z" )
#
self.assertEqual( datetime_to_str(1234567890), "2009-02-13T23:31:30Z" )
# Rounding issues
self.assertEqual( datetime_to_str(0.199999), "1970-01-01T00:00:00.199999Z" )
self.assertEqual( datetime_to_str(0.1999991), "1970-01-01T00:00:00.199999Z" )
self.assertEqual( datetime_to_str(0.1999992), "1970-01-01T00:00:00.199999Z" )
self.assertEqual( datetime_to_str(0.1999998), "1970-01-01T00:00:00.200000Z" )
self.assertEqual( datetime_to_str(0.1999999), "1970-01-01T00:00:00.200000Z" )
self.assertEqual( datetime_to_str(0.200000), "1970-01-01T00:00:00.200000Z" )
self.assertEqual( datetime_to_str(0.2000001), "1970-01-01T00:00:00.200000Z" )
self.assertEqual( datetime_to_str(0.2000009), "1970-01-01T00:00:00.200001Z" )
self.assertEqual( datetime_to_str(0.200001), "1970-01-01T00:00:00.200001Z" )
def test2_str_to_datetime(self):
"""Reading..."""