two newbie questions ... "date from string"

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue May 13 07:30:30 EDT 2003


Daan Hoogland <hoogland at astron.nl> wrote in
news:mailman.1052821639.7509.python-list at python.org: 

> 
> Any idea on the datetime_from_string parsing question, anyone? Or do I
> have to write one myself?
> 
> I can't find a function to convert a date string back to a datetime
> object. The date i have is returned by postgresql in the (well known)
> format "YYYY-MM-DD hh:mm:ss:mmmmmm". Am i missing something in the
> help? Or is there an extended Date package that does this?

>>> time.strptime("2003-05-13 12:15:18:000000", "%Y-%m-%d %H:%M:%S")
(2003, 5, 13, 12, 15, 18, 1, 133, -1)

Unfortunately, unless you are using Python 2.3, you will find that not all 
platforms include the time.strptime function (I think Unix has it, Windows 
doesn't). In Python 2.3 strptime is implemented in a module _strptime.py 
which should be useable on 2.2.1+ if you copy it.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list