Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Time string with addition

by Melly (Hermit)
on Jan 05, 2012 at 14:10 UTC ( #946404=perlquestion: print w/replies, xml ) Need Help??
Melly has asked for the wisdom of the Perl Monks concerning the following question:


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 (Pope) 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?


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.


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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946404]
Approved by ww
[LanX]: well even my random fingers have memory ...
[Eily]: you could have tried randomly swapping your fingers
[Eily]: enigma style
[choroba]: so it was you who typed the whole phrases?
[Yaerox]: I feel like i'm not seeing the forest for the trees. Can someone give me a hint how to research converting all kinds of files into ansi? If origin file is utf8 convert to ansi, is origin file is ansi then skip file.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2017-03-28 13:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (331 votes). Check out past polls.