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.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 )), $/;
(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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |