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

Global with four saturday-dates

Quote Reply
Global with four saturday-dates
A customer buys a product today.
We need the dates for the four following saturdays.

Example:
Today is 2004-04-01.
Then the output should be
2004-04-03
2004-04-10
2004-04-17
2004-04-24
Exception: If you buy the product at saturday, this saturday shouldnt be included!
The next saturday is the first date!

I need the output in hiddenfields!
e.g.
<input type="hidden" name=date1 value="2004-04-03">
<input type="hidden" name=date2 value="2004-04-10">
<input type="hidden" name=date3 value="2004-04-17">
<input type="hidden" name=date4 value="2004-04-24">

Thanks in advance,

Coyu
Quote Reply
Re: [Coyu] Global with four saturday-dates In reply to
See if Date::Calc is installed on your server.

If not, you can put pCalc ( a pure perl replacement) in your Plugins::Date directory.

Read the documentation, and it will do just what you want.

Call it from a global with use Plugins::Date::Pcalc

Or, you could call it directly, for some functions, in the template <%Plugins::Date::Pcalc::function()%>

I used this on a few things to provide the date of recurring meetings (3rd tuesday of the month) and such for different groups in Gforum and Links.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [Coyu] Global with four saturday-dates In reply to
What about this (these functions are from memory so they may need looking up):

<%set today=GT::Date::date_transform($date,'%yyyy%-%mm%-%dd%','%dddd%')%>
<%if today eq "Saturday"%><%set date1 = GT::Date::date_add($date,7)%>
<%elsif today eq "Friday"%><%set date1 = GT::Date::date_add($date,1)%>
...
<%endif%>
<%set date2=GT::Date::date_add($date1,7)%>
<%set date3=GT::Date::date_add($date1,14)%>
<%set date4=GT::Date::date_add($date1,21)%>
Quote Reply
Re: [afinlr] Global with four saturday-dates In reply to
I've had some problems with the date function, which is why I've used pcalc in some cases over the GT::Date routines for calendar math.

If you were going to do it using GT::Date, you'd probably be better off doing it in a global, and avoiding issues with the parser. Since you are happy with the Links default date format:

Code:
sub {
Links::init_date();
my $today = GT::Date::date_get();
my $week1 = GT::Date::date_add($today, 7);
my $week2 = GT::Date::date_add($today, 14);
my $week3 = GT::Date::date_add($today, 21);
my $week4 = GT::Date::date_add($today, 28);
return ( {date1 => $week1, date2 => $week2, date3 => $week3, date4 => $week4} );
}


call this as <%get_dates%> or something like that.

Untested, but should work.

The problems I've hit have come with date formatting, and -- in your examples -- assigning functions in the templates. This is a simpler case than I initially took it for, so it's possible to do it as above, without much potential for problems.

Rather than simply adding 7 to today, I was looking at it as actually picking out the next 4 DDD-days of the week.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
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
Quote Reply
Re: [pugdog] Global with four saturday-dates In reply to
btw... the next version of links will contain a payment module, and maybe the date routines will be enhanced to work along with it.

While there are certainly advantages at times for using the built-in Links date routines (especially for standard program calls), Date::Calc / Date:Pcalc are the best [and most standard] modules out there for most date calculations in plugins and globals. Don't be afraid to use them ;)

If you can compile Date::Calc into your perl, it will become one of your favorite modules :) It's required for most calendar programs and such.

Pcalc can be put into your Plugins directory, in the Date subdirectory, and requires no compillation.


PUGDOG� Enterprises, Inc.

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