Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi i have an issue with the post by Jim Boothe which shows to calculate the values of the week number. the issue is when for the dates other than that of 08 and 09 of every month this code works fine for 08 and 09 the script fails it gives the error "date.sh[17]: 08: The specified number is not valid for this command."
# !/bin/ksh
calcweek() {
if [ $LEAP -eq 0 ] ; then
set -A DIM 0 0 31 59 90 120 151 181 212 243 273 304 334
else
set -A DIM 0 0 31 60 91 121 152 182 213 244 274 305 335
fi
((JDAY=DIM[$MM]+DD))
((WEEK=(JDAY+$1)/7))
echo $YEAR-$WEEK
}MM=$1
DD=$2
YEAR=$3MMDD=$MM$DD
typeset -Z2 WEEK((YEAR%100)) && ((LEAP=!(YEAR%4))) || ((LEAP=!(YEAR%400)))
cal 01 $YEAR |
awk '{getline;getline;print NF;exit}' |
read W1Kcase $W1K$LEAP in
70) echo '0101 52 9999 5' ;;
10) echo '0102 XX 9999 4' ;;
20) echo '0103 53 9999 3' ;;
30) echo '0000 00 9999 9' ;;
40) echo '0000 00 1229 8' ;;
50) echo '0000 00 1230 7' ;;
60) echo '0000 00 1231 6' ;;
71) echo '0101 52 1231 5' ;;
11) echo '0102 52 9999 4' ;;
21) echo '0103 53 9999 3' ;;
31) echo '0000 00 9999 9' ;;
41) echo '0000 00 9999 8' ;;
51) echo '0000 00 1229 7' ;;
61) echo '0000 00 1230 6' ;;
esac | read JCUT PWK DCUT ADJif [ $MMDD -le $JCUT ] ; then
if [ $PWK = 'XX' ] ; then
((PYEAR=YEAR-1))
((PYEAR%100)) && ((PLEAP=!(PYEAR%4))) || ((PLEAP=!
(PYEAR%400)))
((PWK=52+PLEAP))
fi
echo $((YEAR=YEAR-1))-$PWK
else
if [ $MMDD -ge $DCUT ] ; then
echo $((YEAR=YEAR+1))-01
else
calcweek $ADJ
fi
fiexit 0

The code above came from Nov 2004 I believe, and it calculates the ISO calendar week of the year, which is a bit tricky.
ISO weeks begin on Mondays, and week #1 of each year is the first week having a majority of its days (4+) in the current year. This is also the week that contains Jan 4. The first week of a year can begin as early as Dec 29 of the previous year. Also, as many as 3 days of January can be in the last week of the previous year, which could be either week 52 or 53.
I just ran the above code on HP-UX for several days of Aug 2005, including the 8th and 9th, with no problems, so it will be difficult for me to debug.
$ ./isoweekT.sh 08 01 2005
2005-31$ ./isoweekT.sh 08 07 2005
2005-31$ ./isoweekT.sh 08 08 2005
2005-32$ ./isoweekT.sh 08 09 2005
2005-32$ ./isoweekT.sh 08 10 2005
2005-32$ ./isoweekT.sh 08 11 2005
2005-32

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |