Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Logical ways to calculate being within two times

by stevieb (Canon)
on Nov 24, 2016 at 01:28 UTC ( [id://1176467]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176467]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found