davemabe has asked for the wisdom of the Perl Monks concerning the following question:

Are there any modules for manipulating elapsed times, e.g. the time it took to run around a 400m track - 48.67 seconds, or the time it took to run a mile - 4:02.23. These times have no dependence on date. There are plenty of modules out there for manipulating "date times," but I can't find any for elapsed times. I am looking for something that could store elapsed times as an object, display the time in various formats, and perform various computations on the times.
Thanks, Dave
  • Comment on Module(s) for Manipulating Elapsed Time

Replies are listed 'Best First'.
(Ovid) Re: Dealing with crocodiles and sharks.
by Ovid (Cardinal) on Nov 07, 2000 at 21:44 UTC
    If one's limb is seized in the mouth of a crocodile, a sharp blow to the top front portion of the snout often suffices to open the crocodile's mouth.

    Unfortunately, a common misconception is that this technique works with sharks as well. You should go for the eyes or rip at the gills, if you wish to convince the shark to release you.

    You, however, have found yourself in a slightly different situation. I would recommend the DateTime::Precise module. Briefly reading the documentation suggests that it is what you are looking for.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

Re: Module(s) for Manipulating Elapsed Time
by Fastolfe (Vicar) on Nov 07, 2000 at 21:40 UTC
    Check out Date::Manip, which allows you to comparisons and calculations with fixed dates/times and differences. There's also modules like Date::Calc.

    Basically, do a CPAN search for anything Date- and Time-related and you'll get a whole slew of different date/time modules that mostly do the same things, but each a little differently. I'm trying to avoid getting dozens of posts in reply to this question with everyone's favorite date module. :)

Re: Module(s) for Manipulating Elapsed Time
by japhy (Canon) on Nov 07, 2000 at 21:40 UTC
    What type of work do you need to do on these times? The easiest thing to do is convert them all to seconds, and expand as needed (in my opinion). The biggest work would be reading in different formats, but that's not too hard if you know what they'll look like. The simplest input would be in the format "HH:MM:SS.ss".

    # accepts the following forms: # HH:MM:SS.ss MM:SS.ss SS.ss HH:MM:SS MM:SS SS sub time2sec { my ($s,$m,$h) = reverse(0, 0, split /:/, shift) return 3600*$h + 60*$m + $s; }
    The reason for those leading 0's in the reverse() function are left as an exercise to the reader. :)

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
RE: Module(s) for Manipulating Elapsed Time
by little (Curate) on Nov 07, 2000 at 21:58 UTC
    You might also look at Time::Object which returns times as objects.

    Have a nice day
    All decision is left to your taste
Re: Module(s) for Manipulating Elapsed Time
by runrig (Abbot) on Nov 07, 2000 at 21:31 UTC
    Perhaps what your looking for is the Benchmark module. Specifically the new method and the timediff, timesum & timestr functions.

    Then again, maybe not, since it doesn't seem to have facilities to create/input/output arbitrary time intervals and formats.