I have a code that calculates a given datetime as follows
time = datetime.datetime.now() - datetime.timedelta(minutes=60)
I then use time to set a field in an offer object to be saved in the database. The offer.time is a Offer model instance; it is configured to be:
time = models.DateTimeField(blank=True, null=True)
It happens, however, that offer.time is updated when calling offer.save(). When setting offer.time = time, I get 2017-04-29 09:36:14.895581. After calling offer.save, offer.time is 2017-04-29 09:36:14.895000. Why isn't save() preserving the original time?
Any ideas?
Thanks in advance.
https://docs.mongodb.com/manual/reference/method/Date/
Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.
This explains why 2017-04-29 09:36:14.895581 becomes 2017-04-29 09:36:14.895000
2017-04-29 09:36:14 will be stored as 1493458574000 leaving only 3 more decimals to store milliseconds. When adding milliseconds the value will be 1493458574895. When this is read back into the DateTimeField, the value will be 2017-04-29 09:36:14.895000.