0

Hi Perl,

I am looking for code where i can calculate a first saturday for all even months and first saturday for all odd Months.
Moreover i need a code to find a second saturady for all months of the year.

Can you please help me out that how i can find required information..?

2
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by d5e5
0

One way of finding the first Saturday of each month for a given year:

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

use Date::Simple;

my $year = 2011;
my @month_names = qw(January February March
                    April May June July August
                    September October November December);

foreach('01'..'12'){
    my $dt = first_saturday($_);
    my $mth = $month_names[$_ - 1];
    say "The first Saturday in $mth of $year is $dt";
}

sub first_saturday{
    my $mnbr = shift;
    my $date = Date::Simple->new("$year-$mnbr-01");
    my ($first_day, $first_sat);
    $first_day = $date->day_of_week();
    
    if ($first_day == 6){
        $first_sat = 0;
    }
    else{
        $first_sat = 6 - $first_day;
    }
    return $date + $first_sat;
}
0

The following contains a subroutine that returns an array of all the Saturday dates in a given month and year.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

use Date::Simple;

my @month_names = qw(January February March
                    April May June July August
                    September October November December);

foreach('01'..'12'){
    my ($year, $dt_arref) = get_saturdays_in_month(2011, $_);
    my $mth = $month_names[$_ - 1];
    say "The first Saturday in $mth of $year is $$dt_arref[0]";
    say "The second Saturday in $mth of $year is $$dt_arref[1]\n";
}

sub get_saturdays_in_month{
    my $yr =shift;
    my $mnbr = shift;
    my $date = Date::Simple->new("$yr-$mnbr-01");
    my $first_day;
    my @saturdays;
    $first_day = $date->day_of_week();
    
    while ($date->month == $mnbr){
        push @saturdays, $date if $date->day_of_week == 6;
        $date = $date->next;
    }
    return $yr, \@saturdays;
}
0

You may prefer the following way of doing it if you don't have Date::Simple .

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;

use Time::Piece;
use Time::Seconds;

foreach('01'..'12'){
    my ($year, $dt_arref, $month_name) = get_saturdays_in_month(2011, $_);
    print "The first Saturday in $month_name of $year is $$dt_arref[0]\n";
    print "The second Saturday in $month_name of $year is $$dt_arref[1]\n\n";
}

sub get_saturdays_in_month{
    my $yr =shift;
    my $mnbr = shift;
    my $date = Time::Piece->strptime("$yr-$mnbr-01",'%Y-%m-%d');
    my $mthname = $date->strftime('%B');
    my @saturdays;
    
    while ($date->strftime('%m') == $mnbr){
        push @saturdays, $date->strftime("%Y-%m-%d") if $date->strftime('%A') eq 'Saturday';
        $date += ONE_DAY;
    }
    return $yr, \@saturdays, $mthname;
}
0

Hi d5e5,

Thanks for your response but i am getting an error with all three above solutions.

With 1 i am getting an error like

Perl v5.10.0 required--this is only v5.8.8, stopped at check_sat.pl line 4.
BEGIN failed--compilation aborted at check_sat.pl line 4.

With 2 i am also getting same above error

and

with 3 i am getting below error.

Can't locate Time/Piece.pm in @INC (@INC contains: /usr/lib64 /usr/lib/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 /usr/lib/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl .) at check_sat.pl line 5.
BEGIN failed--compilation aborted at check_sat.pl line 5.


Just for your reference i cannot upgrade present perl version therefore i need a solution which is compatible with current version.

currently i have perl, v5.8.8 built for x86_64-linux-thread-multi install on linux machine.

Please help me out to fix this issue.

Thanks

Edited by realoneomer: n/a

0

You can avoid the "Perl v5.10.0 required--this is only v5.8.8" error by removing the use 5.010; statement (or changing it to use 5.008; ) and then changing the say statements

say "The first Saturday in $mth of $year is $$dt_arref[0]";
say "The second Saturday in $mth of $year is $$dt_arref[1]\n";

to print statements like the following:

print "The first Saturday in $mth of $year is $$dt_arref[0]\n";
print "The second Saturday in $mth of $year is $$dt_arref[1]\n\n";

You can probably eliminate the "Can't locate Time/Piece.pm..." error by installing the Time::Piece module from CPAN. I understand you are not allowed to upgrade to a newer version of Perl but you should still be able to install modules from CPAN.

0

Also you can try running the following which uses only standard modules that you probably already have.

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;

use constant (ONE_DAY => 86400);#Number of seconds in a day
use POSIX;

foreach(0 .. 11){
    my ($year, $dt_arref, $month_name) = get_saturdays_in_month(2011, $_);
    my $dtstr = strftime('%F', localtime($$dt_arref[0]));
    print "The first Saturday in $month_name of $year is $dtstr\n";
    $dtstr = strftime('%F', localtime($$dt_arref[1]));
    print "The second Saturday in $month_name of $year is $dtstr\n\n";
}

sub get_saturdays_in_month{
    my $yr =shift;
    my $mn = shift;
    my $da = 1;
    my $yrnbr = $yr - 1900;

    my $dt = mktime(0, 0, 0, $da, $mn, $yrnbr);
    my $datestr = ctime($dt);
    
    my $mthname = substr($datestr,4,3);
    my @saturdays;
    #
    while (@saturdays < 4){
        my $dayname = substr($datestr,0,3);
        push @saturdays, $dt if $dayname eq 'Sat';
        $dt += ONE_DAY;
        $datestr = ctime($dt);
    }
    return $yr, \@saturdays, $mthname;
}
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.