写完后感觉这个代码好长好笨啊,请大家指正。
如果大家用的上,在这里就给大家共享了。
#! /bin/sh
leapy()
{
year=$1
RET="false"
fhundred=`expr $year % 400`
ohundred=`expr $year % 100`
four=`expr $year % 4`
if [ $fhundred -eq 0 ]
then
RET="true"
fi
if [ \( $four -eq 0 \) -a \( $ohundred -ne 0 \) ]
then
RET="true"
fi
echo $RET
#return $RET
}
dealy()
{
year1=$1
year2=$2
ycount=`expr $year1 - $year2`
#ycount=$1
count=1
nday=0
if [ $ycount -lt 0 ]
then
echo "The order of year's number is Error!"
exit 1
fi
while [ $count -lt $ycount ]
do
year=`expr $year2 + $count`
leapyear=`leapy $year`
if $leapyear
then
nday=`expr $nday + 1`
fi
count=`expr $count + 1`
done
days=`expr $ycount \* 365 + $nday`
echo $days
#return $days
}
dealm()
{
month=$1
year=$2
if [ \( $month -le 7 \) -a \( $month -ge 0 \) ]
then
days=`expr $month / 2 \* 30 + \( $month - $month / 2 \) \* 31`
elif [ \( $month -ge 8 \) -a \( $month -le 12 \) ]
then
days=`expr $month / 2 \* 31 + \( $month - $month / 2 \) \* 30`
else
echo "The mistaking of month"
exit 1
fi
leapyear=`leapy $year`
if $leapyear
then
days=`expr $days - 1`
else
days=`expr $days - 2`
fi
echo $days
#return $days
}
countdate()
{
if [ $# -ne 2 ]
then
echo "Usage: countdate <yyyymmdd> <yyyymmdd>"
exit 1
fi
date1=$1
date2=$2
lend1=`expr length $date1`
lend2=`expr length $date2`
if [ $lend1 -ne 8 -o $lend2 -ne 8 ]
then
echo "The date format of input is not right!"
echo "Usage: countdate <yyyymmdd> <yyyymmdd>"
exit 1
fi
year1=`expr substr $date1 1 4`
year2=`expr substr $date2 1 4`
month1=`expr substr $date1 5 2`
month1=`echo $month1|sed -e 's/^0//'`
month1=`expr $month1 - 1`
month2=`expr substr $date2 5 2`
month2=`echo $month2|sed -e 's/^0//'`
month2=`expr $month2 - 1`
day1=`expr substr $date1 7 2`
day1=`echo $day1|sed -e 's/^0//'`
day2=`expr substr $date2 7 2`
day2=`echo $day2|sed -e 's/^0//'`
yearsub=`expr $year1 - $year2`
if [ $yearsub -ge 0 ]
then
#ydays=`dealy $yearsub`
ydays=`dealy $year1 $year2`
else
echo "The date order is Error!"
exit 1
fi
mdays1=`dealm $month1 $year1`
mdays2=`dealm $month2 $year2`
days1=`expr $ydays + $mdays1 + $day1`
days2=`expr $mdays2 + $day2`
days=`expr $days1 - $days2`
echo $days
#return $days
}
result=`countdate $1 $2`
echo $result
6 个解决方案
#1
的確很複雜,用linux這樣算可能要簡單些
D1=`date --date='20070409' +"%s"`
D2=`date --date='20070304' +"%s" `
D3=$(($D1 - $D2))
echo $(($D3/60/60/24))
D1=`date --date='20070409' +"%s"`
D2=`date --date='20070304' +"%s" `
D3=$(($D1 - $D2))
echo $(($D3/60/60/24))
#2
这段程序需要在UNIX/Linux环境下运行,Linux下的date命令功能比UNIX多,在Linux你的方法很好,这么简洁,谢谢指教
#3
C好像有现成的吧,为啥还要要SHELL来实现呢?
#4
惭愧,我对C库函数不熟悉,能告诉我是哪一个哭的哪一个函数吗?不知道你说的是UNIX/Linux下的C还是 ANSI C?
#5
做个记号
昨天刚学到shell
昨天刚学到shell
#6
说可以用C实现的那位兄弟,能指教一下吗?
#1
的確很複雜,用linux這樣算可能要簡單些
D1=`date --date='20070409' +"%s"`
D2=`date --date='20070304' +"%s" `
D3=$(($D1 - $D2))
echo $(($D3/60/60/24))
D1=`date --date='20070409' +"%s"`
D2=`date --date='20070304' +"%s" `
D3=$(($D1 - $D2))
echo $(($D3/60/60/24))
#2
这段程序需要在UNIX/Linux环境下运行,Linux下的date命令功能比UNIX多,在Linux你的方法很好,这么简洁,谢谢指教
#3
C好像有现成的吧,为啥还要要SHELL来实现呢?
#4
惭愧,我对C库函数不熟悉,能告诉我是哪一个哭的哪一个函数吗?不知道你说的是UNIX/Linux下的C还是 ANSI C?
#5
做个记号
昨天刚学到shell
昨天刚学到shell
#6
说可以用C实现的那位兄弟,能指教一下吗?