Operating System - Linux
1753987 Members
3588 Online
108811 Solutions
New Discussion

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

 
SOLVED
Go to solution
Hein van den Heuvel
Honored Contributor

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

I had something like that floating around.
Modified it to do the brute-force count, like Peter did.

It should basically be a simple divide by 7 of end day minus begin day but taking the begin and end day-of-the week into account made my head hurt.

Note, there is one gotcha if you play with timelocal local. You can get a surpise 3600 seconds difference on the right/wrong days.
In 2006 in the EDT zone 0h0 4/2 - 0h0 4-3 = 82800 seconds 0.958333333333333 days, not 86400 and 1.
Rounding takes care of that as needed.

Brute-force perl:

use Time::Local;
sub mytime {
my ($y,$m,$d) = split /-/,$_[0];
return timelocal(0,0,0,$d,$m-1,$y);
}
$beg = mytime(shift @ARGV);
$end = mytime(shift @ARGV);
@nam = qw/Sun Mon Tue Wed Thu Fri Sat/;
for $i (0..6) { $weeks[$i]=0 }
# barely tolerant for daylight saving jumps
for ($i=$beg; $i < $end; $i += 86400) {
$weeks[(localtime($i))[6]]++;
}
for $i (0..6) {
print "$nam[$i] $weeks[$i]\n";
}


Cheers,
Hein.
Pankaj Yadav_1
Frequent Advisor

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

I divided the no. of days by 7 and that gave me the answer.
Also I took care of the remainder by the modulus(%) operator to calculate the exact no. of Mondays, Tuesdays etc.

I have given you all points.
Thanks a lot to all of you.