NAME
Date::DayOfNthWeek - Simple Perl module for finding the first, last or the Nth Tuesday (or any other day) of the month.
SYNOPSIS
use Date::DayOfNthWeek;
# Sunday = 0, just like in localtime()
my $wday = 2;
# See if today is first Tuesday of the month
my $first = first_week($wday);
# See if today is last Tuesday of the month
my $last = last_week($wday);
# See if today is 3rd Tuesday of the month
my $week = 3;
my $last = week_day($wday,$week);
ABSTRACT
Date::DayOfNthWeek - Simple Perl module for finding out if today is the first, last or the Nth Tuesday (or any other day) of the month.
Has three functions: last_week($); # today is in the last week of the month first_week($); # today is in the first week of the month week_day($,$); # today is in the Nth week of the month
I wrote this to send out use in a cron job to send out reminders about the Morris County Perl Mongers monthly meetings. Using Date::Calc and Date::Manip where more than what I needed.
This only works for finding information about TODAY, no future calculations. If you want that use Date::Calc or Date::Manip. This is meant to
DESCRIPTION
Date::DayOfNthWeek - Simple Perl module for finding the first, last or the Nth Tuesday (or any other day) of the month.
A week is considered to start on Sunday. There may be 1 .. 7 days in the first week of the month.
Has three functions:
first_week($); # day is in the first week of the month
Takes an int between 0 and 6 and returns 1 if today is the first [Sun - Sat] of the month
last_week($); # day is in the last week of the month
Takes an int between 0 and 6 and returns 1 if today is the last [Sun - Sat] of the month
week_day($,$); # day is in the Nth week of the month
Takes an int between 0 and 6 [Sun - Sat] and an int for week of the month [1-5]. Returns 1 if today is the that day of the Nth week of the month.
EXAMPLE
I wrote this to send out use in a cron job to send out reminders about the Morris County Perl Mongers (MCPM) monthly meetings. Using Date::Calc and Date::Manip were more than what I needed.
I am using this to send out a reminder about the MCPM meetings. We meet in a local Irish Pub on the 3rd Tuesday of the month.
#!/usr/local/bin/perl
use Date::DayOfNthWeek qw(week_day);
my $d = 2; # set to the day of week I want -- SUNDAY=0 my $w = 2; # set to the week PRIOR to the meeting so I can send out the reminder
my $ok = week_day($d,$w);
if ($ok) { &nextweek; } else { my $ww = $w+1; # keeps me from changing the value of $w if ($ww > 6) { $ww = 1; } # fixes range input errors for wrapping to next week $ok = week_day($d,$ww); if ($ok) { &tonight; } else { $d--; # see if this is the day before the meeting if ($d < 0) { $d = 6; } # fixes range input error for wrapping to previous week day $ok = week_day($d,$w); &tomorrow if $ok; } }
sub nextweek { print "Meeting is next week\n"; } sub tomorrow { print "Meeting is tomorrow\n"; } sub tonight { print "Meeting is tonight\n"; }
FORMULA
The formula for calculating the week is:
(int(((Day of the Month - 1)+ Day of the Week)/7))+1
my %hash = ();
for my $c (0 ..6 ) {
my $a = $date+$c;
my $key = $a%7;
my $w = (int($a/7))+1;
$hash{$key} = $w;
}
my $q = $hash{$wday};
The trick is the hash and using the mod operation. If you don't do something like this there are several cases where the answer is wrong. This way is 100% accurate.
Here is the test script I used to check the formula
#!/usr/local/bin/perl
for $mday (1 .. 31) { $date=$mday-1;
print "Mday\tWday\tWeek\n$mday\n";
for (0 ..6 ) {
$day = $date+$_;
$wday = $day%7;
$week = (int($day/7))+1;
print "\t\t$wday\t\t$week\n";
}
}
Here are some fact that make the test info quicker to check
The 1st is always in week #1 The 8th is always in week #2 The 15th is always in week #3 The 22nd is always in week #4 The 29th is always in week #5
A month can have 4-6 weeks.
EXPORT
None by default
SEE ALSO
localtime()
AUTHOR
Andy Murren, <andy@murren.org>
COPYRIGHT AND LICENSE
Copyright 2002 by Andy Murren
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.