While there are many date & time packages available on CPAN, for my purposes, I generally just store times as returned by time() (seconds since 1970/01/01 00:00:00 IIRC). Then taking the difference is a simple subtraction. Of course, you'll need to format the results when you're done. But I typically do something like:

my $start=time; # ... long processing job goes here... my $dur = time - $start; # Print out in appropriate format: print "Job took $dur seconds\n"; # For longer jobs, I often use one of: printf "Job took %5.2 minutes\n", $dur/60.0; printf "Job took %5.2 hours\n", $dur/(60*60.0); printf "Job took %5.2 days\n", $dur/(24*60*60.0); printf "Job took %5.2 weeks\n", $dur/(7*24*60*60.0); # etc.

Pretty simple and adequate for what I do. If I cared for more accuracy or features, I'd check out one of the Date modules on CPAN.

...roboticus

I have a fancier formatting module to show in format "Days HH:MM:SS", but the margin is too small for me to note it here. (Actually, it's on my server at home, and I haven't brought MY::Utils.pm into work with me yet. It's on my TODO list.)


In reply to Re: Date and time difference by roboticus
in thread Date and time difference by Anonymous Monk

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.