######################################
#Purpose : 日期计算函数 #
#Date : 2007-11-13 15:43 #
######################################
# !/bin/ksh
# 获取月份最大日期
get_mon_days()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
r1=`expr $Y \% 4`
r2=`expr $Y \% 100`
r3=`expr $Y \% 400`
case $M in
01|03|05|07|08|10|12) days=31;;
04|06|09|11) days=30;;
02)
r1=`expr $Y \% 4`
r2=`expr $Y \% 100`
r3=`expr $Y \% 400`
# 闰年
if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0 ] ; then
days=29
else
days=28
fi
;;
*)
days=0
;;
esac
echo $days
}
check_date()
{
# 0 合法 1 非法
FLAG=0
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
D=`expr substr $1 7 2`
days=`get_mon_days $Y$M`
if ! [ $D -ge 1 -a $D -le $days ] ; then
FLAG=1
fi
echo $FLAG
}
#返回昨天日期
get_before_date()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
D=`expr substr $1 7 2`
YY=`expr $Y - 1`
MM=`expr $M - 1`
DD=`expr $D - 1`
MM=`printf "%02d" $MM`
DD=`printf "%02d" $DD`
dd=$Y$MM
dad=`get_mon_days $dd`
be_date=$Y$M$DD
if [ $D -eq 01 ]
then
if [ $M -ne 01 ]
then
be_date=$Y$MM$dad
fi
if [ $M -eq 01 ]
then
be_date=$YY"1231"
fi
fi
echo $be_date
}
#返回明天日期
get_next_date()
{
Y=`expr substr $1 1 4`
M=`expr substr $1 5 2`
D=`expr substr $1 7 2`
YY=`expr $Y + 1`
MM=`expr $M + 1`
DD=`expr $D + 1`
MM=`printf "%02d" $MM`
DD=`printf "%02d" $DD`
r1=`expr $Y \% 4`
r2=`expr $Y \% 100`
r3=`expr $Y \% 400`
# 通常情况
next_date=$Y$M$DD
# 特殊情况1,小月月末
if [ $D -eq 30 ]
then
case $M in
04|06|09|11) next_date=$Y$MM"01";;
esac
fi
# 特殊情况2,大月月末
if [ $D -eq 31 ]
then
next_date=$Y$MM"01"
case $M in
12) next_date=$YY"0101";;
esac
fi
# 特殊情况3,2月月末
if [ $M -eq 02 ]
then
# 闰年
if [ r1 -eq 0 -a r2 -ne 0 -o r3 -eq 0 ]
then
if [ $D -eq 29 ]
then
next_date=$Y$MM"01"
fi
else
if [ $D -eq 28 ]
then
next_date=$Y$MM"01"
fi
fi
fi
echo $next_date
}
# 开始调用
get_before_date $1
get_next_date $1
check_date $1