Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Re: [pugdog] Global with four saturday-dates

Quote Reply
Re: [pugdog] Global with four saturday-dates In reply to
Actually, looking at the problem again, it would be better to use the date module. He wants to find the next 4 saturdays. While you can use other routines, and work it all out, it's clearer and more maintainable to use standard modules.

This specific need does not require a passed in date, because it's referenced from today. If you did want to use a passed in date, you would need to do some additional checking (ie: is the date greater than 4 weeks old?)

using pcalc: (indexes start at 1, not 0)
Code:
use Date::Pcalc qw( Today Day_of_Week Add_Delta_Days );
my $searching_dow = 6; # 6 = Saturday ## day of week you want to find
my @today = Today(); ## format is year month day, perl will flatten passed data structures
my $current_dow = Day_of_Week(@today); ## today is
my ($year,$month,$day) = Add_Delta_Days(@today, $searching_dow - $current_dow); ## perl flattens data when passed
my $first_date = "$year-$month-$day"; ## contains the next saturday

Assign $first_date to the "my $today" variable in the previous code, rather than GT::Date::date_get(); and you can use Link's internal processes to get the next 4 dates.

To screen out if today is a saturday

($first_date == GT::Date::date_get() ) && ( $first_date = GT::Date::date_add($first_date, 7) );

Code:
sub {
use Plugins::Date::Pcalc qw( Today Day_of_Week Add_Delta_Days );
my $searching_dow = 6; # 6 = Saturday ## day of week you want to find
my @today = Today(); ## format is year month day, perl will flatten passed data structures
my $current_dow = Day_of_Week(@today); ## today is
my ($year,$month,$day) = Add_Delta_Days(@today, $searching_dow - $current_dow); ## perl flattens data when passed
my $first_date = "$year-$month-$day"; ## contains the next saturday

## switch to Links date routines, returns proper format for simple calcs
Links::init_date();
($first_date == GT::Date::date_get() ) && ( $first_date = GT::Date::date_add($first_date, 7) );
my $week2 = GT::Date::date_add($first_date, 7);
my $week3 = GT::Date::date_add($first_date, 14);
my $week4 = GT::Date::date_add($first_date, 21);
return ( {date1 => $first_date, date2 => $week2, date3 => $week3, date4 => $week4} );
}

PS: just because something *can* be done in 1 or 2 lines, doesn't mean it should be done in 1 or 2 lines. Shorter isn't always better, and maintainability/readability especially in infrequently used or called routines is more important (usually) than terseness. The check for "today" could have been done before the add_delta_days, but logically, it fits better in the lower block of code.


PUGDOG´┐Ż Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.

Last edited by:

pugdog: Apr 2, 2004, 5:01 AM
Subject Author Views Date
Thread Global with four saturday-dates Coyu 2011 Mar 31, 2004, 11:58 PM
Post Re: [Coyu] Global with four saturday-dates
pugdog 1904 Apr 1, 2004, 6:20 AM
Thread Re: [Coyu] Global with four saturday-dates
afinlr 1900 Apr 1, 2004, 7:29 AM
Thread Re: [afinlr] Global with four saturday-dates
pugdog 1893 Apr 1, 2004, 3:50 PM
Thread Re: [pugdog] Global with four saturday-dates
pugdog 1877 Apr 2, 2004, 4:54 AM
Post Re: [pugdog] Global with four saturday-dates
pugdog 1875 Apr 2, 2004, 5:12 AM