commit 995026f8d3c4d98dc17517381435f598e1b50e65
parent bf5c3e2453040554e823420d8a36b61d5fec2ac0
Author: Samir Parikh <noreply@samirparikh.com>
Date: Wed, 27 Apr 2022 15:56:45 +0000
get tests looking for date of Last occurrence of day to pass
Diffstat:
1 file changed, 32 insertions(+), 11 deletions(-)
diff --git a/meetup/Meetup.pm b/meetup/Meetup.pm
@@ -5,7 +5,7 @@ use Exporter qw<import>;
our @EXPORT_OK = qw<meetup>;
#use Data::Dumper;
-use Date::Calc qw( Nth_Weekday_of_Month_Year );
+use Date::Calc qw( Nth_Weekday_of_Month_Year Days_in_Month );
sub meetup {
my ($desc) = @_;
@@ -24,21 +24,42 @@ sub meetup {
@week_number{ qw(
First Second Third Fourth Fifth Last Teenth
) } = ( 1 .. 7 );
+ my $month_num = $month_number{ $input{ month } };
+ my $day_num = $day_number{ $input{ day_of_week } };
+ my $week_num = $week_number{ $input{ week } };
+ # if we are asked to find the last day in the month
+ if ( $input{ week } eq 'Last' ) {
+ my $days_in_month = Days_in_Month( $input{ year }, $month_num );
+ my ($year, $month, $day) =
+ Nth_Weekday_of_Month_Year(
+ $input{ year },
+ $month_num,
+ $day_num,
+ 3 # find the day of month of the third occurrence of the day
+ );
+ # $day now holds the day of the month of the third occurence of the day
+ # Check what would be the date 2 weeks from that day.
+ # If the date exceeds the number of days in the month, then the last
+ # occurrence of that day occurs in week 4. Otherwise, it occurs in
+ # week 5.
+ $week_num = $day + 14 > $days_in_month ? 4 : 5;
+ }
#print Dumper \%input;
- print "$desc\n";
- print "year = ", $input{ year }, "\n";
- print "month number = ", $month_number{ $input{ month } }, "\n";
- print "day number = ", $day_number{ $input{ day_of_week } }, "\n";
- print "week number = ", $week_number{ $input{ week } }, "\n";
+# print "$desc\n";
+# print "year = ", $input{ year }, "\n";
+# print "month number = ", $month_number{ $input{ month } }, "\n";
+# print "day number = ", $day_number{ $input{ day_of_week } }, "\n";
+# print "week number = ", $week_number{ $input{ week } }, "\n";
#return undef;
my ($year, $month, $day) =
Nth_Weekday_of_Month_Year(
$input{ year },
- $month_number{ $input{ month } },
- $day_number{ $input{ day_of_week } },
- $week_number{ $input{ week } }
- ) if $week_number{ $input{ week } } < 6;
- return"$year-$month-$day";
+ $month_num, #$month_number{ $input{ month } },
+ $day_num, #$day_number{ $input{ day_of_week } },
+ $week_num #$week_number{ $input{ week } }
+ ) if $week_num < 6; #$week_number{ $input{ week } } < 6;
+ my $result = sprintf( "%04d-%02d-%02d", $year, $month, $day );
+ return $result;
}
1;