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..?

Dani AI

Generated

Short answer: find the weekday of the 1st day of each month, compute how many days to the next Saturday, add that to get the first Saturday, and then add 7 days to get the second Saturday. Filter months by parity (month % 2 == 0 for even, 1 for odd). That algorithm is language‑agnostic and guaranteed to produce valid first/second Saturdays for any year.

Here is a small, portable Python example you can run without extra packages:

from datetime import date, timedelta

def first_and_second_saturdays(year, month):
    first = date(year, month, 1)
    # weekday(): Monday=0 .. Sunday=6 ; Saturday == 5
    offset = (5 - first.weekday()) % 7
    first_sat = first + timedelta(days=offset)
    second_sat = first_sat + timedelta(days=7)
    return first_sat, second_sat

year = 2011
even_firsts = []
odd_firsts = []
for m in range(1, 13):
    f, s = first_and_second_saturdays(year, m)
    (even_firsts if m % 2 == 0 else odd_firsts).append(f)

print("Even-month first Saturdays:", even_firsts)
print("Odd-month first Saturdays: ", odd_firsts)

If you must stick with older Perl (as reported), translate the same logic: determine the weekday of the 1st, compute the offset to Saturday, first = 1 + offset, second = first + 7. If installing CPAN modules is allowed you can use a date library to simplify things; if not, use core date/time facilities but prefer date-aware routines rather than repeatedly adding a fixed number of seconds (that can give surprising results across DST/localtime boundaries).

Quick troubleshooting tips: verify interpreter and module availability before you start (for Perl: perl -v and a quick perl -MModuleName -e1 test; for Python just python3 -c "import datetime"). Credit to for the Perl-focused examples earlier in the thread — the algorithm above is the same idea, presented here as a compact, cross-platform Python alternative and with notes for safe Perl translation.

Recommended Answers

All 6 Replies

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;
}

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;
}

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;
}

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_ line 4.
BEGIN failed--compilation aborted at check_ line 4.

With 2 i am also getting same above error

and

with 3 i am getting below error.

Can't locate Time/ 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_ line 5.
BEGIN failed--compilation aborted at check_ 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

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.

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.