perl dates: a notify alert window

Discussion in 'Perl Misc' started by Eric, Mar 2, 2009.

  1. Eric

    Eric Guest

    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
    Eric, Mar 2, 2009
    #1
    1. Advertising

  2. Eric

    cartercc Guest

    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,$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
    cartercc, Mar 2, 2009
    #2
    1. Advertising

  3. Eric <> 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
    Jürgen Exner, Mar 2, 2009
    #3
  4. Eric <> wrote in news:7dd6096e-4103-43b3-b413-
    :

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

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
    A. Sinan Unur, Mar 2, 2009
    #4
  5. cartercc <> 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,$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
    Jürgen Exner, Mar 2, 2009
    #5
  6. Eric

    cartercc Guest

    On Mar 2, 3:25 pm, Jürgen Exner <> 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,$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
    cartercc, Mar 2, 2009
    #6
  7. Eric

    Eric Guest

    On Mar 2, 5:34 pm, Ben Morrow <> wrote:
    > Quoth cartercc <>:
    >
    > > On Mar 2, 3:25 pm, Jürgen Exner <> 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.
    Eric, Mar 3, 2009
    #7
  8. Eric

    cartercc Guest

    On Mar 2, 5:34 pm, Ben Morrow <> 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
    cartercc, Mar 3, 2009
    #8
  9. 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.
    Robert Billing, Mar 3, 2009
    #9
  10. Eric

    Dr.Ruud Guest

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

    --
    Ruud
    Dr.Ruud, Mar 3, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    858
    Rob Schieber
    Sep 30, 2005
  2. Mersh
    Replies:
    0
    Views:
    483
    Mersh
    Mar 13, 2007
  3. Ganesh
    Replies:
    0
    Views:
    548
    Ganesh
    Jun 29, 2007
  4. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    189
    Mark Schupp
    Aug 9, 2004
  5. Replies:
    1
    Views:
    202
    Jano Svitok
    Jul 17, 2007
Loading...

Share This Page