change the date string into timestamp

Heiko Wundram modelnine at ceosg.de
Sat Apr 9 05:55:11 EDT 2005


Am Samstag, 9. April 2005 11:38 schrieb praba kar:
>     In Php strtotime() will change a date
> string into timestamp.  I want to know which
> python function will  change a date string
> into timestamp.

You want the standard library function strptime from the time module (in case 
it's a timestamp you're looking for), otherwise have a look at the library 
documentation for the datetime module, which gives you a datetime class which 
can also be constructed strptime-like from a string.

Example:

>>> date = "Fri, 8 Apr 2005 09:22:14 +0900"
>>> timetuple = time.strptime(date,"%a, %d %b %Y %H:%M:%S +0900")
>>> timetuple
(2005, 4, 8, 9, 22, 14, 4, 98, -1)
>>> timestamp = time.mktime(timetuple)
>>> timestamp
1112944934.0

The timestamp is in local time, as is the timetuple (I've hardcoded the +0900, 
there's no format string which will parse the timezone parameter, and recode 
the time to UTC, it's up to you to do that).

-- 
--- Heiko.
listening to: Pearl Jam - Spin The Black Circle
  see you at: http://www.stud.mh-hannover.de/~hwundram/wordpress/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050409/527ef1b4/attachment.sig>


More information about the Python-list mailing list