gpt4 book ai didi

python - DateTimeField 中的 Django 和微秒差异

转载 作者:行者123 更新时间:2023-12-01 03:02:22 25 4
gpt4 key购买 nike

我有一个计算给定日期时间的代码,如下所示

time = datetime.datetime.now() - datetime.timedelta(minutes=60)

然后,我使用 time 设置要保存在数据库中的 offer 对象中的字段。 offer.time 是一个 Offer 模型实例;它配置为:

time = models.DateTimeField(blank=True, null=True) 

但是,调用 offer.save() 时,offer.time 会被更新。设置 offer.time = time 时,我得到 2017-04-29 09:36:14.895581。调用 offer.save 后,offer.time2017-04-29 09:36:14.895000。为什么 save() 不保留原始时间?

有什么想法吗?

提前致谢。

最佳答案

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.

这解释了为什么 2017-04-29 09:36:14.895581 变成 2017-04-29 09:36:14.895000

2017-04-29 09:36:14 将存储为 1493458574000 只留下 3 位小数来存储毫秒。添加毫秒后,该值将为 1493458574895。当将此值读回 DateTimeField 时,该值将为 2017-04-29 09:36:14.895000

关于python - DateTimeField 中的 Django 和微秒差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com