datetime.timedelta返回意外结果?

时间:2015-02-22 13:53:12

标签: python python-2.7 datetime timedelta empythoned

我似乎不明白我应该如何使用Python的datetime.timedelta函数来计算明天的日期。

示例:

from datetime import datetime, timedelta print(datetime.now()) print( datetime.now() + timedelta(hours=24))

输出:

2015-02-22 14:51:11.515000 2015-02-23 05:18:30.728056

但第二行应该更像: 2015-02-23 14:51:11

您可以在行动here中看到这一点。

更新感谢大家的意见!显然,timedelta在Python 2.7.2 [1]上被打破了 - 很高兴找到修复的版本。

[1]感谢J.F. Sebastian指出我错了:timedelta在Repl.it的'Empythoned

上被打破了

2 个答案:

答案 0 :(得分:1)

timedelta的实施已经破裂:

   timedelta(hours=24)
=> datetime.timedelta(0, 52039, 213056)

应该是1天(或86400秒,这是52039秒和213056微秒)

此外,中间值失败:

timedelta(seconds=65000)
Internal error: Assertion failed: 0 <= temp && temp < 1000000

答案 1 :(得分:0)

您链接的python似乎已被破坏,结果在我的机器上:

  >>> from datetime import datetime, timedelta                                                                                                                                     
>>> print(datetime.now())                                                                                                                                                        
2015-02-22 15:03:44.447179                                                                                                                                                       
>>> for i in range(24):                                                                                                                                                          
...     print( datetime.now() + timedelta(hours=i))                                                                                                                              
...                                                                                                                                                                              
2015-02-22 15:03:46.383497                                                                                                                                                       
2015-02-22 16:03:46.383625                                                                                                                                                       
2015-02-22 17:03:46.383677                                                                                                                                                       
2015-02-22 18:03:46.383721                                                                                                                                                       
2015-02-22 19:03:46.383765                                                                                                                                                       
2015-02-22 20:03:46.383819                                                                                                                                                       
2015-02-22 21:03:46.383841                                                                                                                                                       
2015-02-22 22:03:46.383866                                                                                                                                                       
2015-02-22 23:03:46.383887                                                                                                                                                       
2015-02-23 00:03:46.383909                                                                                                                                                       
2015-02-23 01:03:46.383930                                                                                                                                                       
2015-02-23 02:03:46.383952                                                                                                                                                       
2015-02-23 03:03:46.383973                                                                                                                                                       
2015-02-23 04:03:46.383995                                                                                                                                                       
2015-02-23 05:03:46.384017                                                                                                                                                       
2015-02-23 06:03:46.384063                                                                                                                                                       
2015-02-23 07:03:46.384094                                                                                                                                                       
2015-02-23 08:03:46.384212                                                                                                                                                       
2015-02-23 09:03:46.384240                                                                                                                                                       
2015-02-23 10:03:46.384262                                                                                                                                                       
2015-02-23 11:03:46.384290                                                                                                                                                       
2015-02-23 12:03:46.384318                                                                                                                                                       
2015-02-23 13:03:46.384337                                                                                                                                                       
2015-02-23 14:03:46.384388 

而在链接的口译员

2015-02-22 15:01:16.352000
2015-02-22 15:01:16.353000
2015-02-22 16:01:16.354000
Internal error: Assertion failed: 0 <= temp && temp < 1000000