I'm one of the DateTime developers, so it may surprise you that in this case I'd wonder if you need the entire swiss army knife that is DateTime, or if all you need is a butter knife...

DateTime can do many things beside simple duration formatting. It can manipulate dates and time and convert them between timezones to most points in history. You can even convert your dates to the Julian calendar or even Tolkien's shire calendar. But if you don't need all that don't bother with DateTime (but think about it carefully, will you be writing some other code in the future to work with these dates? If so, then use DateTime)

The Butter Knife
The following is the non-DateTime way to do it. The math is fairly simple and I've put it into constants so you can follow it easily.

Start with finding out how many of the largest period (weeks) are in the duration, then subtract that from the duration and move to the next level of precision. In the end you'll be left with seconds.

sub doSomethingHere { my $duration = shift; my $MINUTE = 60; my $HOUR = 60 * $MINUTE; my $DAY = 24 * $HOUR; my $WEEK = 7 * $DAY; my $weeks = int($duration / $WEEK); $duration -= $weeks * $WEEK; my $days = int($duration / $DAY); $duration -= $days * $DAY; my $hours = int($duration / $HOUR); $duration -= $hours * $HOUR; my $minutes = int($duration / $MINUTE); my $seconds = $duration - $minutes * $MINUTE; return ($weeks, $days, $hours, $minutes, $seconds); }
(Disclaimer: The above code is not golf. There are more compact ways to do it without declaring the constants, but this is to illustrate the procedure)

In reply to Re: figuring difference in times? by BigLug
in thread figuring difference in times? 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.