日期:2014-05-16  浏览次数:20803 次

HP UNIX shell_计算任意时刻的上一个时间
没有函数可用,只能自己实现,有问题请回复我
#*****************************
#description 	get last hour
#input       	2009091812
#output 	2009091811
#*****************************
getLastHour(){
param=$1
#echo "param is:"$param
pYear=`echo $param | awk  '{ print substr($0,1,4) }'`
pMonth=`echo $param | awk  '{ print substr($0,5,2) }'`
pDay=`echo $param | awk  '{ print substr($0,7,2) }'`
pHour=`echo $param | awk  '{ print substr($0,9,2) }'`

#start
if [ $pHour = "00" ];
then
	pHour="23"
	if [ $pDay = "01" ];
	then
		if [ $pMonth = "01" ];
		then
			pMonth="12"
			pDay="31"
			((pYear=$pYear-1))
		else
			if [ $pMonth = "03" ];
			then
				((a4=$pYear%4))
				((a100=$pYear%100))
				((a400=$pYear%400))
				if [[ ($a4 = 0 && a100 != 0) || $a400 = 0 ]];
				then 
					pDay="29"
				else	
					pDay="28"
				fi
			elif [[ $pMonth = "05" || $pMonth = "07" || $pMonth = "10" || $pMonth = "12"  ]];
			then
				pDay="30"
			else
				pDay="31"
			fi
			((pMonth=$pMonth-1))	
		fi
	else
		((pDay=$pDay-1))		
	fi
else
	((pHour=$pHour-1))
fi

#fomat
len=`echo $pHour | awk '{ print length($0) }'`
	if [ $len = "1" ];
	then
	pHour="0"$pHour
	fi
	
len=`echo $pDay | awk '{ print length($0) }'`		
	if [ $len = "1" ];
	then
	pDay="0"$pDay
	fi
		
len=`echo $pMonth | awk '{ print length($0) }'`
	if [ $len = "1" ];
	then
	pMonth="0"$pMonth
	fi		

lastHour="$pYear""$pMonth""$pDay""$pHour"
return $lastHour
}