good ways to convert string into time

H

Hank

hi,

i have a string as follows

18Nov2003:18:23:43:405

Is there an easy way to convert that to absolute time? What i really
want to do is to parse these times from a log file and do time
comparisons, averages, stop minus start (elapsed).

Probably create my own conversion table i guess?

thanks
 
P

Peter Hansen

Hank said:
i have a string as follows

18Nov2003:18:23:43:405

It's a minor point, but what happens for days-of-the-month that are
less than 10? Leading zero, or one character shorter? Same question
goes for the other fields, I suppose...
Is there an easy way to convert that to absolute time? What i really
want to do is to parse these times from a log file and do time
comparisons, averages, stop minus start (elapsed).

Probably create my own conversion table i guess?

Probably not, actually...

-Peter
 
G

Gerrit Holl

i have a string as follows

18Nov2003:18:23:43:405
Is there an easy way to convert that to absolute time? What i really
want to do is to parse these times from a log file and do time
comparisons, averages, stop minus start (elapsed).

Probably create my own conversion table i guess?
</quote>

You can use time.strptime:
strptime(string, format) -> struct_time

Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as strftime()).

Gerrit.
 
S

Sac

Hank said:
hi,

i have a string as follows

18Nov2003:18:23:43:405

Is there an easy way to convert that to absolute time? What i really
want to do is to parse these times from a log file and do time
comparisons, averages, stop minus start (elapsed).

Probably create my own conversion table i guess?

thanks


Hank,

A source that may be of interest to you is "Text Processing in Python"
by Dr. David Mertz.

Excellent reference.
 
B

Bengt Richter

hi,

i have a string as follows

18Nov2003:18:23:43:405

Is there an easy way to convert that to absolute time? What i really
want to do is to parse these times from a log file and do time
comparisons, averages, stop minus start (elapsed).

Probably create my own conversion table i guess?

thanks

Quick and dirty using the time module (and re to split your string)
Not tested beyond what your see here!

====< s2t.py >==================
import time,re
rxo = re.compile(r'(\d+)([a-zA-Z]+)(\d+):(\d+):(\d+):(\d+):(\d+)')
monthnums = dict([(mo,i+1) for i,mo in enumerate(
'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split())])
def s2t(s, dst=-1): # guess daylight svgs
da,mo,yr,hr,min,sec,ms = rxo.search(s).groups()
mo = monthnums[mo]
tinfo = map(int, (yr,mo,da,hr,min,sec,ms))
ms = tinfo.pop()
return time.mktime(tinfo+[0,0,dst])+ms/1000.0
================================
'Tue Nov 18 18:23:43 2003'

IOW, s2t converts your time info to a floating point number in seconds from the epoch,
which e.g., time.ctime and other time functions can use.

The milliseconds (I assumed) are ignored by ctime, but I tacked them on in the number returned.
(Note that floating point won't represent all decimals accurately, but it should be good rounded
to ms, e.g.,
ED('1069208623.4049999713897705078125')

That's all the bit info. Looks like a good four 9's below your ms unit.
ED('1069208623.405')


PS. I think there is a bug in time.mktime -- I accidentally got it trying to find time zero:
(boom)

I got:

The instruction at "0x7802a7ff" referenced memory at "0x00000000". The memory
could not be "read".

That shouldn't happen no matter what garbage I type as args, ISTM ;-/
Guess I'll post a plainer mktime example separately.

Regards,
Bengt Richter
 

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

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top