Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl dates: a notify alert window

Reply
Thread Tools

perl dates: a notify alert window

 
 
Eric
Guest
Posts: n/a
 
      03-02-2009

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
 
Reply With Quote
 
 
 
 
cartercc
Guest
Posts: n/a
 
      03-02-2009
Eric wrote:
> 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,$isd st) = 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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      03-02-2009
Eric <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-02-2009
Eric <(E-Mail Removed)> wrote in news:7dd6096e-4103-43b3-b413-
http://www.velocityreviews.com/forums/(E-Mail Removed):

>
> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-02-2009
cartercc <(E-Mail Removed)> wrote:
>Eric wrote:
>> 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,$isd st) = 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,$isd st) =
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
 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      03-02-2009
On Mar 2, 3:25*pm, Jürgen Exner <(E-Mail Removed)> wrote:
> 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,$isd st) = * * * * * * *
> * * * * 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
 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      03-03-2009
On Mar 2, 5:34*pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth cartercc <(E-Mail Removed)>:
>
> > On Mar 2, 3:25*pm, Jürgen Exner <(E-Mail Removed)> wrote:

>
> > > 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];
>
> > > 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].

>
> 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.



 
Reply With Quote
 
cartercc
Guest
Posts: n/a
 
      03-03-2009
On Mar 2, 5:34*pm, Ben Morrow <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
Robert Billing
Guest
Posts: n/a
 
      03-03-2009
Eric wrote:

>> 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


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.
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      03-03-2009
cartercc wrote:
> 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.

>
> localtime() returns both the hour and the weekday, like this:
>
> my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isd st) = 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

--
Ruud
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Alert..General Alert?..New Discovery?. =?Utf-8?B?U3BhbW1lcipLaWxsZXI=?= Wireless Networking 0 07-24-2007 03:36 PM
on click in popup window throws alert : need to avoid alert of postdata Ganesh ASP .Net 0 06-29-2007 06:51 AM
How to create a form Alert - Not using Alert Boxes Mersh Java 0 03-13-2007 04:14 PM
VPN log - Received Unexpected InitialContact Notify (PLMgrNotify:886) reo Cisco 0 04-20-2005 09:46 AM
ALERT: Virus Scam Alert! Toronto Garage Door Company Computer Support 1 11-18-2003 04:16 PM



Advertisments