Well, it works just dandy BrowserUK :)

Needs cleanup and implementation into the various locations the pieces need to be, but it's fast and appears to be accurate after testing with numerous values.

I'll implement it, then write a boatload of unit tests against the logic. Here's the code I put together with a few comments describing what's what:

use warnings; use strict; use feature 'say'; # simulate the light and times db entries my $light = 0; my $on = '18:00'; my $hrs = 12; my $now = time; my ($start, $end); # the following will be run once only when # the main webapp is started. This sets the # initial lights-on/off times into the db. # of course, it'll just overwrite without first # checking if the time was set previously or not if (! $start){ set_times(); $start = $start - 24 * 3600; $end = $end - 24 * 3600; } # simulate event calling the light method action_light(); say "light is on" if $light; # object method that the event calls sub action_light { if ($now > $start && $now < $end){ if (! $light){ $light = 1; } } elsif ($light) { $light = 0; set_times(); } } # obj method called by action_light() and upon # main webapp startup sub set_times { my $time = time; $time += 60 until localtime($time) =~ /$on:/; $start = $time; $end = $start + $hrs * 3600; }

Thanks for your input!


In reply to Re^4: Logical ways to calculate being within two times by stevieb
in thread Logical ways to calculate being within two times by stevieb

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.