Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

How do I add two or more quantities of time - HH:MM:SS ?

by signal9 (Pilgrim)
on Oct 24, 2002 at 16:35 UTC ( #207770=categorized question: print w/replies, xml ) Need Help??
Contributed by signal9 on Oct 24, 2002 at 16:35 UTC
Q&A  > dates and times

Answer: How do I add two or more quantities of time - HH:MM:SS ?
contributed by mojotoad

This question, as stated, does not make much sense. What does it mean to add two times off of the clock? (see below if you just meant time segments expressed as hours, minutes, and seconds)

Perhaps you meant you wanted the difference between two times. In that case, Time::Piece is your friend.

First, you want to make your time pieces. These are generated via localtime(), gmtime, new(), or strptime(). If you happen to have seconds since the epoch, you can generate arbitrary time pieces with gmtime or localtime. Otherwise, if you are starting with HH::MM::SS you can use strptime() like so:

use Time::Piece; $t = Time::Piece->strptime('%T', $time_string);

Once you have your time pieces, you can find the difference by subtracting them directly. This generates a Time::Seconds object -- since this is the difference between two dates, it represents no particular date. Rather, it represents the number of seconds between to points in time. It can be displayed in a variety of convenient formats (see the pod).

$seconds = $t2 - $t1; print "Diff (secs): ", $seconds->seconds, "\n";

Additionally, if you want to add two chunks of time as expressed with hours, minutes, and seconds (as you might have been asking), you will want to express them as Time::Seconds objects using strptime. Then you can proceed normally with addition:

$total_secs = $s2 + $s1; print "Secs: ", $total_secs->seconds, "\n";


Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [Lady_Aleena]: So putting a nice DE or WM up on a Linux machine is not an option?
    [Discipulus]: i need some converter to have directly an OVF format directly: at the moment i'm trying with vmware converter standalone
    [Lady_Aleena]: I don't know what to tell you. I can barely get my Linux machine to do things I want.
    [Discipulus]: if DE or WM means desktop env and win manager.. no, not an option
    [Lady_Aleena]: That's what they mean, at least to me. 8)
    [Discipulus]: in the first attempt i failed, with an not usable v-machine.. ;=)
    [Discipulus]: but then discovered a registry trick to do before the physical2virtual migration
    [Lady_Aleena]: Sounds like a headache.

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (8)
    As of 2017-05-22 18:42 GMT
    Find Nodes?
      Voting Booth?