Cover V05, I08
Article
Listing 1
Listing 2

aug96.tar


Listing 1: Bourne shell functions for creating yesterday's date

##
# alz (n) - add leading zero;  if n < 10,  returns "0n", otherwise n.
#       Result is returned in Znn because of the way return works --
#       it strips the leading zero back off.
alz () {
if  [ $1 -lt 10 ]; then
if  [ `echo "$1" | wc -c` -gt 2 ]; then
Znn="$1"
else
Znn="0$1"
fi
else
Znn="$1"
fi
return $1
}
##
# slz (n) - strip leading zero;  inverse of alz.
slz () {
return $1;
}
##
# ly (yyyy)  -  leap year; returns 1 if yyyy is a leap year, 0 otherwise.
ly () {
C4=`expr $1 % 4`
C100=`expr $1 % 100`
C400=`expr $1 % 400`
R=0
if  [ \( $C4 -eq 0 -a $C100 -ne 0 \) -o $C400 -eq 0 ]; then
R=1
fi
return $R;
}
##
# dim (mm yyyy) - days in month; returns days in month mm in year yyyy.
DIM="31 28 31 30 31 30 31 31 30 31 30 31"
dim ()  {
TMP=`echo $DIM | cut -f$1 -d' '`
ADdim=0
if  [ $1 -eq 2 ]; then
ly $2
ADdim=$?
fi
return `expr $TMP + $ADdim`;
}
##
# moy (mm) - month of year; returns value in Mmm: string representation of
#       month mm.
MOY="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
moy () {
Mmm=`echo $MOY | cut -f$1 -d' '`
return 0;
}
##
# nom (mmm) - number of month;  returns numeric value of month "mmm".
nom () {
case $1
in
"Jan")  return 1;;
"Feb")  return 2;;
"Mar")  return 3;;
"Apr")  return 4;;
"May")  return 5;;
"Jun")  return 6;;
"Jul")  return 7;;
"Aug")  return 8;;
"Sep")  return 9;;
"Oct")  return 10;;
"Nov")  return 11;;
"Dec")  return 12;;
* )  return 0;;
esac
}
##
# today - return values in Ydd Ymm Yyy corresponding to today's date.
today () {
T1=`date '+%y %m %d'`
Yyy=`echo "$T1" | cut -f1 -d' '`
Ymm=`echo "$T1" | cut -f2 -d' '`
Ydd=`echo "$T1" | cut -f3 -d' '`
return 0;
}
##
# tomorrow ( dd mm yyyy ) - return values in Ydd Ymm Yyy representing
#       a date that stands in relation "tomorrow" to dd mm yyyy.
tomorrow () {
Ymm=$2
Yyy=$3
dim $2 $3
T1=$?
if  [ $1 -eq $T1 ]; then
Ydd=1
if  [ "$2" -eq 12 ]; then
Ymm = 1
Yyy=`expr $3 + 1`
else
Ymm=`expr $2 + 1`
fi
else
Ydd=`expr $1 + 1`
fi
return 0;
}
##
# yesterday ( dd mm yyyy ) - returns values in Ydd Ymm Yyy representing
#       a date that stands in relation "yesterday" to dd mm yyyy.
yesterday () {
Ymm=$2
Yyy=$3
if  [ "$1" -eq 1 ]; then
if  [ "$2" -eq 1 ]; then
Ydd=31
Ymm=12
Yyy=`expr $3 - 1`
return 0
else
Ymm=`expr $2 - 1`
if  [ $Ymm -eq 2 ]; then
ly $3
ADyes=$?
Ydd=`expr 28 + $ADyes`
return 0
else
Ydd=`echo $DIM | cut -f$Ymm -d' '`
return 0
fi
fi
else
Ydd=`expr $1 - 1`
fi
return 0;
}