-
Notifications
You must be signed in to change notification settings - Fork 0
/
_util-ldom.sh
executable file
·72 lines (64 loc) · 1.54 KB
/
_util-ldom.sh
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
#!/bin/sh
# ldom.sh - created with 'mkshcmd'
if [ "$1" = "--help" -o "$1" = "-h" -o "$1" = "" ]; then
echo "Last Day of Month"
echo " outputs the last numerical day of the month"
echo "usage: ldom.sh [now|_date_]"
echo "options:"
echo " now as of the current month"
echo " _date_ either '2022-02' or '2022-02-25'"
exit 0
fi
D="$1"
if [ "$D" = "now" ]; then
D="$(date -Iseconds | cut -d \T -f 1)"
fi
X="$(echo $D | cut -d \- -f 2)"
if [ "$X" = "" ]; then
echo "error: not a valid date '$D'"
exit
fi
if [ "$X" = "$D" ]; then
echo "error: not a valid date '$D'"
exit
fi
if [ $X -lt 1 ]; then
echo "error: not a valid month '$D'"
exit
fi
if [ $X -gt 12 ]; then
echo "error: not a valid month '$D'"
exit
fi
if [ $(echo $D | cut -d \- -f 1 | wc -c) -ne 5 ]; then
echo "error: not a full year '$D'"
exit
fi
isLeapYear () {
if [ $(($1 % 4)) -eq 0 -a ! $(($1 % 100)) -eq 0 ]; then
return 0
elif [ $(($1 % 400)) -eq 0 ]; then
return 0
fi
return 1
}
lastDayOfMonth () {
Y=$(echo $1 | cut -d \- -f 1)
M=$(echo $1 | cut -d \- -f 2)
case $M in
1|01) DoM=31 ;; # January
3|03) DoM=31 ;; # March
5|05) DoM=31 ;; # May
7|07) DoM=31 ;; # July
8|08) DoM=31 ;; # August
10) DoM=31 ;; # October
12) DoM=31 ;; # December
2|02) DoM=28 # February
isLeapYear "$Y" && DoM=29
;;
*) DoM=30 ;; # April, June, September, November
esac
echo "$DoM"
}
lastDayOfMonth "$D"
exit 0