perl dates: a notify alert window

E

Eric

I'm having a little difficulty with this one. I want to have a notify
script "not" send messages during a weekend "quiet window" from Friday
17:00 through Sunday 17:00. Sounds simple enough on the surface. I
just need an "okToSend()" function to return a True (1) if the
current date and time are not within that blackout window.

But .... how do you do this?
If the current epoch timestamp is $now = time; How do I determine if
it's within this blackout window? Not so simple. I've tried
calculating the start of week Monday, and then adding enough seconds
to determine Friday 17:00 and the same for Sunday 17:00 and then
seeing if the current timestamp is between these two numbers .... but
my code is not very elegant and this just feels like the wrong
approach.

Any suggestions?


Thanks,
Eric
 
C

cartercc

Eric said:
I'm having a little difficulty with this one. I want to have a notify
script "not" send messages during a weekend "quiet window" from Friday
17:00 through Sunday 17:00. Sounds simple enough on the surface. I
just need an "okToSend()" function to return a True (1) if the
current date and time are not within that blackout window.

localtime() returns both the hour and the weekday, like this:

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime
(time);
print qq(day is $wday and hour is $hour\n);
my $quiet_window = 1;
#default
if ($wday == 5 and $hour > 17) { $quiet_window = 0; } #Friday night
elsif ($wday == 6) { $quiet_window = 0; }
#Saturday
elsif ($wday == 0 and $hour < 17) { $quiet_window = 0; } #Sunday
morning
else { $quiet_window = 01 }
#default again
 
J

Jürgen Exner

Eric said:
just need an "okToSend()" function to return a True (1) if the
current date and time are not within that blackout window.

But .... how do you do this?

Take a look at Date::Calc. It has numerous functions to determine things
like the current day of the week (is it Fri/Sat/Sun?) and the current
time (if is after 17:00 (for Friday) or before 17:00 (for Sunday)).

jue
 
A

A. Sinan Unur

I'm having a little difficulty with this one. I want to have a notify
script "not" send messages during a weekend "quiet window" from Friday
17:00 through Sunday 17:00. Sounds simple enough on the surface. I
just need an "okToSend()" function to return a True (1) if the
current date and time are not within that blackout window.

#!/usr/bin/perl

use strict;
use warnings;

sub is_blackout {
my ($hour, $wday) = localtime[2,6];

return ( $wday == 5 && $hour > 17 )
or ( $wday == 6 )
or ( $wday == 0 && $hour < 17 )
;
}

print is_blackout() ? "Yes" : "No", "\n";




--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
J

Jürgen Exner

cartercc said:
localtime() returns both the hour and the weekday, like this:

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime
(time);
print qq(day is $wday and hour is $hour\n);
my $quiet_window = 1;
#default
if ($wday == 5 and $hour > 17) { $quiet_window = 0; } #Friday night
elsif ($wday == 6) { $quiet_window = 0; }
#Saturday
elsif ($wday == 0 and $hour < 17) { $quiet_window = 0; } #Sunday
morning
else { $quiet_window = 01 }
#default again

Please don't take this personal, but this is actually a very nice
example code, where people don't trust boolean algebra.
To me

sub quiet_window {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
return ( ($wday == 5 and $hour > 17) #Friday
or ($wday == 6) #Saturday
or ($wday == 0 and $hour < 17) #Sunday
}

is much easier to write, to read, and to understand than that cascading
elsif chain.

The query for localtime could also be rewritten as
my (undef, undef, $hour, undef, undef, undef, $wday, undef, undef) =
localtime(time);
because those two values are all you want.

Or even better use an array slice:
my ($hour, $wday) = localtime(time)[2,6];
to extract exactly those elements you are interested in.

jue
 
C

cartercc

Please don't take this personal, but this is actually a very nice
example code, where people don't trust boolean algebra.
To me

sub quiet_window {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =              
        localtime(time);
return ( ($wday == 5 and $hour > 17) #Friday
        or ($wday == 6)         #Saturday
        or ($wday == 0 and $hour < 17) #Sunday

}

is much easier to write, to read, and to understand than that cascading
elsif chain.

No offense taken at all. I didn't take the time to make this efficient
in any way or to dress it up -- I only wanted to convey the idea that
localtime() returns the values you need. Also, it's not a matter of
not trusting Boolean algebra, but of clarifying the logic. When I
first dashed this off, I had all the logic in a single set of
parentheses, but I changed it to make it clear that one had to test
BOTH the day and the hour. Obviously, there's more than one way to do
it.
Or even better use an array slice:
        my ($hour, $wday) = localtime(time)[2,6];
to extract exactly those elements you are interested in.

.... at the risk of confusing someone who doesn't know what an array
slice is, as I myself at one time didn't. The differences between
@var, $var, and $var[0] are confusing enough without throwing in @var
[0]. ;-)

CC
 
E

Eric

Quoth cartercc said:
Or even better use an array slice:
        my ($hour, $wday) = localtime(time)[2,6];

It's a list slice, and it needs another set of parens:

    my ($hour, $wday) = (localtime(time))[2,6];

Of course the (time) is optional:

    my ($hour, $day) = (localtime)[2,6];
... at the risk of confusing someone who doesn't know what an array
slice is, as I myself at one time didn't. The differences between
@var, $var, and $var[0] are confusing enough without throwing in @var
[0]. ;-)

Programming for those who don't know the language is stupid, especially
when you're talking about a language feature as basic as slices. If you
were objecting to use of *foo{THING}, or %Foo::, or something equally
obscure, you might have a point.

In any case, my objection to the list slice is that noone should be
expected to remember which magic numbers correspond to which elements.
I'd write something more like

    use Time::localtime;

    my $tm = localtime;
    return ($tm->wday == 5 and $tm->hour > 17)
        or ($tm->wday == 6)
        or ($tm->wday == 0 and $hour < 17);

if I wasn't going to use Date::Calc or DateTime.

Ben


Perl is a great language but there is no better support community than
the perlers. A. Sinan, this is exactly the kind of elegance that I
suspected was possible and was looking for, Thank you. Ben, Thank you
for taking a great idea and making it even better. You've taught me
something that I'll likely use in every perl script or program that I
write going forward.

Thanks everyone for the help with this.
 
C

cartercc

Programming for those who don't know the language is stupid,

Hey, that's what they pay me for! If the people who I work for knew
how to do it for themselves, I'd be out of a job!
In any case, my objection to the list slice is that noone should be
expected to remember which magic numbers correspond to which elements.

I cheated. I copied directly from the documentation of localtime().
Did the old Ctl-C, Ctl-V (on a Windows machine). I realize it's not
kosher to cheat, but I won't tell anyone if you won't.

CC
 
R

Robert Billing

Eric said:
Perl is a great language but there is no better support community than
the perlers. A. Sinan, this is exactly the kind of elegance that I

It's only fair to point out that the minimal solution exists, but is far
too unreadable to be good practice.

( ( time() - $k1 ) % $k2 ) < $k3

where k1 is the number of seconds from the epoch to the first window, k2
is the number of seconds in a week and k3 is the size of the window.
However this depends on knowing the epoch, which is a Bad Thing, and
doing arithmetic with raw time() values which is a Worse Thing. This
version is really only good for winning obfuscation contests, you
wouldn't want it in the real world.
 
D

Dr.Ruud

cartercc said:
Eric:

localtime() returns both the hour and the weekday, like this:

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime
(time);
print qq(day is $wday and hour is $hour\n);
my $quiet_window = 1;
#default
if ($wday == 5 and $hour > 17) { $quiet_window = 0; } #Friday night
elsif ($wday == 6) { $quiet_window = 0; }
#Saturday
elsif ($wday == 0 and $hour < 17) { $quiet_window = 0; } #Sunday
morning
else { $quiet_window = 01 }
#default again

ITYM: $wday == 5 and $hour > 16
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top