Converting a string to time - simple task but don't know how to do it!

Alex Martelli aleax at aleax.it
Sat Sep 14 21:45:27 CEST 2002


Richard Kessler wrote:

> I have a string returned from a database expressing the time i.e.
> 2002-09-14
> 10:30:34.  I need to compare this value to the current time to determine
> the amount of time elapsed between the two. I get the current time using
> time.time() but I cannot find a way to convert the string from the
> database into a time value (seconds from epoch) to do the simple
> subraction to find seconds elapsed.

What you want is function strptime of module time.  Unfortunately, up
to Python 2.2 it's platform-dependent whether module time does supply
function strptime or not (in Python 2.3, you'll be able to use that
function on any platform).  There are pure-Python implementations
of strptime out on the net, e.g. 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/56036


Alex




More information about the Python-list mailing list