Repository: Scille/umongo

Issue:
ValueError: microsecond must be in 0..999999
Hello, all

I've got a new error after the v2.0.0 milestone, likely relating to #172. Here's the error text:

```python
  File "/Users/space/Documents/Programming/dt-blog-platform/api-server/db/objs.py", line 268, in commit
    self.updatedon = datetime.datetime.utcnow()
  File "/var/root/.local/share/virtualenvs/api-server-DI-zCP9m/lib/python3.6/site-packages/umongo/document.py", line 295, in __setattr__
    self._data.set(name, value, to_raise=AttributeError)
  File "/var/root/.local/share/virtualenvs/api-server-DI-zCP9m/lib/python3.6/site-packages/umongo/data_proxy.py", line 152, in set
    value = field._deserialize(value, name, None)
  File "/var/root/.local/share/virtualenvs/api-server-DI-zCP9m/lib/python3.6/site-packages/umongo/fields.py", line 245, in _deserialize
    return ret.replace(microsecond=round(ret.microsecond, -3))
ValueError: microsecond must be in 0..999999
```

My guess is something funky is going on either with my system, or maybe the round isn't working properly? Not too sure, but this should be a simple one.

The way I did this is set a `StrictDateTimeField` equal to `datetime.datetime.utcnow()` in an overriden `commit` method. Ex:

```python
@instance.register
class Foo(Document):
    name = fields.StringField()
    updatedon = fields.StrictDateTimeField()

    def commit(self, *args, **kwargs):
        self.updatedon = datetime.datetime.utcnow()
        return super(Foo, self).commit(*args, **kwargs)
```

This error happens when calling the commit method on the object.

Thanks in advance!

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/22655215/badge)](https://coveralls.io/builds/22655215)

Coverage increased (+0.05%) to 96.078% when pulling **c88369a8eef41d9913cffa1212ed7910d16a37b4 on 189_microsecond_overflow** into **3f5339442d1ec3ce66d4e41dabcdb8242bf2e684 on master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/22655215/badge)](https://coveralls.io/builds/22655215)

Coverage increased (+0.05%) to 96.078% when pulling **c88369a8eef41d9913cffa1212ed7910d16a37b4 on 189_microsecond_overflow** into **3f5339442d1ec3ce66d4e41dabcdb8242bf2e684 on master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/22655215/badge)](https://coveralls.io/builds/22655215)

Coverage increased (+0.05%) to 96.078% when pulling **c88369a8eef41d9913cffa1212ed7910d16a37b4 on 189_microsecond_overflow** into **3f5339442d1ec3ce66d4e41dabcdb8242bf2e684 on master**.


Patch:
diff --git a/umongo/fields.py b/umongo/fields.py
index 4a57703..2cb87e3 100644
--- a/umongo/fields.py
+++ b/umongo/fields.py
@@ -165,6 +165,18 @@ class FloatField(BaseField, ma_fields.Float):
     pass
 
 
+def _round_to_millisecond(datetime):
+    """Round a datetime to millisecond precision
+
+    MongoDB stores datetimes with a millisecond precision.
+    For consistency, use the same precision in the object representation.
+    """
+    microseconds = round(datetime.microsecond, -3)
+    if microseconds == 1000000:
+        return datetime.replace(microsecond=0) + dt.timedelta(seconds=1)
+    return datetime.replace(microsecond=microseconds)
+
+
 class DateTimeField(BaseField, ma_fields.DateTime):
 
     def _deserialize(self, value, attr, data):
@@ -172,9 +184,7 @@ class DateTimeField(BaseField, ma_fields.DateTime):
             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))
+        return _round_to_millisecond(ret)
 
 
 class LocalDateTimeField(BaseField, ma_fields.LocalDateTime):
@@ -184,9 +194,7 @@ class LocalDateTimeField(BaseField, ma_fields.LocalDateTime):
             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))
+        return _round_to_millisecond(ret)
 
 
 # class TimeField(BaseField, ma_fields.Time):


Test patch:
diff --git a/tests/test_fields.py b/tests/test_fields.py
index b2503c0..d4d2133 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -171,6 +171,19 @@ class TestFields(BaseTest):
         })
         assert data['a'].microsecond == 123000
         assert data['b'].microsecond == 123000
+        s = MySchema()
+        data, _ = s.load({
+            'a': dt.datetime(2016, 8, 6, 12, 59, 59, 999876),
+            'b': dt.datetime(2016, 8, 6, 12, 59, 59, 999876),
+        })
+        assert data['a'].hour == 13
+        assert data['b'].hour == 13
+        assert data['a'].minute == 0
+        assert data['b'].minute == 0
+        assert data['a'].second == 0
+        assert data['b'].second == 0
+        assert data['a'].microsecond == 0
+        assert data['b'].microsecond == 0
 
     def test_strictdatetime(self):