Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by BrowserUk (Patriarch)
on Nov 23, 2016 at 00:28 UTC ( [id://1176378]=note: print w/replies, xml ) Need Help??


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

If I'm understanding you correctly,

Given your 20:00 start time and 12 hours duration, calculate epoch times:

$s = '20:00'; $d = 12; $e = time; $e += 60 until localtime( $e ) =~ m[$s:]; $start = $e; $end = $start + $d * 3600; printf "start:%f end:%f\n", $start, $end;; start:1479931244.206990 end:1479974444.206990

The code above assumes that if a start time is given that is earlier than the current time, it means that time tomorrow.

You might be concerned by the 'crude' mechanism of converting the time to an epoch -- the until loop -- but it will take at most 1/10,000th of a second to do it.

It also assumes that time will be given to the nearest minute; though it is easily changed to allow to the nearest second, but the epoch conversion would then take upto 5/10,000ths of a second.

Of course, you could also load the whole of DateTime to do the conversion, but that probably takes longer just to load than this method :)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Logical ways to calculate being within two times
by stevieb (Canon) on Nov 24, 2016 at 01:28 UTC

    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://1176378]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-29 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found