Dear wise perlmonks, I am getting desperate with this subroutine I am writing: It takes an array containing hours minutes seconds, then it compares the current time to that time and calculates how long it still takes in seconds till that time has arrived. Unfortunately I have not been able to calculate hours correctly because that keeps being wrong and my brain has melted away:(. Or does anyone know a module that could do this for me? Cheers and thank you very much:) Rob

sub timeleft { $hour = (localtime(time))[2]; $minutes = (localtime(time))[1]; $seconds = (localtime(time))[0]; #print "$minutes"; print " Time now is $hour:$minutes:$seconds "; print "Time remaining until $_[0]:$_[1]:$_[2] = "; if ($_[2] == $seconds) {$seconds = 0} if ($_[2] > $seconds) {$seconds = $_[2] - $seconds;} #correct if ($_[2] < $seconds) {$seconds = (60- $seconds) + $_[2]; #correct $spareminute = 1;} #minuteklopt $minutes -= $spareminute; #print "$minutes"; if ($_ [1] == $minutes ) {$minutes = 0; } if ($_ [1] > $minutes ) {$minutes = $_[1] - $minutes; } #correct if ($_ [1] < $minutes ) {$minutes = (60 - $minutes) + $_[1]; #correct $sparehour + 1; } $minuteprint = $minutes; $minutes *= 60; $hour -= $sparehour; if ($_[0] == $hour) {$hour =0;} if ($_[0] > $hour) {$hour =$_[0] - $hour;} if ($_[0] < $hour) {$hour = (24 - $hour) + $_[0] ;} $hourprint = $hour; $hour *= 60; $hour *= 60; print "$hourprint: $minuteprint: $seconds "; $baas = $hour + $minute + $seconds; #$baas = ($hour * 60 * 60) + ($minutes * 60) + $seconds; return $baas }

In reply to Timeleft subroutine by robertw

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.