Date::DayOfNthWeek - Simple Perl module for finding the first, last or the Nth (Sun .. Sat) of the month.
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 = day_week($wday,$week);
Date::DayOfNthWeek - Simple Perl module for finding out if today is the first, last or the Nth (Sun .. Sat) 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 day_week($,$); # 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
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
day_week($,$); # 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-6]. Returns 1 if today is the that day of 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 (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(day_week);
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 = day_week($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 = day_week($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 = day_week($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"; }
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.
See the Examples directory for more info and test scripts.
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.
None by default
localtime(), examples distributed with module
Andy Murren, <amurren@cpan.org>
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.
To install Date::DayOfNthWeek, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Date::DayOfNthWeek
CPAN shell
perl -MCPAN -e shell install Date::DayOfNthWeek
For more information on module installation, please visit the detailed CPAN module installation guide.