-
Notifications
You must be signed in to change notification settings - Fork 0
/
validDate
executable file
·76 lines (68 loc) · 1.42 KB
/
validDate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
function exceededDaysInMonth
{
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 "$(basename $0): Unknown month $1" >&2
exit 1
esac
if [ $2 -lt 1 -o $2 -gt $days ]
then
return 1
else
return 0
fi
}
function isLeapYear
{
#return 0 if it is a leap year
# 1 otherwise
year=$1
if [ $((year%4)) -ne 0 ]; then
return 1
elif [ $((year%400)) -eq 0 ]; then
return 0
elif [ $((year%100)) -eq 0 ];then
return 1
else
return 0
fi
}
if [ $# -ne 3 ]
then
echo "Usage $(basename $0: month day year)" >&2
echo "Typical input formats are August 3 1962 and 8 3 1962" >&2
exit 1
fi
newdate=$(normdate "$@")
if [ $? -eq 1 ]
then
exit 1
fi
month="$(echo $newdate | cut -d" " -f1)"
day="$(echo $newdate | cut -d" " -f2)"
year="$(echo $newdate | cut -d" " -f3)"
if ! exceededDaysInMonth $month $day; then
if [ "$month" = "Feb" -a "$day" -eq 29 ]; then
if ! isLeapYear $year; then
echo "$(basename $0): Not a leap year, so Feb doesn't have 29 days" >&2
exit 1
fi
else
echo "$(basename $0): bad day value, $month does not have $day days" >&2
exit 1
fi
fi
echo "valid date: $newdate"
exit 0