datetime module: iso date *input* needed
Andrew Henshaw
andrew.henshaw at gtri.gatech.edu
Fri Jan 31 22:41:05 EST 2003
Dale Strickland-Clark wrote:
> george young <gry at ll.mit.edu> wrote:
>
>>[python 2.3a1, linux]
>>I'm using the 2.3a datetime module. The datetime.isoformat() works
>>nicely:
>> >>> import datetime
>> >>> d=datetime.datetime.now()
>> >>> d
>> datetime.datetime(2003, 1, 30, 17, 48, 7, 848769)
>> >> d.isoformat()
>> '2003-01-30T17:48:07.848769'
>>
>>but now I need a way to make a datetime object from a date in iso format.
>>I can't find a function in datetime to do this, e.g. something like:
>> d=datetime.datetime.parse_iso('2003-01-30T17:48:07.848769')
>>
>>I thought about using the time module strptime function, but it doesn't do
>>fractions of a second. I used to move date/times between python and
>>postgres as epoch integers, but I hope to change to a human-readable
>>format for better debugging.
>>
>>I'm sure I could do this somehow with mx.DateTime, but if the datetime
>>module is going to be the new standard, I would like to use it in my new
>>code...
>>
>>-- George
>
> A bit of a sledgehammer but what about:
>
>>>> import re
>>>> t = '2003-01-30T17:48:07.848769'
>>>> p = re.compile('[.:T-]')
>>>> isobits = [int(s) for s in p.split(t)]
>>>> isobits
> [2003, 1, 30, 17, 48, 7, 848769]
>>>> apply(datetime.datetime, isobits)
>
> I assume this will work but I don't have 2.3a installed so no datetime
> module to play with.
>
> --
> Dale Strickland-Clark
> Riverhall Systems Ltd
Wouldn't you have ambiguities with the fractional portion (e.g..0848 and
.848)? Perhaps it would be best to leave the seconds in floating-point
format.
Andy
More information about the Python-list
mailing list