Operating System - HP-UX
1833771 Members
2465 Online
110063 Solutions
New Discussion

How to determine if this is the 5th Friday in a month?

 
SOLVED
Go to solution
David Yandry
Frequent Advisor

How to determine if this is the 5th Friday in a month?

Greetings,

We do some special payroll processing during months which have a 5th Friday. I have found some methods to determine if this is the last Friday but nothing to tell me if this is the fifth Friday. To make things more complicated, our payroll processing is done on Mondays so I can't even use the date command. What I really need is a cron command that runs on Mondays that determines if there is a 5th Friday in this week. I'm stumped. Any ideas?

Thanks,
David
8 REPLIES 8
John Poff
Honored Contributor

Re: How to determine if this is the 5th Friday in a month?

Hi David,

Here is one way to do it with a little bit of a hack using the 'cal' command in a shell script. There are probably better ways to do it out there, but here is my quick take:

# Test for a fifth Friday in this month.

FIFTHFRI=$(cal 10 2003 | tail +7 | awk '{print $6}')
if [[ ! -z $FIFTHFRI ]]; then
echo "There is a fifth Friday this month on this date: $FIFTHFRI"
else
echo "There is no fifth Friday this month."
fi

You can use replace the 'cal 10 2003' with 'cal $1 $2' so that you can pass the month and year as parameters, or you can just make it 'cal | ' and use the current month.

JP
Mark Grant
Honored Contributor

Re: How to determine if this is the 5th Friday in a month?

Here's a perl version

call it with an argument of

date "+%d%m%y"

#!/usr/bin/perl

($day,$month,$year)=split ":",$ARGV[0];
$year+=2000;

@CAL=`cal $month $year`;

pop(@CAL);;
$LASTLINE=pop(@CAL);

($sun,$mon,$tue,$wed,$thu,$fri,$sat)=split " ",$LASTLINE;

if($mon=$day and $fri>0){
print "This is a fifth friday\n";
}
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to determine if this is the 5th Friday in a month?

Hi David:

A question similar to yours came up a few days ago (although it was a 4th Friday, I think) and he could use the date command but anyway in response to his problem, I added a -N option to caljd.sh that will tell you the occurrence of a given weekday. e.g 1 for first, 2 for 2nd, ...

In your case, you would run a cron job EVERY Monday and then the job itself will determine if this is the 5th week.

if [[ $(caljd.sh -n 4 -N) -eq 5 ]]
then
echo "Do your thing"
fi

The idea is that we find the julian day 4 days from Monday (-n 4 or Friday) and then report the occurrence of THAT weekday.

You must use caljs.sh version 2.21; none of the earlier versions have '-N'. Also, make sure that caljd.sh is in your PATH --- which must be explicitly set in your cron'ed script ---- because cron has an intentionally very sparse environment.

Here's caljd.sh, Vrsn 2.21.


If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to determine if this is the 5th Friday in a month?

And here is the Perl version, caljd.pl. Use it exactly like caljd.sh; the options are the same.

If it ain't broke, I can fix that.
Mark Grant
Honored Contributor

Re: How to determine if this is the 5th Friday in a month?

Doh!

Just noticed that I got the argument wrong in my previous post.

The script should be called with

date "+%d:%m:%y"

It should be run on a Monday and will tell you if that week contains a Friday that is the fifth Friday of the month. Or it looks like it will to me :)
Never preceed any demonstration with anything more predictive than "watch this"
David Yandry
Frequent Advisor

Re: How to determine if this is the 5th Friday in a month?

Thanks guys. I really like A. Clay's method because it is a one-liner.

Clay, is there an easy way to test to see if this really works? All I can test using your example appears to be the current date.

Thanks,
David

--- points to follow
A. Clay Stephenson
Acclaimed Contributor

Re: How to determine if this is the 5th Friday in a month?

David, do a caljd.sh -u for full usage but

caljd.sh -n 4 -N 10 27 2003 will test for Oct 10, 2003.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to determine if this is the 5th Friday in a month?

David,

I'm an idiot, I meant to say that

caljd.sh -n 4 -N 10 27 2003

will test for 4 days after Oct 27, 2003 and return the occurrence of that weekday (Friday) with the month.


If it ain't broke, I can fix that.