Re: How to get 5th Saturday? (456 Views)
Reply
Frequent Advisor
Ryan Clerk
Posts: 44
Registered: ‎05-12-2002
Message 1 of 11 (456 Views)
Accepted Solution

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
Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 2 of 11 (456 Views)

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...
Honored Contributor
Mel Burslan
Posts: 3,214
Registered: ‎08-26-1998
Message 3 of 11 (456 Views)

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...
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 4 of 11 (456 Views)

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
Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 5 of 11 (456 Views)

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
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 6 of 11 (456 Views)

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.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 7 of 11 (456 Views)

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.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 8 of 11 (456 Views)

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.
Advisor
Craig Sterley
Posts: 29
Registered: ‎08-24-2005
Message 9 of 11 (456 Views)

Re: How to get 5th Saturday?

How about using cron to a generic messaging script?

18 * * * 6 [`date "+%d"` -gt 28] && /path/to/script
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 10 of 11 (456 Views)

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.
Respected Contributor
Greg Vaidman
Posts: 252
Registered: ‎09-12-2000
Message 11 of 11 (456 Views)

Re: How to get 5th Saturday?

seems like a lot of trouble - the following snippet will return the 5th saturday of the current month, or a blank if there is none:

cal | awk 'NR==7{print $7}'

you could run this on the first of each month and based on the output, generate the alert. for example:

sat5=$( cal | awk 'NR==7{print $7}' )
if [[ -n $sat5 ]]; then
: alert logic here
fi
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.