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

Products: Gossamer Links: Development, Plugins and Globals: Re: [pugdog] Global with four saturday-dates: Edit Log

Here is the list of edits for this post
Re: [pugdog] Global with four saturday-dates
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

Edit Log: