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

Community
  • 1
  • 1
Pierlo Upitup
  • 1,614
  • 4
  • 19
  • 27
  • Weird... seems to run fine on my machine – Hari Menon Feb 22 '15 at 13:59
  • Your code is working fine on my machine too. – Vinkal Feb 22 '15 at 14:00
  • Hit the link in the description to see the problem on the repl.it too – Pierlo Upitup Feb 22 '15 at 14:00
  • Just making sure... did you try on your own machine too or just repl.it? – Hari Menon Feb 22 '15 at 14:01
  • I'm going to guess it's a bug in that online system you're using. Works as expected on my machine as well. Checked on a server I'm logged into right now and works fine there as well. – mnjeremiah Feb 22 '15 at 14:02
  • Yeah, definitely broken `timedelta`. If you use select python 3, it works fine on repl.it as well – Hari Menon Feb 22 '15 at 14:05
  • unrelated to `timedelta()` issue on repl.it but related to getting tomorrow's date: [How can I subtract a day from a python date?](http://stackoverflow.com/q/441147/4279) – jfs Feb 22 '15 at 14:05
  • 1
    `timedelta` is *not* broken in CPython 2.7.2. It is broken on repl.it that uses Empythoned (CPython compiled to JS using emscripten). – jfs Feb 22 '15 at 14:24

2 Answers2

1

The timedelta implementation there is broken:

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

Should be 1 days (or 86400 seconds, this is 52039 seconds and 213056 microseconds)

Also, intermediate values fail:

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

The python you've linked seems to be broken, results on my machine:

  >>> 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 

Whereas on the linked interpreter

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
lisu
  • 2,213
  • 14
  • 22