[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

datetime string conversion error

Jordan Apgar

3/16/2010 6:57:00 PM

Hey all,
I'm trying to convert a string to a date time object and all my fields
convert except for month which seems to default to january.

here's what I'm doing:
date = "2010-03-16 14:46:38.409137"
olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")

print date
print olddate

I get:
2010-03-16 14:46:38.409137
2010-01-16 14:46:38.409137

notice the 01 in the second date from what I could tell everything is
formatted correctly.

thanks for the help.
~Jordan
6 Answers

Christian Heimes

3/16/2010 7:08:00 PM

0

Jordan Apgar wrote:
> Hey all,
> I'm trying to convert a string to a date time object and all my fields
> convert except for month which seems to default to january.
>
> here's what I'm doing:
> date = "2010-03-16 14:46:38.409137"
> olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>
> print date
> print olddate
>
> I get:
> 2010-03-16 14:46:38.409137
> 2010-01-16 14:46:38.409137
>
> notice the 01 in the second date from what I could tell everything is
> formatted correctly.


%j is documtend as "Day of the year as a decimal number [001,366].". Did
you mean %d instead?

Christian

Jordan Apgar

3/16/2010 7:12:00 PM

0

On Mar 16, 3:07 pm, Christian Heimes <li...@cheimes.de> wrote:
> Jordan Apgar wrote:
> > Hey all,
> > I'm trying to convert a string to a date time object and all my fields
> > convert except for month which seems to default to january.
>
> > here's what I'm doing:
> > date = "2010-03-16 14:46:38.409137"
> >  olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>
> > print date
> > print olddate
>
> > I get:
> > 2010-03-16 14:46:38.409137
> > 2010-01-16 14:46:38.409137
>
> > notice the 01 in the second date from what I could tell everything is
> > formatted correctly.
>
> %j is documtend as "Day of the year as a decimal number [001,366].". Did
> you mean %d instead?
>
> Christian

That fixed it, thank you

MRAB

3/16/2010 7:53:00 PM

0

Jordan Apgar wrote:
> Hey all,
> I'm trying to convert a string to a date time object and all my fields
> convert except for month which seems to default to january.
>
> here's what I'm doing:
> date = "2010-03-16 14:46:38.409137"
> olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>
> print date
> print olddate
>
> I get:
> 2010-03-16 14:46:38.409137
> 2010-01-16 14:46:38.409137
>
> notice the 01 in the second date from what I could tell everything is
> formatted correctly.
>
The problem seems to be related to your use of "%j", which parses the
day of the year.

If I use "%d" instead, which parses the day of the month (I think that's
what you intended!), then the month comes out as 3 as expected.

Josh English

3/16/2010 11:31:00 PM

0

On Mar 16, 11:56 am, Jordan Apgar <twistedphr...@gmail.com> wrote:

> here's what I'm doing:
> date = "2010-03-16 14:46:38.409137"
>  olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>

Due to circumstances, I'm using Python 2.5.4 on one machine (2.6 on
the other).

When I have a script as simple as this:

import datetime

datetime.datetime.strptime('2010-09-14', "%Y-%m-%d")


Running this script brings up a calendar, believe it or not. The
calendar displays March 2010, and shows the 22nd as a holiday. When I
dismiss the dialog box I get:
Traceback (most recent call last):
File "strptimetest.py", line 3, in <module>
datetime.datetime.strptime('2010-09-14', "%Y-%m-%d")
File "C:\Python25\lib\_strptime.py", line 272, in <module>
_TimeRE_cache = TimeRE()
File "C:\Python25\lib\_strptime.py", line 191, in __init__
self.locale_time = LocaleTime()
File "C:\Python25\lib\_strptime.py", line 74, in __init__
self.__calc_weekday()
File "C:\Python25\lib\_strptime.py", line 94, in __calc_weekday
a_weekday = [calendar.day_abbr[i].lower() for i in range(7)]
AttributeError: 'module' object has no attribute 'day_abbr'


err... what? Is this an old weirdness I don't remember from the 2.5
series?

I can select dates in the calendar, but nothing dismisses it but the
close box.

Josh English
Incredibly Confused

Gabriel Genellina

3/17/2010 2:26:00 AM

0

En Tue, 16 Mar 2010 20:31:11 -0300, Josh English
<joshua.r.english@gmail.com> escribió:

> On Mar 16, 11:56 am, Jordan Apgar <twistedphr...@gmail.com> wrote:
>
>> here's what I'm doing:
>> date = "2010-03-16 14:46:38.409137"
>> olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>>
>
> Due to circumstances, I'm using Python 2.5.4 on one machine (2.6 on
> the other).
>
> When I have a script as simple as this:
>
> import datetime
>
> datetime.datetime.strptime('2010-09-14', "%Y-%m-%d")
>
>
> Running this script brings up a calendar, believe it or not. The
> calendar displays March 2010, and shows the 22nd as a holiday. When I
> dismiss the dialog box I get:
> Traceback (most recent call last):
> File "strptimetest.py", line 3, in <module>
> datetime.datetime.strptime('2010-09-14', "%Y-%m-%d")
> File "C:\Python25\lib\_strptime.py", line 272, in <module>
> _TimeRE_cache = TimeRE()
> File "C:\Python25\lib\_strptime.py", line 191, in __init__
> self.locale_time = LocaleTime()
> File "C:\Python25\lib\_strptime.py", line 74, in __init__
> self.__calc_weekday()
> File "C:\Python25\lib\_strptime.py", line 94, in __calc_weekday
> a_weekday = [calendar.day_abbr[i].lower() for i in range(7)]
> AttributeError: 'module' object has no attribute 'day_abbr'

I'd say you have a calendar.py script somewhere along your sys.path, that
shadows the calendar module in the standard library.

--
Gabriel Genellina

Dave Angel

3/17/2010 10:56:00 AM

0



Gabriel Genellina wrote:
> En Tue, 16 Mar 2010 20:31:11 -0300, Josh English
> <joshua.r.english@gmail.com> escribió:
>
>> On Mar 16, 11:56 am, Jordan Apgar <twistedphr...@gmail.com> wrote:
>>
>>> here's what I'm doing:
>>> date = "2010-03-16 14:46:38.409137"
>>> olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>>>
>>
>> Due to circumstances, I'm using Python 2.5.4 on one machine (2.6 on
>> the other).
>>
>> When I have a script as simple as this:
>>
>> import datetime
>>
>> datetime.datetime.strptime('2010-09-14', "%Y-%m-%d")
>>
>>
>> Running this script brings up a calendar, believe it or not. The
>> calendar displays March 2010, and shows the 22nd as a holiday. When I
>> dismiss the dialog box I get:
>> Traceback (most recent call last):
>> File "strptimetest.py", line 3, in <module>
>> datetime.datetime.strptime('2010-09-14', "%Y-%m-%d")
>> File "C:\Python25\lib\_strptime.py", line 272, in <module>
>> _TimeRE_cache = TimeRE()
>> File "C:\Python25\lib\_strptime.py", line 191, in __init__
>> self.locale_time = LocaleTime()
>> File "C:\Python25\lib\_strptime.py", line 74, in __init__
>> self.__calc_weekday()
>> File "C:\Python25\lib\_strptime.py", line 94, in __calc_weekday
>> a_weekday = [calendar.day_abbr[i].lower() for i in range(7)]
>> AttributeError: 'module' object has no attribute 'day_abbr'
>
> I'd say you have a calendar.py script somewhere along your sys.path,
> that shadows the calendar module in the standard library.
>
And to find it, you could try the following:

import datetime
print calendar.__file__

I suspect you have more problems than just that file, but perhaps
finding that one can tell you what extra package you've got installed
that shadows parts of the standard library. Try temporarily renaming it
to see if the problem goes away.

DaveA