in reply to Re^3: Logical ways to calculate being within two times
in thread Logical ways to calculate being within two times

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!