Skip to content

Commit

Permalink
Merge 5fb2ae7 into e781e12
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Mar 8, 2019
2 parents e781e12 + 5fb2ae7 commit 8d8490f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 7 additions & 2 deletions tests/test_fields.py
Expand Up @@ -150,6 +150,7 @@ def test_datetime(self):

class MySchema(EmbeddedSchema):
a = fields.DateTimeField()
b = fields.LocalDateTimeField()

s = MySchema(strict=True)
data, _ = s.load({'a': datetime(2016, 8, 6)})
Expand All @@ -161,10 +162,14 @@ class MySchema(EmbeddedSchema):
with pytest.raises(ValidationError):
s.load({'a': "dummy"})

# Test DateTimeField rounds to milliseconds
# Test DateTimeField and LocalDateTimeField round to milliseconds
s = MySchema()
data, _ = s.load({'a': datetime(2016, 8, 6, 12, 30, 30, 123456)})
data, _ = s.load({
'a': datetime(2016, 8, 6, 12, 30, 30, 123456),
'b': datetime(2016, 8, 6, 12, 30, 30, 123456),
})
assert data['a'].microsecond == 123000
assert data['b'].microsecond == 123000

def test_strictdatetime(self):

Expand Down
8 changes: 6 additions & 2 deletions umongo/fields.py
Expand Up @@ -181,8 +181,12 @@ class LocalDateTimeField(BaseField, ma_fields.LocalDateTime):

def _deserialize(self, value, attr, data):
if isinstance(value, datetime):
return value
return super()._deserialize(value, attr, data)
ret = value
else:
ret = super()._deserialize(value, attr, data)
# MongoDB stores datetimes with a millisecond precision.
# Don't keep more precision in the object than in the database.
return ret.replace(microsecond=round(ret.microsecond, -3))


# class TimeField(BaseField, ma_fields.Time):
Expand Down

0 comments on commit 8d8490f

Please sign in to comment.