parsing a date string

M

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)

Any suggestions?

Thanks
MikeG
 
T

Thomas Guettler

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top