parsing a date string

Thomas Guettler guettli at thomas-guettler.de
Mon Mar 7 16:37:28 CET 2005


Am Sun, 06 Mar 2005 19:35:23 +0000 schrieb MikeyG:

> Hi,
> 
> I have a date string in the ctime() format ('Sat Mar  5 10:38:07 2005') 
> and I want to know how long ago that was in whole days.
> 
> So far I have:
> 
> import time
> import datetime
> age = 
> (datetime.date.fromtimestamp(time.mktime(time.strptime(date.strip()))) - 
> datetime.date.today()).days
> 
> Which is an absurd number of function calls and is possibly not even 
> correct (something in the docs about mktime() taking localtime whereas 
> my string is in UTC)

Try %Z:

time.mktime(time.strptime("Sat Mar  5 10:38:07 2005 UTC",
 "%a %b %d %H:%M:%S %Y %Z"))
--> 1110015487.0

time.mktime(time.strptime("Sat Mar  5 10:38:07 2005 CEST", 
  "%a %b %d %H:%M:%S %Y %Z"))
--> 1110011887.0

HTH,
 Thomas


-- 
Thomas Güttler, http://www.thomas-guettler.de/





More information about the Python-list mailing list