From 40c19a3cb0486f9ef456ef77c074337241758def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 31 Aug 2018 11:25:55 +0200 Subject: [PATCH] pillar.api.utils.utcnow() now truncates microseconds to milliseconds MongoDB stores datetimes in millisecond precision, to keep datetimes the same when roundtripping via MongoDB we now truncate the microseconds. --- pillar/api/utils/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pillar/api/utils/__init__.py b/pillar/api/utils/__init__.py index 4ff46af0..cdc85fb1 100644 --- a/pillar/api/utils/__init__.py +++ b/pillar/api/utils/__init__.py @@ -245,4 +245,10 @@ def random_etag() -> str: def utcnow() -> datetime.datetime: - return datetime.datetime.now(tz=bson.tz_util.utc) + """Construct timezone-aware 'now' in UTC with millisecond precision.""" + now = datetime.datetime.now(tz=bson.tz_util.utc) + + # MongoDB stores in millisecond precision, so truncate the microseconds. + # This way the returned datetime can be round-tripped via MongoDB and stay the same. + trunc_now = now.replace(microsecond=now.microsecond - (now.microsecond % 1000)) + return trunc_now