DayOfNthWeek

 view release on metacpan or  search on metacpan

DayOfNthWeek.pm  view on Meta::CPAN


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.

=head2 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(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";   }

=head2 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.

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.

=head2 EXPORT

None by default

=head1 SEE ALSO

localtime(), examples distributed with module

=head1 AUTHOR

Andy Murren, E<lt>amurren@cpan.orgE<gt>

=head1 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. 

=cut



( run in 1.308 second using v1.01-cache-2.11-cpan-2398b32b56e )