1

I don't seem to understand how I'm supposed to use Python's datetime.timedelta function to calculate tomorrow's date.

Example:

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

Outputs:

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

But the second line should be more like: 2015-02-23 14:51:11

You can see this in action here.

UPDATE Thanks everyone for your comments! Apparently timedelta is broken on Python 2.7.2 [1] – would be nice to find out in which version this was fixed, though.

[1] Thanks to J.F. Sebastian for pointing out that I was wrong: timedelta is broken on Repl.it's Empythoned

4

2 回答 2

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
于 2015-02-22T14:02:15.413 回答
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
于 2015-02-22T14:04:52.287 回答