Most everyone has replied with some form of "Convert it to seconds, do the math, convert it back again". This is an entirely acceptable answer, and absent any other requirements this is what I would do.

There is, however, a possible reason not to do this if performance is an issue. Note that there would have to be a really good reason to do it this way, but here goes.

Converting a string such as 20041101174146 to a number of seconds would take at least 6 mathematical operations for the 2004, 11, 01, 17, 41, and 46 components. Converting it back again will take another 6, then there's either the string splitting or modulus operations to break it up, and the opposites to put it back again. This could entail 25 discrete steps.

In most cases, and certainly with the example above, this can be done with a substring, a subtract, a test for undererflow, and an assignment back to the substring. It is possible that adding a value to a time string such as the above could cause 5 overflows (19991231235959 + 1 second = 20000101000000), and this case would take considerably more computation than the generic convert-to-seconds approach, but in most cases there should be a performance gain.


In reply to Re: Date Manipulation Calculation Question - performance alternative by PhilHibbs
in thread Date Manipulation Calculation Question by EchoAngel

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.