-1

Ok So it looks like there may be a bug or something is missing in the documentation. If you add a leading 0 to the days parm for example, you will get a different result than without it, See sample:

>>> import datetime
>>> datetime.date.today() - datetime.timedelta(063)
datetime.date(2014, 1, 4)
>>> datetime.date.today() - datetime.timedelta(63)
datetime.date(2013, 12, 23)
>>> datetime.date.today() - datetime.timedelta(days=63)
datetime.date(2013, 12, 23)
>>> datetime.date.today() - datetime.timedelta(days=063)
datetime.date(2014, 1, 4)
>>> datetime.date.today() - datetime.timedelta(days=063)
datetime.date(2014, 1, 4)
>>> datetime.date.today() - datetime.timedelta(days=int(063))
datetime.date(2014, 1, 4)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
radtek
  • 34,210
  • 11
  • 144
  • 111

3 Answers3

1

A leading 0 means the number is interpreted as an octal number (base 8). So 063 is actually 51 in decimal.

isedev
  • 18,848
  • 3
  • 60
  • 59
1

063 is an octal number:

>>> 063
51

because (6 * 8) + 3 is 51.

From the integer literals documentation:

octinteger     ::=  "0" ("o" | "O") octdigit+ | "0" octdigit+

In Python 3, the "0" octdigt+ part of the grammar was dropped, because too many people kept tripping over this; 063 would be a syntax error in Python 3.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

I don't think this is related to timedelta, I tried looking at this manual first: http://docs.python.org/2/library/datetime.html#timedelta-objects

Then I tried something obvious:

>>> int = 063
>>> int
51

It looks like a leading in a numeric type is understood as a octal value to python. Also, if it starts with 0x its considered a hex which makes sense. The leading 0 really threw me off but it is also answered in this thread:

How does python interpret numbers with leading zeroes

Community
  • 1
  • 1
radtek
  • 34,210
  • 11
  • 144
  • 111