Operating System - Linux
1748177 Members
4300 Online
108758 Solutions
New Discussion юеВ

Re: How to get 5th Saturday?

 
SOLVED
Go to solution
Ryan Clerk
Frequent Advisor

How to get 5th Saturday?

Hello experts,

We are allowed scheduled maintenance times 4 times each year on the 5th Saturday of a month. Is there a way to find the 5th Saturdays throughout the year so that we can automatically notify our users?

Thanks,
Ryan
10 REPLIES 10
Mel Burslan
Honored Contributor

Re: How to get 5th Saturday?

I am not sure if there is a system internal command for this but a snippet of code like this will give you the month calendars


year=2007
for i in 01 02 03 04 05 06 07 08 09 10 11 12
do
cal ${i} ${year} | grep -v [A-Z,a-z] grep [0-9]
done

will give you the monthly calendars.
store them in separate files and look for ones with 5 or 6 lines. In these files, 5th saturday is the last (7th field of) line number 5.

Sorry I can not write the code for you as I have some buisness to tend right now, but this should get you going I hope.
________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor

Re: How to get 5th Saturday?

sorry I forgot a pipe between two grep commands on the line starting with "cal" command. Please beware
________________________________
UNIX because I majored in cryptology...
Steven E. Protter
Exalted Contributor

Re: How to get 5th Saturday?

Shalom,

http://www.hpux.ws/caljd.sh
http://www.hpux.ws/caljd.pl

SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
spex
Honored Contributor

Re: How to get 5th Saturday?

Hi Ryan,

Set environmental variable Y to the year of interest (e.g. Y=$(date +%Y)), and then:

for M in 01 02 03 04 05 06 07 08 09 10 11 12
do
cal ${M} ${Y} | awk -v M=${M}-v Y=${Y} 'NR==7 {if($7 !~ /^$/){printf("%s/%s/%s\n",M,$7,Y)}}'
done

PCS
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to get 5th Saturday?

Well, I wouldn't write no code for the special case of the 5th Saturday but some years back I did write some code for the general case. (I think the PC guys needed the 2nd Sunday for something or other and on account of it being Windows I wrote it in Perl.)

This should work.

dayofmonth.pl -w 6 -n 5
without a year argument the current year is assummed.
dayofmonth.pl -w 6 -n 5 2012
will work for 2012.

Invoke as dayofmonth.pl -u for full usage.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to get 5th Saturday?

You will also need the Perl script, caljd.pl, called by the previous script.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to get 5th Saturday?

I hadn't touched dayofmonth.pl in over 3 years and because caljd.pl has added the optional abbreviated and full month names which include NLS support during this time, I added the feature to dayofmonth.pl as well. This is dayofmonth.pl Vrsn 1.02p.
If it ain't broke, I can fix that.
Craig Sterley
Advisor

Re: How to get 5th Saturday?

How about using cron to a generic messaging script?

18 * * * 6 [`date "+%d"` -gt 28] && /path/to/script
Hein van den Heuvel
Honored Contributor

Re: How to get 5th Saturday?

How about a simple brute-force:

--- special-saturdays.pl ---
use strict;
my $try = time();
my $DAY = 86400; # 24 * 60 *60
for (0..365) {
$try += $DAY;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($try);
print scalar localtime($try)."\n" if ($wday == 6 and $mday > 28);
}

#perl special-saturdays.pl
Sat Jun 30 06:53:49 2007
Sat Sep 29 06:53:49 2007
Sat Dec 29 05:53:49 2007
Sat Mar 29 05:53:49 2008


Similar building your own date format...

use strict;
my $try = time();
my $DAY = 86400; # 24 * 60 *60
for (0..365) {
$try += $DAY;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($try);
printf "%d-%02d-%02d\n",$year+1900,$mon+1,$mday if ($wday == 6 and $mday > 28);
}

perl special-saturdays.pl
2007-06-30
2007-09-29
2007-12-29
2008-03-29

fwiw,
Hein.