分类:
2008-03-21 16:52:45
确认工作对我们是一项很大的挑战,而在脚本中重要的是确认时间,它必需保证是有效的。如 果我们在这方面忽视了闰年,工作似乎也不会有太大的影响,因为我们的日历规则每年始终如一。在这个例子中,我们比较需要的是:每个月特定日期的表格。另 外,对于闰年而言,在脚本中我们还必须增加闰年的计算规则,如下:
* 年份不能被4整除,不是闰年
* 年份能被4及400整除,是闰年
* 年份能被4及100整除,但不能被400整除,不是闰年
* 上面沒提及的年份,能够被4整除的就是闰年
脚本源代码
#!/bin/sh
# valid-date -- Validates a date, taking into account leap year rules.
exceedsDaysInMonth()
{
# Given a month name, return 0 if the specified day value is
# less than or equal to the max days in the month; 1 otherwise
case $(echo $1|tr '[:upper:]' '[:lower:]') in
jan* ) days=31 ;; feb* ) days=28 ;;
mar* ) days=31 ;; apr* ) days=30 ;;
may* ) days=31 ;; jun* ) days=30 ;;
jul* ) days=31 ;; aug* ) days=31 ;;
sep* ) days=30 ;; oct* ) days=31 ;;
nov* ) days=30 ;; dec* ) days=31 ;;
* ) echo "$0: Unknown month name $1" >&2; exit 1
esac
if [ $2 -lt 1 -o $2 -gt $days ] ; then
return 1
else
return 0 # the day number is valid
fi
}
isLeapYear()
{
# This function returns 0 if a leap year; 1 otherwise.
# The formula for checking whether a year is a leap year is:
# 1. Years not divisible by 4 are not leap years.
# 2. Years divisible by 4 and by 400 are leap years.
# 3. Years divisible by 4, not divisible by 400, and divisible by 100,
# are not leap years.
# 4. All other years divisible by 4 are leap years.
year=$1
if [ "$((year % 4))" -ne 0 ] ; then
return 1 # nope, not a leap year
elif [ "$((year % 400))" -eq 0 ] ; then
return 0 # yes, it's a leap year
elif [ "$((year % 100))" -eq 0 ] ; then
return 1
else
return 0
fi
}
## Begin main script
if [ $# -ne 3 ] ; then
echo "Usage: $0 month day year" >&2
echo "Typical input formats are August 3 1962 and 8 3 2002" >&2
exit 1
fi
# Normalize date and split back out returned values
newdate="$(normdate "$@")"
if [ $? -eq 1 ] ; then
exit 1 # error condition already reported by normdate
fi
month="$(echo $newdate | cut -d\ -f1)"
day="$(echo $newdate | cut -d\ -f2)"
year="$(echo $newdate | cut -d\ -f3)"
# Now that we have a normalized date, let's check to see if the
# day value is logical
if ! exceedsDaysInMonth $month "$2" ; then
if [ "$month" = "Feb" -a "$2" -eq "29" ] ; then
if ! isLeapYear $3 ; then
echo "$0: $3 is not a leap year, so Feb doesn't have 29 days" >&2
exit 1
fi
else
echo "$0: bad day value: $month doesn't have $2 days" >&2
exit 1
fi
fi
echo "Valid date: $newdate"
exit 0
运行脚本
请在命令列上以公元"月日年"的方式输入日期。月份的部分,可以打数字、英文全名或三个字母的英文缩写;年的部分必须要输入4位数的格式。
结果
$ valid-date august 3 1960
Valid date: Aug 3 1960
$ valid-date 9 31 2001
valid-date: bad day value: Sep doesn't have 31 days
$ valid-date feb 29 2004
Valid date: Feb 29 2004
$ valid-date feb 29 2006
valid-date: 2006 is not a leap year, so Feb doesn't have 29 days
改进与加强
一个与程序有异曲同工之妙的是判別时间格式,时间的表示可能是24小时制或是上/下午制(am/pm)。首先先将时间的时分秒区隔出来,确认分与秒是否在0~60之间,接着检查第一个值是否在0~12间(上/下午制)或0~24间(24小时制)。
(很幸运的,在日常生活上,我们不必去理会闰秒以及微时间的变化。)