[Tutor] Matching word, time

Magnus Lycka magnus@thinkware.se
Thu, 26 Sep 2002 00:50:14 +0200


At 11:54 2002-09-25 -0700, Sean 'Shaleh' Perry wrote:
>On Tuesday 24 September 2002 12:57, Ali Soleymanzadeh wrote:
> > Test Run By asoleyma on Tue Sep 24 12:33:47 2002
> > runtest completed at Tue Sep 24 12:33:48 2002
> >
> > How can I calculate the time, in seconds, which my test was running?
>
>there is a handy python package called mxDateTime, search for it in the=20
>vaults
>of Parnasus.

You don't really need mxDateTime for this (although, in the
long run it might well be worth having).

On most unices, the time modules contains a time.strptime()
method that will handle this. (The time module is just a
thin wrapper on top of the C time library. There are strptime
implementations in Python I think.)

 >>> import time
 >>> timestamp =3D "Tue Sep 24 12:33:48 2002"
 >>> format =3D "%a %b %d %H:%M:%S %Y"
 >>> print time.strptime(timestamp, format)
(2002, 9, 24, 12, 33, 48, 1, 267, 0)

Now you have a time tuple, and you can convert it
to seconds since epoch and just subtract from another
one.

It's not so difficult to handle without strptime though...
Use good old re:

import re, time

pattern =3D re.compile(r"""(?P<comment>.+?)#Whatever leading text
(?P<dow>\w{3})\ #Day of Week 3 letters
(?P<month>\w{3})\ #Month 3 letters
(?P<day>\d{1,2})\ # Day of month 1-2 digits
(?P<hour>\d{1,2}):# Hour
(?P<min>\d{1,2}):# Minutes
(?P<sec>\d{1,2})\ # Seconds
(?P<year>\d{4})# Year 4 digits""",re.VERBOSE)

data =3D """Test Run By asoleyma on Tue Sep 24 12:33:47 2002
runtest complete at Tue Sep 24 12:33:48 2002""".split('\n')

months =3D 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split()
days =3D 'Mon Tue Wed Thu Fri Sat Sun'.split()

lastTime =3D 0
for line in data:
     match =3D pattern.search(line)
     if not match:
         print "Could not analyze:", line
         continue
     d =3D match.groupdict()
     print d['comment'],
     ttuple =3D (int(d['year']), months.index(d['month'])+1, int(d['day']),
               int(d['hour']), int(d['min']), int(d['sec']),
               days.index(d['dow']), 0, 0)
     print time.asctime(ttuple),
     newTime =3D time.mktime(ttuple)
     if lastTime:
         print "Duration =3D %i seconds" % (newTime-lastTime),
     print
     lastTime =3D newTime

Test Run By asoleyma on  Tue Sep 24 12:33:47 2002
runtest complete at  Tue Sep 24 12:33:48 2002 Duration =3D 1 seconds

I'm cheating a bit with time zones etc...

With mxDateTime

 >>> from mx import DateTime
 >>> print DateTime.DateTimeFrom("Tue Sep 24 12:33:48 2002")
2002-09-24 12:33:48.00
 >>> # But be aware, bad times are silently accepted or ignored...
 >>> print DateTime.DateTimeFrom("Tue Sep 2344 12:33:48 211002")
2021-10-02 12:33:48.00
 >>> print DateTime.DateTimeFrom("Tue Slept 2344 12:33:48 211002")
2021-10-02 12:33:48.00
 >>> print DateTime.DateTimeFrom("Tue Slept 24 12:33:48 211002")
2021-10-02 12:33:48.00
 >>> print DateTime.DateTimeFrom("Tue Slept 24 12:33:48 2002")
2002-09-26 12:33:48.00


--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se