Skip to content

Commit

Permalink
Merge #105
Browse files Browse the repository at this point in the history
Sorry Nicolas, I had to kind of mangled this
  • Loading branch information
uiri committed Jul 14, 2018
2 parents ec4b5bd + 4a39a29 commit 886e55c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/decoding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def tag(value):
elif isinstance(value, datetime.datetime):
return {'type': 'datetime', 'value': value.isoformat()
.replace('+00:00', 'Z')}
elif isinstance(value, datetime.time):
return {'type': 'time', 'value': value.strftime('%H:%M:%S.%f')}
assert False, 'Unknown type: %s' % type(value)


Expand Down
7 changes: 7 additions & 0 deletions toml/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def _getpath(p):
FNFError = IOError


TIME_RE = re.compile("([0-9]{2}):([0-9]{2}):([0-9]{2})(\.([0-9]{3,6}))?")


class TomlDecodeError(ValueError):
"""Base toml Exception / Error."""

Expand Down Expand Up @@ -687,6 +690,10 @@ def load_value(self, v, strictly_valid=True):
inline_object = self.get_empty_inline_table()
self.load_inline_object(v, inline_object)
return (inline_object, "inline_object")
elif TIME_RE.match(v):
h, m, s, _, ms = TIME_RE.match(v).groups()
time = datetime.time(int(h), int(m), int(s), int(ms) if ms else 0)
return (time, "time")
else:
parsed_date = _load_date(v)
if parsed_date is not None:
Expand Down
9 changes: 9 additions & 0 deletions toml/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def _dump_float(v):
return "{0:.16}".format(v).replace("e+0", "e+").replace("e-0", "e-")


def _dump_time(v):
utcoffset = v.utcoffset()
if utcoffset is None:
return v.isoformat()
# The TOML norm specifies that it's local time thus we drop the offset
return v.isoformat()[:-6]


class TomlEncoder(object):

def __init__(self, _dict=dict, preserve=False):
Expand All @@ -112,6 +120,7 @@ def __init__(self, _dict=dict, preserve=False):
int: lambda v: v,
float: _dump_float,
datetime.datetime: lambda v: v.isoformat().replace('+00:00', 'Z'),
datetime.time: _dump_time,
}

def get_empty_table(self):
Expand Down

0 comments on commit 886e55c

Please sign in to comment.