473,503 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 23820
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
5437
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 simple calculator prgram that illistrates good OOP...
5
18694
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: tsEndTime=Convert.ToDateTime(rdr.GetValue(15))
12
2413
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 class, since I am writing a program that really...
13
3871
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, a PITA in WordMailMerge, sending it as a text...
12
2682
by: apple.davinci | last post by:
as many as possible. int i = 333; char* s; how to convert i to s;
10
1633
by: steve yee | last post by:
the code is like this: class Asn_T { public: DataType_T m_data; }; class MyData {
6
2060
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 ways to parse strings effectively? Thanks!
8
6032
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 running in a different time zone as compared to my...
41
2303
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 seem to see patterns. But even a crappy rand()...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.