Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Django and microsecond discrepancy in DateTimeField

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!