http://www.perlmonks.org?node_id=946404

Melly has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to create sql-standard time (00:00:00) and then add an arbitary number of seconds to it - e.g.

00:00:00 + 61 = 00:01:01

Any suggestions?

map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk

Replies are listed 'Best First'.
Re: Time string with addition
by Corion (Patriarch) on Jan 05, 2012 at 14:24 UTC

    Consider doing the calculation in SQL.

    As an alternative, do the calculation in seconds and convert it to a string after your calculation using POSIX::strftime.

      Hmm... lost a post... anyway, sql not an option since sql won't have access to the required offset... However, I think I've solved it with localtime(+$x) and then extracting the time part... Thanks all...

      map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
      Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
Re: Time string with addition
by MidLifeXis (Monsignor) on Jan 05, 2012 at 14:25 UTC

    Any concerns with leap seconds, DST, or other time trickery? If not, see split, join, sprintf, % and int.

    If you are looking for a CPAN module, what have you found, and what didn't work?

    --MidLifeXis

Re: Time string with addition
by moggs (Sexton) on Jan 05, 2012 at 16:34 UTC

    The modulo operator will be your key here:

    $in = 391; $hour = sprintf("%02d", int($in / 3600)); $min = sprintf("%02d", int($in / 60)); $sec = sprintf("%02d", $in % 60); print "$in .. $hour:$min:$sec\n";

    However, I agree with the idea of letting SQL handle the complexities of dates and times - being as it's in use anyway.

    Moggs

Re: Time string with addition
by sundialsvc4 (Abbot) on Jan 06, 2012 at 13:59 UTC

    A quick CPAN search on the terms date time datetime calc (e.g...) should give you all the tools that you could possibly need, should you decide not to do the job in SQL.   (And, frankly, “I’d rather not,” because I tend to find SQL functions both non-standard and sometimes wanting.)

    I don’t know if DateTime::Calendar::Mayan has been updated for 2012 yet, or not, but it’s there, nevertheless.

    Any way you do it, you do it the same way:   convert whatever string you have into an opaque date/time value, manipulate the value with the tools provided, then convert it back to a string.   Use your own regular expressions, or ones cabbaged from Regexp::Common, to fill in any gaps.