472,353 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

good ways to convert string into time

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
Jul 18 '05 #1
5 23746
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
Jul 18 '05 #2
Sac
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.
Jul 18 '05 #3
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


Jul 18 '05 #4
<quote name="Hank" date="1069145888" email="so*********@yahoo.ca">
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

Jul 18 '05 #5
On 18 Nov 2003 09:58:08 -0800, so*********@yahoo.ca (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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: altergothen | last post by:
I'm currently doing a course in C# programming fundamentals. Please will you check and comment on the following assignment: Assignment: Create a...
5
by: simon | last post by:
I have datetime variable: Datetime tsEndTime; Should I use (DateTime): tsEndTime=(DateTime)rdr.GetValue(15) or is better to use: ...
12
by: Peter Lin | last post by:
Hey, I am just wondering if anyone has got any idea of setting up a new class so that you could just print like the old ways with the printer...
13
by: salad | last post by:
Hi Guys: I was stuck. I needed to send a report to a file. My beautiful report(s) in Access were going to require loss of formatting with RTFs,...
12
by: apple.davinci | last post by:
as many as possible. int i = 333; char* s; how to convert i to s;
10
by: steve yee | last post by:
the code is like this: class Asn_T { public: DataType_T m_data; }; class MyData {
6
by: bwaichu | last post by:
I am writing a very basic web server, and I need to parse the HTTP Request string that I am receiving. Are there any good C books that suggest...
8
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is...
41
by: Zytan | last post by:
Anyone do any tests on it? I would assume it has improved since C's rand(), but who knows. For some reason, and it could just be coincidence, I...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.