Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello Monks,

I want to store times (hours, minutes, seconds, fractions of a second) in an object. Time::Piece and Time::Seconds make a good beginning. They offer some arithmetic operations and have overloaded >, <, ... .

The following code uses Time::Seconds and Time::Piece to construct the sum of "16:51" and "34:48" in the format (min:sec).

#!/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Seconds; my $t1 = Time::Piece->strptime("16:51", "%M:%S"); my $t2 = Time::Piece->strptime("34:48", "%M:%S"); $t1 += $t2->min*60 + $t2->sec; print $t1->strftime("%M:%S");

But there are two reasons why they are not sufficient for what I want to do.

  1. I need a fraction of a second (e.g. "16:51,12" and "34:48,14"). I did not found fraction for strftime or strptime.
  2. I want to be able to use more arithmetic operations with these time objects. For example: ($t1 * 4) + $t2

Here the summary: I would like to have a parse function (time string e.g. "16:51,14" to time object). It should be possible to apply arithmetic operations and comparisons of time objects and the time object should at least store hours, minutes, seconds and fractions of a second. And of course I want to be able to print the time object (something like strftime).

Is there already a module in CPAN which can do this? I did not find any. What would you suggest how to solve that? I don't want to reinvent everything. If possible I want to use an available module.

I'm very interested in your answers.

Thank you and Greetings

Dirk


In reply to Time calculations by Dirk80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-19 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found