datetime.timedelta returns 意想不到的结果?
datetime.timedelta returns unexpected results?
我似乎不明白我应该如何使用 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
上被破坏了
那里的 timedelta 实现有问题:
timedelta(hours=24)
=> datetime.timedelta(0, 52039, 213056)
应为 1 天(或 86400 秒,这是 52039 秒和 213056 微秒)
此外,中间值失败:
timedelta(seconds=65000)
Internal error: Assertion failed: 0 <= temp && temp < 1000000
您链接的 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
我似乎不明白我应该如何使用 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
那里的 timedelta 实现有问题:
timedelta(hours=24)
=> datetime.timedelta(0, 52039, 213056)
应为 1 天(或 86400 秒,这是 52039 秒和 213056 微秒)
此外,中间值失败:
timedelta(seconds=65000)
Internal error: Assertion failed: 0 <= temp && temp < 1000000
您链接的 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