Operating System - HP-UX
1830648 Members
2857 Online
110015 Solutions
New Discussion

Re: Find every Thursday in a year?

 
SOLVED
Go to solution
Steve Start
Advisor

Find every Thursday in a year?

Hi,

Does anybody know how to get date or cal to show every Thursday in a year?

Thanks,
Steve
5 REPLIES 5
Steven E. Protter
Exalted Contributor

Re: Find every Thursday in a year?

This tool can do it:

http://www.hpux.ws/merijn/caljd-2.23.sh

http://www.hpux.ws/merijn/caljd-2.2.pl

You'll need to read the notes to learn how to give the tool input.

A. Clay's date magic tool!

It can do all kinds of date magic. Bookmark it, its updated periodically. I've ported it to Linux(shell change).

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
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Find every Thursday in a year?

When combined with the above mentioned caljd.sh (with caljd.sh installed somewhere in your PATH) this untested and typed "on the fly" script should do it.

#!/usr/bin/sh

typeset PROG=${0##*/}

usage()
{
echo "\n${PROG}: year wkday\n" >&2
echo "year - 4-digit year" >&2
echo "wkday: 0 - Sun, 1 - Mon, ... 6 - Sat\n" >&2
return 255
} # usage


typeset -i STAT=0
if [[ ${#} -ge 2 ]]
then
typeset -i YR=${1}
typeset -i WKDAY=${2}
shift 2
typeset -i JDAY1=$(caljd.sh 1 1 ${YR})
typeset -i WDAY1=$(caljd.sh -w ${JDAY1})
typeset OFFSET=$((${WKDAY} - ${WDAY1}))
if [[ ${OFFSET} -lt 0 ]]
then
((OFFSET += 7))
fi
typeset -Z4 YEAR=${YR}
typeset -Z2 MONTH=0
typeset -Z2 DAY=0
typeset JD=$((${JDAY1} + ${OFFSET}))
while [[ ${YEAR} -eq ${YR} ]]
do
caljd.sh ${JD} | read MONTH DAY YEAR
if [[ ${YEAR} -eq ${YR} ]]
then
echo "${MONTH}/${DAY}/${YEAR}"
fi
((JD += 7))
done
else
usage
STAT=${?}
fi
exit ${STAT}

-------------------------------------------

Invoke it like this:
weekday.sh 2005 4

The trick is to find the 1st desired weekday in the year and then display every 7th day until the year changes. If I ain't done gone and typed something wrong, this here ought to do it for you.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Find every Thursday in a year?

Hi Steve:

To be ultra nit-picky, please make these changes:

typeset OFFSET=$((${WKDAY} - ${WDAY1}))
to
typeset -i OFFSET=$((${WKDAY} - ${WDAY1}))

and

typeset JD=$((${JDAY1} + ${OFFSET}))
to
typeset -i JD=$((${JDAY1} + ${OFFSET}))


The script will work just fine w/o these changes but I tend to be a rigorous kind of guy. I didn't notice anything else.

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

Re: Find every Thursday in a year?

Thanks, that worked great.
Muthukumar_5
Honored Contributor

Re: Find every Thursday in a year?

We can do with cal easily as,

#!/bin/sh

# Input year
year=${1:-2004}
month=1

while [[ $month -le 12 ]]
do

cal $month $year | head -1
cal $month $year | grep -v $year | cut -b 12-14
let month=month+1

done

It will give you informations there.
Easy to suggest when don't know about the problem!