You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Only naive datetime objects will be allowed as MongoDB doesn't store the timezone information. Also, the microsecond information will be truncated.
Naive datetimes are relative to the configured timezone of the host running the program. This can lead to bugs when the database is used by multiple hosts where timezone configuration is inconsistent. An option to ensure that datetime are UTC datetimes before saving data, and to set timezone info to UTC when getting data from the db will prevent this kind of bugs.
Solution
The solution I think is best is to add an argument to the Field() function.
When saving instance, datetime.astimezone(tz=timezone.utc) should be used to ensure datetime is in UTC. When retrieving data from the database, datetime fields will became aware datetimes with timezone set to UTC. The timezone information will not be stored in MongoDB.
Alternative solutions
An alternative solution is to use naive datetimes in UTC (e.g. with datetime.utcnow()), but this need to adapt existing code, and convert aware datetimes to naive datetime when we want to save them to the database.
Additional context
If this feature request is accepted, I could do a PR 😃
Thank you in advance for your answers,
The text was updated successfully, but these errors were encountered:
Hi, @baptiste0928,
I took the liberty to propose a pull request (#325) that would solve this issue in a clean way, without breaking any API, and without the need to define any parameter in the Field part.
Feature request
Context
According to the documentation :
Naive datetimes are relative to the configured timezone of the host running the program. This can lead to bugs when the database is used by multiple hosts where timezone configuration is inconsistent. An option to ensure that datetime are UTC datetimes before saving data, and to set timezone info to UTC when getting data from the db will prevent this kind of bugs.
Solution
The solution I think is best is to add an argument to the
Field()
function.When saving instance,
datetime.astimezone(tz=timezone.utc)
should be used to ensure datetime is in UTC. When retrieving data from the database, datetime fields will became aware datetimes with timezone set to UTC. The timezone information will not be stored in MongoDB.Alternative solutions
An alternative solution is to use naive datetimes in UTC (e.g. with
datetime.utcnow()
), but this need to adapt existing code, and convert aware datetimes to naive datetime when we want to save them to the database.Additional context
If this feature request is accepted, I could do a PR 😃
Thank you in advance for your answers,
The text was updated successfully, but these errors were encountered: