Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > good ways to convert string into time

Reply
Thread Tools

good ways to convert string into time

 
 
Hank
Guest
Posts: n/a
 
      11-18-2003
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
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      11-18-2003
Hank wrote:
>
> 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
 
Reply With Quote
 
 
 
 
Gerrit Holl
Guest
Posts: n/a
 
      11-18-2003
<quote name="Hank" date="1069145888" email="(E-Mail Removed)">
> 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.

--
Mozilla _is_ the web: it grows faster than you can download it.
1011001 1101111 1110101 1110010 1110011 0101100
1000111 1100101 1110010 1110010 1101001 1110100

 
Reply With Quote
 
Jp Calderone
Guest
Posts: n/a
 
      11-18-2003
On Tue, Nov 18, 2003 at 09:58:08AM -0800, Hank wrote:
> 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?


time.strptime

http://www.python.org/doc/lib/module-time.html


>
> thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
Sac
Guest
Posts: n/a
 
      11-18-2003
Hank wrote:

> 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.


 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      11-18-2003
On 18 Nov 2003 09:58:08 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (Hank) wrote:

>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
================================

>>> import s2t
>>> s2t.s2t('18Nov2003:18:23:43:405')

1069208623.405
>>> import time
>>> time.ctime(s2t.s2t('18Nov2003:18:23:43:405'))

'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.,

>>> from ut.exactdec import ED
>>> ED(s2t.s2t('18Nov2003:18:23:43:405'),'all')

ED('1069208623.4049999713897705078125')

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

>>> ED(s2t.s2t('18Nov2003:18:23:43:405'),'all').round( 3)

ED('1069208623.405')


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

>>> s2t.s2t('01Jan1970:00:00:00:000', 1)

25200.0
>>> s2t.s2t('31Dec1969:23:00:00:000', 1)

21600.0
>>> s2t.s2t('31Dec1969:17:00:00:000', 1)

0.0
>>> s2t.s2t('31Dec1969:17:00:00:000', 0)

3600.0
>>> s2t.s2t('31Dec1969:17:00:00:000', 1)

0.0
>>> s2t.s2t('31Dec1969:16:00:00:000', 1)

(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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert time to ruby time to calculate the time difference. Ruwan Budha Ruby 4 03-09-2011 04:43 PM
Split string (then) Convert string into Integer news ASP General 2 05-26-2010 11:58 AM
convert time string in UTC to time in local time davelist@mac.com Python 1 03-11-2007 12:57 AM
how many ways to convert a integer to a string apple.davinci@gmail.com C Programming 12 03-10-2006 09:18 PM
How do I convert TIME into Cookie and last-modified-time format? Perl Misc 2 10-01-2004 07:28 PM



Advertisments