Certainly follow up on dragonchild's list of relevant modules. Just in case you're looking for a "maximally portable" approach -- i.e. one the depends solely on core modules, and so does not presume/require installation of optional modules -- then here is an example of how you might use Time::Local for this sort of job:
use strict; use Time::Local; my ( $dy, $mo, $yr ) = ( localtime )[3..5]; my $ref_time = "08:35:00"; # (pm would be 20:35:00) my ( $hr, $mi, $sc ) = split( /:/, $ref_time ); my $ref_sec = timelocal( $sc, $mi, $hr, $dy, $mo, $yr ); my $delta_time = "01:23:00"; my ( $add_hr, $add_mi, $add_sc ) = split( /:/, $delta_time ); my $delta_sec = (($add_hr * 60) + $add_mi) * 60 + $add_sc; ( $sc, $mi, $hr, $dy, $mo, $yr ) = localtime( $ref_sec + $delta_sec ); # don't forget to add 1 to $mo, and 1900 to $yr if/when you use them; # one way to check results: print scalar( localtime( $ref_sec + $delta_sec )), $/;
Basically, the idea is to do delta arithmetic in seconds, and to convert between component values (second, minute, hour, day, month, year) and "seconds since the epoch" as needed. Time::Local::timelocal() converts efficiently from component values to "seconds since the epoch", and localtime goes the other way.

(As an interesting side note, timelocal() will do the right thing if you feed it "out-of-range" component values, e.g. $dy = 31 and $mo = 1 (i.e. February), or $hr = 28, etc.


In reply to Re: Which module for Time? by graff
in thread Which module for Time? by bkiahg

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.