Django timezone当天diff不是0

时间:2018-02-07 18:03:13

标签: django datetime django-timezone

测试功能

from django.utils import timezone

def date_diff_now(date):
    print(date)
    print(timezone.now())
    print(date - timezone.now())
    print((date - timezone.now()).days)

结果

2018-02-07 17:46:36.442314+00:00
2018-02-07 17:47:32.084900+00:00
-1 day, 23:59:04.357374
-1

为什么同一天的2个日期时间之间的差异不会返回0?

1 个答案:

答案 0 :(得分:0)

来自Basic time and dates

  

如果天数的标准化值超出指定范围,   引发OverflowError。

     

请注意,负值的标准化最初可能会令人惊讶。   例如,

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)

所以它看起来像一个已知的溢出错误。