Che M | 17 May 06:07
Picon
Favicon

datetime syntax error for May 8th and 9th 2008??

Am I missing something?  Why does May 7th
and other dates work but I'm getting "invalid
token" for May 8th and 9th?  (I have not tested
many other dates of the year in this way) This
is from a freshly begun IDLE session:

IDLE 1.2     
>>> import datetime
>>> datetime.datetime(2008,05,07)
datetime.datetime(2008, 5, 7, 0, 0)
>>> datetime.datetime(2008, 05, 08)
SyntaxError: invalid token
>>> datetime.datetime(2008, 05, 09)
SyntaxError: invalid token

Is this a glitch in the Matrix?

Che

E-mail for the greater good. Join the i’m Initiative from Microsoft.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Che M | 17 May 06:16
Picon
Favicon

Re: datetime syntax error for May 8th and 9th 2008??



From: pine508 <at> hotmail.com
To: tutor <at> python.org
Subject: datetime syntax error for May 8th and 9th 2008??
Date: Sat, 17 May 2008 00:09:03 -0400

.ExternalClass .EC_hmmessage P {padding:0px;} .ExternalClass body.EC_hmmessage {font-size:10pt;font-family:Tahoma;} Am I missing something?  Why does May 7th
and other dates work but I'm getting "invalid
token" for May 8th and 9th?  (I have not tested
many other dates of the year in this way) This
is from a freshly begun IDLE session:

IDLE 1.2     
>>> import datetime
>>> datetime.datetime(2008,05,07)
datetime.datetime(2008, 5, 7, 0, 0)
>>> datetime.datetime(2008, 05, 08)
SyntaxError: invalid token
>>> datetime.datetime(2008, 05, 09)
SyntaxError: invalid token
After trying it a bit more, I see that writing it this way works:

>>> datetime.datetime(2008,05,8)
datetime.datetime(2008, 5, 8, 0, 0)

So, then, why can one not use YYYY-MM-DD with these two
dates and must instead switch to YYYY-MM-D?  I have a feeling
the explanation will be interesting and somehow make sense,
though right now it seems odd.




Keep your kids safer online with Windows Live Family Safety. Help protect your kids.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
John Fouhy | 17 May 06:29

Re: datetime syntax error for May 8th and 9th 2008??

On 17/05/2008, Che M <pine508 <at> hotmail.com> wrote:
> >>> datetime.datetime(2008, 05, 08)
> SyntaxError: invalid token

It's simpler than that... Try this:

>>> x = 08
  File "<stdin>", line 1
    x = 08
         ^
SyntaxError: invalid token
>>> x = 010
>>> x
8

Basically, python interprets integer literals starting with 0 as octal
numbers.  It's an old convention from C (or earlier?).  It doesn't
affect strings, so int('010') == 10 (unless you use eval).

HTH!

--

-- 
John.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

wesley chun | 17 May 08:38
Picon

Re: datetime syntax error for May 8th and 9th 2008??

On Fri, May 16, 2008 at 9:29 PM, John Fouhy <john <at> fouhy.net> wrote:
> On 17/05/2008, Che M <pine508 <at> hotmail.com> wrote:
>> >>> datetime.datetime(2008, 05, 08)
>> SyntaxError: invalid token
>
> It's simpler than that... Try this:
>
>>>> x = 08
>  File "<stdin>", line 1
>    x = 08
>         ^
> SyntaxError: invalid token
>>>> x = 010
>>>> x
> 8
>
> Basically, python interprets integer literals starting with 0 as octal
> numbers.  It's an old convention from C (or earlier?).  It doesn't
> affect strings, so int('010') == 10 (unless you use eval).

che,

john is correct.  any leading zero (0) of an integer number will be
translated as base 8 or octal, which counts by 8's:

00, 01, 02, 03, 04, 05, 06, 07, 10 (decimal/base 10: 8*1+0=8),
11 (decimal: 8*1+1=9), 12 (decimal: 8*1+2=10), 13 (decimal 8+3=11),
14, 15, 16, 17 (decimal: 15), 20 (decimal: 8*2+0=16), 21 (decimal:
8*2+1=17), etc...

the problem with "08" is that because you're counting by 8s, a value
of "08" is invalid because the digits "8" and "9" are not part of the
octal "alphabet" or character set. that is why "07" works but not
"08".

as an FYI, because of this confusion, starting with Python 3.0, octal
numbers will require a lowercase "o" (in the same manner as
hexadecimal/base 16 numbers require an "x") after leading 0, i.e.,
0o7, 0o16, etc.  (Oo8 is still an invalid octal number.)

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
 http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Che M | 20 May 09:01
Picon
Favicon

Re: datetime syntax error for May 8th and 9th 2008??



> Date: Fri, 16 May 2008 23:38:42 -0700
> From: wescpy <at> gmail.com
> To: pine508 <at> hotmail.com; john <at> fouhy.net
> Subject: Re: [Tutor] datetime syntax error for May 8th and 9th 2008??
> CC: tutor <at> python.org
>
> On Fri, May 16, 2008 at 9:29 PM, John Fouhy <john <at> fouhy.net> wrote:
> > On 17/05/2008, Che M <pine508 <at> hotmail.com> wrote:
> >> >>> datetime.datetime(2008, 05, 08)
> >> SyntaxError: invalid token
> >
> > It's simpler than that... Try this:
> >
> >>>> x = 08
> > File "<stdin>", line 1
> > x = 08
> > ^
> > SyntaxError: invalid token
> >>>> x = 010
> >>>> x
> > 8
> >
> > Basically, python interprets integer literals starting with 0 as octal
> > numbers. It's an old convention from C (or earlier?). It doesn't
> > affect strings, so int('010') == 10 (unless you use eval).
>
>
> che,
>
> john is correct. any leading zero (0) of an integer number will be
> translated as base 8 or octal, which counts by 8's:
>
> 00, 01, 02, 03, 04, 05, 06, 07, 10 (decimal/base 10: 8*1+0=8),
> 11 (decimal: 8*1+1=9), 12 (decimal: 8*1+2=10), 13 (decimal 8+3=11),
> 14, 15, 16, 17 (decimal: 15), 20 (decimal: 8*2+0=16), 21 (decimal:
> 8*2+1=17), etc...
>
> the problem with "08" is that because you're counting by 8s, a value
> of "08" is invalid because the digits "8" and "9" are not part of the
> octal "alphabet" or character set. that is why "07" works but not
> "08".
>
> as an FYI, because of this confusion, starting with Python 3.0, octal
> numbers will require a lowercase "o" (in the same manner as
> hexadecimal/base 16 numbers require an "x") after leading 0, i.e.,
> 0o7, 0o16, etc. (Oo8 is still an invalid octal number.)
>
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thank you, Wesley, that is helpful and interesting. 
Best regards,
Che

E-mail for the greater good. Join the i’m Initiative from Microsoft.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Gmane