Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Timer subtraction

by sundialsvc4 (Abbot)
on Oct 06, 2017 at 12:21 UTC ( #1200822=note: print w/replies, xml ) Need Help??


in reply to Timer subtraction

If we take the OP as literally written:

my $first = "00:00:01:04"; my $last = "00:00:08:861"; my $output = $first-$last;
... the values of both $first and $last are quite-clearly strings.   In order to do arithmetic against them, they must be converted to a numeric form.   There are many, many libraries in the CPAN library that are concerned with time, and some of these are built in to the Core so as to be immediately available without installing.

For instance, DateTime takes the now-common approach of treating a date/time as a Perl object.   You create one, either specifying a string to be decoded or the individual parts of a time, then you can simply manipulate it using the methods provided.

In this particular case, where a string-format might not be automagically recognized on its own, you might need to break-apart the four parts of the string (e.g. with split() so that you can instantiate the object with the pieces-of-interest as shown in the documentation for DateTime:

my $dt = DateTime->new( year => 1966, month => 10, day => 25, hour => 7, minute => 15, second => 47, nanosecond => 500000000, time_zone => 'America/Chicago', );

You have a rich choice of time-handling modules at your disposal which will take care of all of the niggling problems associated with time encoding/decoding and time-arithmetic.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1200822]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-12-12 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (327 votes). Check out past polls.

    Notices?