I package my python (flask
) application with docker. Within my app I'm generating UTC date with datetime
library using datetime.utcnow()
.
Unfortunately, when I inspect saved data with MongoDB Compass
the UTC date is offset two hours (to my local time zone). All my docker containers have time zone set to Etc/UTC
. Morover, mongoengine
connection to MongoDB uses tz_aware=False
and tzinfo=None
, what prevents on fly date conversions.
Where does the offset come from and how to fix it?
MongoDB will store date and time information using UTC internally, but can easily convert to other timezones at time of retrieval as needed.
Internally, Date objects are stored as a signed 64-bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970). Not all database operations and drivers support the full 64-bit range. You may safely work with dates with years within the inclusive range 0 through 9999 .
Ensure Your MongoDB Instance is Running Compass must connect to a running MongoDB instance. Make sure you have installed MongoDB and have a running mongod process. You should also check that the port where your MongoDB instance is running matches the port you provide in the Compass connect dialog.
Finally, after trying to prove myself wrong, and hairless head I found the cause and solution for my problem.
We are living in the world of illusion and what you see is not what you get!!!. I decided to inspect my data over mongo shell
client
rather than MongoDB Compass GUI
. I figure out that data that arrived to database contained correct UTC date. This narrowed all my previous
assumption that there has to be something wrong with my python application, and environment that the application is living in. What left was MongoDB Compass
itself.
After changing time zone on my machine to a random time zone, and refreshing collection within MongoDB Compass
, displayed UTC date changed to a date that fits random time zone.
Be aware that MongoDB Copass
displays whatever is saved in database Date
field, enlarged about your machine's time zone. Example, if you saved UTC time equivalent to 8:00 am
,
and your machine's time zone is Europe/Warsaw then MongoDB Compass
will display 10:00am
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With