Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date comparison function 2

Status
Not open for further replies.

asheikh

MIS
Mar 26, 2004
2
0
0
US
Does any one know how to compare two days in KSH scripts.

Example :

date1="2006-10-02"
date2="2005-11-23"

compare both days to get difference in number of days

Any tips, help would be appreciated
 
here's something to start with - the idea is to convert your dates to Julian format and do the math [substraction] in Julian format.

date2julian

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Not to elegant, but there is surely a better solution for ")/86400" and there is some way to omit bc.

Code:
echo "("$(date -d 2006-10-02 +%s)-$(date -d 2005-11-32 +%s)")/86400" | bc
303

seeking a job as java-programmer in Berlin:
 
That -d is very handy! Unfortunately not available in Solaris... works on SCO OpenServer and with GNU date though.

Annihilannic.
 
works on SCO OpenServer
Which version ? (not on my 5.0.7)
 
Hmm... sorry, I must have typed man date in the wrong session. [blush]

Annihilannic.
 
Not wanting to reinvent the wheel, but this is a ksh script solution (granted, it does not work for pre-gregorian-calendar dates: i.e. both dates must be after 1582-10-15):

Code:
#!/bin/ksh

# parameters
# days in year
yd=365
# days in leapyear
ld=366
# elapsed days before month x in non-leap year
mo[1]=0   mo[2]=31  mo[3]=59  mo[4]=90   mo[5]=120  mo[6]=151
mo[7]=181 mo[8]=212 mo[9]=243 mo[10]=273 mo[11]=304 mo[12]=334
# elapsed days before month x in leap year
ml[1]=0   ml[2]=31  ml[3]=60  ml[4]=91   ml[5]=121  ml[6]=152
ml[7]=182 ml[8]=213 ml[9]=244 ml[10]=274 ml[11]=305 ml[12]=335


# variables
date1=2003-03-01
date2=2004-03-01


leapyear()
{
# is year $1 a leapyear?
yr=$1
ly=0
if (( !(yr%4) ))
then
 if (( !(yr%100) ))
 then
  if (( !(yr%400) ))
  then
   ly=1
  fi
 else
  ly=1
 fi
fi
echo ${ly}
}

julian()
{
# convert month, day to julian: day of the year
l=$1
m=$2
d=$3
if (( l==0 ))
then
 ((ju=mo[m]+d))
else
 ((ju=ml[m]+d))
fi
echo ${ju}
}


echo $date1|tr '-' ' '|read y1 m1 d1
l1=$(leapyear $y1)
j1=$(julian $l1 $m1 $d1)
echo $date2|tr '-' ' '|read y2 m2 d2
l2=$(leapyear $y2)
j2=$(julian $l2 $m2 $d2)

((diff=0))
if ((y1==y2))
then
 ((diff=j2-j1))
else
 if ((y1<y2))
 then
  factor=1
 else
  # switch dates, negative diff
  echo ${y1} ${m1} ${d1} ${l1} ${j1} ${y2} ${m2} ${d2} ${l2} ${j2}|read y2 m2 d2 l2 j2 y1 m1 d1 l1 j1
  factor=-1
 fi
 # days until end of year of begin date
 if ((l1==1))
 then
  ((diff=ld-j1))
 else
  ((diff=yd-j1))
 fi
 ((y1+=1))
 # keep adding yd or ld for every year or leapyear passed
 while ((y1<y2))
 do
  l1=$(leapyear $y1)
  if ((l1==1))
  then
   ((diff+=ld))
  else
   ((diff+=yd))
  fi
  ((y1+=1))
 done
 # add julian date of end date
 ((diff+=j2))
 # positive or negative difference?
 ((diff*=factor))
fi

echo "difference between ${date1} and ${date2} is ${diff} days"

# dtcalc
diff between 2003-03-01 and 2004-03-01 is 366 days

HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top