tags:

views:

66

answers:

1

Hello,

I'm using this to convert date time strings to a unix timestamp:

str(int(time.mktime(time.strptime(date,"%d %b %Y %H:%M:%S %Z"))))

However often the date structure isn't the same so I keep getting the following error message:

time data did not match format: data=Tue, 26 May 2009 19:58:20 -0500 fmt=%d %b %Y %H:%M:%S %Z

Does anyone know of any simply function to convert a string representation of a date/time to a unix timestamp in python? I really don't want to have to open a process to call a php script to echo the timestamp everytime time in a loop :)

+4  A: 
from dateutil.parser import parse

parse('Tue, 26 May 2009 19:58:20 -0500').strftime('%s')

# returns '1243364300'
eumiro
This appears to be a reference to the third-party module here: http://labix.org/python-dateutil. As far as I know, there is no simple way to parse date strings of unknown format in the Python standard library.
Walter Mundt
that answered my issue, it's ok with thirdparty modules for me.
Joe