Computing.Net > Forums > Unix > calcuate the week number of a given

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

calcuate the week number of a given

Reply to Message Icon

Name: ahmedwaseem2000
Date: September 8, 2005 at 23:15:03 Pacific
OS: unix
CPU/Ram: 256
Comment:

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=$3

MMDD=$MM$DD
typeset -Z2 WEEK

((YEAR%100)) && ((LEAP=!(YEAR%4))) || ((LEAP=!(YEAR%400)))

cal 01 $YEAR |
awk '{getline;getline;print NF;exit}' |
read W1K

case $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 ADJ

if [ $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
fi

exit 0




Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: September 9, 2005 at 07:15:02 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: calcuate the week number of a given

week number of a date from FY www.computing.net/answers/unix/week-number-of-a-date-from-fy/7012.html

Get a week number from a date. www.computing.net/answers/unix/get-a-week-number-from-a-date/8510.html

How to find week no. of the month? www.computing.net/answers/unix/how-to-find-week-no-of-the-month/5792.html