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**.

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):

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!
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):
 

pip install -e .[dev]
parse_log_pytest
requirements.txt
bumpversion wheel flake8 tox coverage Sphinx pytest pytest-cov
apt-get update apt-get install -y gcc
3.7
requirements/base.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.13
Babel==2.14.0
bump2version==1.0.1
bumpversion==0.6.0
certifi @ file:///croot/certifi_1671487769961/work/certifi
charset-normalizer==3.4.1
coverage==7.2.7
distlib==0.3.9
dnspython==2.3.0
docutils==0.17.1
exceptiongroup==1.2.2
filelock==3.12.2
flake8==5.0.4
idna==3.10
imagesize==1.4.1
importlib-metadata==4.2.0
iniconfig==2.0.0
Jinja2==3.1.6
MarkupSafe==2.1.5
marshmallow==2.21.0
mccabe==0.7.0
packaging==24.0
platformdirs==2.6.2
pluggy==1.2.0
py==1.11.0
pycodestyle==2.9.1
pyflakes==2.5.0
Pygments==2.17.2
pymongo==4.7.3
pytest==7.4.4
pytest-cov==4.1.0
python-dateutil==2.9.0.post0
pytz==2025.2
requests==2.31.0
six==1.17.0
snowballstemmer==2.2.0
Sphinx==4.3.2
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
tomli==2.0.1
tox==3.28.0
typing_extensions==4.7.1
-e git+https://github.com/Scille/umongo.git@3f5339442d1ec3ce66d4e41dabcdb8242bf2e684#egg=umongo
urllib3==2.0.7
virtualenv==20.16.2
zipp==3.15.0

name: umongo
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alabaster==0.7.13
      - babel==2.14.0
      - bump2version==1.0.1
      - bumpversion==0.6.0
      - charset-normalizer==3.4.1
      - coverage==7.2.7
      - distlib==0.3.9
      - dnspython==2.3.0
      - docutils==0.17.1
      - exceptiongroup==1.2.2
      - filelock==3.12.2
      - flake8==5.0.4
      - idna==3.10
      - imagesize==1.4.1
      - importlib-metadata==4.2.0
      - iniconfig==2.0.0
      - jinja2==3.1.6
      - markupsafe==2.1.5
      - marshmallow==2.21.0
      - mccabe==0.7.0
      - packaging==24.0
      - platformdirs==2.6.2
      - pluggy==1.2.0
      - py==1.11.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - pygments==2.17.2
      - pymongo==4.7.3
      - pytest==7.4.4
      - pytest-cov==4.1.0
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - requests==2.31.0
      - six==1.17.0
      - snowballstemmer==2.2.0
      - sphinx==4.3.2
      - sphinxcontrib-applehelp==1.0.2
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.0
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - tomli==2.0.1
      - tox==3.28.0
      - typing-extensions==4.7.1
      - urllib3==2.0.7
      - virtualenv==20.16.2
      - zipp==3.15.0
prefix: /opt/conda/envs/umongo

tests/test_fields.py::TestFields::test_datetime
tests/test_fields.py::TestRequired::test_required tests/test_fields.py::TestRequired::test_required_nested tests/test_fields.py::TestFields::test_basefields tests/test_fields.py::TestFields::test_strictdatetime tests/test_fields.py::TestFields::test_date tests/test_fields.py::TestFields::test_dict tests/test_fields.py::TestFields::test_dict_default tests/test_fields.py::TestFields::test_list tests/test_fields.py::TestFields::test_list_default tests/test_fields.py::TestFields::test_complex_list tests/test_fields.py::TestFields::test_objectid tests/test_fields.py::TestFields::test_reference tests/test_fields.py::TestFields::test_reference_lazy tests/test_fields.py::TestFields::test_generic_reference tests/test_fields.py::TestFields::test_decimal
umongo/fields.py
umongo/fields.py