rtoml icon indicating copy to clipboard operation
rtoml copied to clipboard

Millisecond precision dump and load inconsistency

Open himbeles opened this issue 4 years ago • 0 comments

The following code, exporting and reimporting a timestamp with millisecond precision fails:

from datetime import datetime
import rtoml

t = datetime.fromisoformat("2020-05-25T12:00:01.123450")
d = {"t": t}
dumped = rtoml.dumps(d)
loaded = rtoml.loads(dumped)
assert loaded == d

This is because the last subsecond digit is not dumped to string and load parsing fails with only 5 fractional digits. If the last digit is not zero, it works. Tested on macOS and Windows Python3.9.

Here are three unit tests covering passing and failing scenarios, including a passing test using the toml package instead.

from datetime import datetime
import rtoml
    
def test_date_milliseconds_passing():
    """passes, for 6 significant subsecond digits
    """
    t = datetime.fromisoformat("2020-05-25T12:00:01.123456")
    d = {"t": t}
    dumped = rtoml.dumps(d)
    loaded = rtoml.loads(dumped)
    assert loaded == d

def test_date_milliseconds_failing():
    """ 
    fails because last subsecond digit is not dumped to string and load parsing fails with only 5 fractional digits. 
    """
    t = datetime.fromisoformat("2020-05-25T12:00:01.123450")
    d = {"t": t}
    dumped = rtoml.dumps(d)
    loaded = rtoml.loads(dumped)
    assert loaded == d


def test_date_milliseconds_passing_toml_package():
    """ passes using toml package    
    """
    import toml
    t = datetime.fromisoformat("2020-05-25T12:00:01.123450")
    d = {"t": t}
    dumped = toml.dumps(d)
    loaded = toml.loads(dumped)
    assert loaded == d

himbeles avatar May 25 '21 11:05 himbeles