Hi all,

Need some help please on doing date/time arithmetic. Basically I have a script where I save the starttime and endtime using time(). And at the end of the run, I do a endtime - starttime to get the difference of how long the script took to run which I believe is in seconds.

Question is if there is any way of converting the result so that it can be displayed in x-days, x-hours, x-mins, x-seconds instead of second?

At the moment, am thinking of writing some sort of function to convert the seconds to hours, minute, seconds but before I go to that length, I am wishfully thinking that maybe someone had already come across the same requirement that am looking for. Any advise will be very much appreciated.

Excerpt of the Perl script as below. Note that am very restricted to what modules I have available and cannot install additional Date/Time modules that I've found very useful. At the moment, am only allowed Time::Local.

Any response or suggestions will be very much appreaciated. Thanks in advance.

use strict; use POSIX; use File::Basename; use Time::Local; ...... ...... my $start_time = scalar localtime; my $start = time(); ...... ...... my $end_time = scalar localtime; my $end = time(); #my $end_timelocal = timelocal($sec, $min, $hour, $mday, $mon, $year); print "end_time ==> $end_time" . "\n"; #print "end_timelocal ==> $end_timelocal" . "\n"; my $diff = $end - $start;

In reply to Date/Time Arithmetic by newbie01.perl

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.