The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get Day of Week from date vpapaiya HP-UX 5 03-07-2008 04:47 PM
How to find Day of the Week from the given date (Perl)? deepakwins UNIX for Dummies Questions & Answers 5 03-07-2008 04:45 PM
week number alisevA3 UNIX for Dummies Questions & Answers 2 10-31-2007 05:01 AM
get date of Thursday of any week Miller UNIX for Advanced & Expert Users 2 10-21-2007 11:47 PM
Getting week number of the month sbasetty Shell Programming and Scripting 21 11-06-2006 06:13 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-09-2005
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
calcuate the week number of a given date

Hi All,

can any one help me fix the error in this - i am still a novice in shell programming. I got this code after some googling now the code works with all the dates( as much as i know) except for 08 th and 09th of every month. can any one of you please help me fix this issue?

Thanks in advance.

Quote:
# !/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
  #2 (permalink)  
Old 09-09-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Posix, in their wisdom, decided that a leading zero indicates an octal constant so 08 and 09 are illegal octal constants. This change broke a lot of code. Did you test for month=8 or 9? Also week number = 8 or 9?

Define precisely what you mean by week number. Are the first 7 days of the year always week 1?
  #3 (permalink)  
Old 09-09-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
If you have perl...
Code:
#!/usr/bin/ksh
YEAR=2005
MM=05
DD=08
WEEKNUM=$(perl -e '
   use Time::Local;
   $DATE = timelocal(0, 0, 0, $ARGV[2], $ARGV[1]-1, $ARGV[0]-1900);
   $YEARDAY = (localtime $DATE)[7];
   $WEEKNUM = int($YEARDAY / 7) + 1;
   print $WEEKNUM;
   ' $YEAR $MM $DD)
echo Week number is $WEEKNUM
Test result...
Code:
Week number is 19
Note that you can get the current week number by using...
Code:
date +%W
  #4 (permalink)  
Old 09-09-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Ygor, look at this.

Code:
$ date --date="2005/05/08" +%W 
18
$ ./ygor.ksh                   
Week number for 2005/05/08 is 19

and this

Code:
$ date --date="2005/09/09" +%W 
36
$ ./ygor.ksh                   
Week number for 2005/09/09 is 36
Edit:

Code:
$ date --date="2005/12/31" +%W 
52
$ ./ygor.ksh                   
Week number for 2005/12/31 is 53
Why the difference for an old date and same results for today ? Looks like boundary cases are coming into play.

vino
  #5 (permalink)  
Old 09-09-2005
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
First of all thank you all for the replies!!!!

Perderabo - I have checked for the month 08 and 09 it is working perfectly fo r other dates. it also works for the week 09 and 08 but the issue is only with the day being 09 or 08 any suggestions would be of great help. I have seen your code of sword fish(Gosh) thats a wonderful peice of work. I hope you have a solution for this as well. it is calculated with the ISO way like the first week could start as soon as 28th of dec or as late as 3rd of jan.

Oops!!!! and for perl -i dont have perl

Last edited by ahmedwaseem2000; 09-09-2005 at 06:11 AM..
  #6 (permalink)  
Old 09-09-2005
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
Guys got the solution - i have added this if statement before calculation. please let me know if you find this is not good.

Code:
# !/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
if [ $DD -eq 08 ];then
DD=8
elif [ $DD -eq 09 ];then
DD=9
fi
((JDAY=DIM[$MM]+DD))
echo $DD day $MM mon
echo $JDAY
((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

Last edited by Perderabo; 09-09-2005 at 01:06 PM.. Reason: Switch quote tags to code tags for readability
  #7 (permalink)  
Old 09-09-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Thanks for posting your final solution. I don't recall ever hearing of ISO week numbers before. So I will add this thread to our date arithmetic FAQ article. I found this page which has an on-line calculator for ISO week numbers.

For the record, here is a datecalc based solution. It emulates the behavior of the on-line calculator in that it outputs a year, a week number, and a day of week number...
Code:
#! /usr/bin/ksh
month=$1
day=$2
year=$3
typeset -Z2 fwk

function calcisoweek
{
        integer year month day dow1y mjd1y mjd isoweek
        year=$1
        month=$2
        day=$3
        dow1y=$(datecalc -d $year 1 1)
        ((dow1y=(dow1y+6)%7))
        mjd1y=$(datecalc -j $year 1 1)
        mjd=$(datecalc -j $year $month $day)
        ((isoweek=(mjd - mjd1y - dow1y + 7 + 3 )/7))
        echo $isoweek
        return 0
}

dow=$(datecalc -d $year $month $day)
((!dow)) && dow=7
fwk=$(calcisoweek $year $month $day)
if ((!fwk)) ; then
        ((year=year-1))
        fwk=$(calcisoweek $year 12 31)
fi
echo ${year}-W${fwk}-${dow}
exit 0
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0