#!/bin/sh
#print information(打印消息)
prn() {
echo $*
}
#print information(打印消息)
#check parameter is number(检查输入是否是数字)
#parameters (参数)
#number that will be checked(要检查的内容)
#[err info](错误信息,可选)
isnum(){
if [ $# -ge 1 ]; then
ok=`echo $1|sed -n '/^[0-9][0-9]*$/p'`
if [ -n $ok ]; then
return 0
else
if [ $# -eq 2 ]; then
prn $2
fi
return 1
fi
else
return 1
fi
}
#check parameter is number(检查输入是否是数字)
#get the month's days(得到指定年月的天数)
#parameters(参数)
#year(年)
#month(月)
leap() {
if [ $# -eq 2 ]; then
case $2 in
1|3|5|7|8|10|12)
Max=31
;;
4|6|9|11)
Max=30
;;
2)
#div by 4 and not by 100 or div by 400 is leap month(闰年-被4整除不能被100整除;被400整除)
if [ `rmd $1 4` -eq 0 -a `rmd $1 100` -ne 0 -o `rmd $1 400` -eq 0 ]; then
Max=29
else
Max=28
fi
;;
*) Max=0
esac
echo $Max
else
echo 0
fi
}
#get the month's days(得到指定年月的天数)
#add operation(加)
#parameters(参数)
#op1(参数1)
#op2(参数2)
add() {
echo `expr $1 + $2`
}
#subtration operation(减)
#parameters(参数)
#op1(参数1)
#op2(参数2)
sub() {
echo `expr $1 - $2`
}
#multiply operation(乘)
#parameters(参数)
#op1(参数1)
#op2(参数2)
mult() {
echo `expr $1 \* $2`
}
#div operation(除)
#parameters(参数)
#op1(参数1)
#op2(参数2)
div() {
echo `expr $1 / $2`
}
#remainder operation(余)
#parameters(参数)
#op1(参数1)
#op2(参数2)
rmd() {
echo `expr $1 % $2`
}
#get lens of string(串长)
#parameters(参数)
#str(串)
strlen() {
echo `expr $1 : '.*'`
}
#cal time offset(计算偏移时间)
#parameters(参数)
#offset(偏移量,单位为分钟,正数向后偏移,负数向前偏移)
#time(基准时间,如不给出取系统时间)
#note:the offset can not be greate than one month(注:目前只能处理1个月内的偏移)
dayoff() {
info="syntax: dayoff '30 200306230000',note 30'units is minute or dayoff 30"
if [ $# -ge 1 ]; then
#check offset format,offset must be number and it can begin with '+' or '-'
op=`echo $1|cut -b 1-1`
len=`expr $1 : '.*'`
if [ $op != "+" -a $op != "-" ]; then
op="+"
off=$1
else
off=`echo $1|cut -b 2-$len`
fi
isnum $off $info
if [ $? -ne 0 ]; then
return 1
fi
#check time format,if time param not supply get system time
if [ $# -eq 2 ]; then
time=$2
#get string length and check parameters
len=`strlen "$time"`
#check time lenth
if [ $len -gt 12 ]; then
prn $info
return 1
elif [ $len -lt 12 ]; then
prn $info
return 1
fi
#check time is num
isnum $2 $info
if [ $? -ne 0 ]; then
return 1
fi
#get each part of time
year=`echo $time | cut -b 1-4`
month=`echo $time | cut -b 5-6`
month=`sub $month 0`
daymax=`leap $year $month`
day=`echo $time | cut -b 7-8`
hour=`echo $time | cut -b 9-10`
minute=`echo $time | cut -b 11-12`
if [ $month -gt 12 -o $month -lt 1 -o $day -gt $daymax -o $day -lt 1 -o $hour -gt 23 \
-o $hour -lt 0 -o $minute -gt 59 -o $minute -lt 0 ]; then
prn "Invalid time range. \n" $info
return 1
fi
else
year=`date +%Y`
month=`date +%m`
month=`sub $month 0`
daymax=`leap $year $month`
day=`date +%d`
hour=`date +%H`
minute=`date +%M`
fi
#get days of the month
#deal each condition
#"+"condition
if [ $op = "+" ]; then
#get total of minutes
total=`add $off $minute`
#cal muniute part
minute=`rmd $total 60`
#get total of hours
total=`div $total 60`
total=`add $total $hour`
#cal hour part
hour=`rmd $total 24`
#get total of days
total=`div $total 24`
days=`add $total $day`
if [ $days -gt $daymax ]; then
day=`sub $days $daymax`
month=`add $month 1`
else
day=$days
fi
if [ $month -gt 12 ]; then
year=`add $year 1`
month=1
fi
#"-"condition
else
#get total hours of off
total=`div $off 60`
#get hour part of off in minute
tmp=`mult $total 60`
#get minute of off
tmp=`sub $off $tmp`
#cal minute part
tmp=`sub $minute $tmp`
if [ $tmp -lt 0 ];then
total=`add $total 1`
minute=`add $tmp 60`
else
minute=$tmp
fi
#get total days of off
tmp=$total
total=`div $tmp 24`
tmp1=`mult $total 24`
tmp=`sub $tmp $tmp1`
#cal hour part
tmp=`sub $hour $tmp`
if [ $tmp -lt 0 ]; then
total=`add $total 1`
hour=`add $tmp 24`
else
hour=$tmp
fi
#deal days
tmp=`sub $day $total`
if [ $tmp -le 0 ]; then
month=`sub $month 1`
if [ $month -eq 0 ]; then
year=`sub $year 1`
if [ $year -lt 0 ]; then
prn can not cal BC
return 1
fi
month=12
day=`add $tmp 31`
else
tmp1=`leap $year $month`
day=`add $tmp $tmp1`
fi
else
day=$tmp
fi
fi
printf "%04d%02d%02d%02d%02d\n" $year $month $day $hour $minute
return 0
else
prn $info
return 1
fi
}
#cal time offset(计算偏移时间)
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |