]> git.plutz.net Git - shellwiki/blob - datetime.sh
Merge commit '6bc502434737d7f08379e79b94fc6fda424ef779'
[shellwiki] / datetime.sh
1 #!/bin/sh
2
3 [ "$include_datetime" ] && return 0
4 include_datetime="$0"
5
6 # Copyright 2023 - 2024 Paul Hänsch
7
8 # Permission to use, copy, modify, and/or distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
11
12 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
15 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
18 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 isdate(){
21   local date="$1" y m d
22
23   if   printf %s "$date" \
24     | grep -xEq '[0-9]{4}-((01|03|05|07|08|10|12)-(0[1-9]|[12][0-9]|3[01])|(04|06|09|11)-(0[1-9]|[12][0-9]|30)|02-(0[1-9]|[12][0-9]))'
25   then  # y-m-d (ISO Date)
26     y="${date%%-*}" d="${date##*-}" m="${date%-*}" m="${m#*-}"
27   elif printf %s "$date" \
28     | grep -xEq '((0?1|0?3|0?5|0?7|0?8|10|12)/(0?[1-9]|[12][0-9]|3[01])|(0?4|0?6|0?9|11)/(0?[1-9]|[12][0-9]|30)|0?2-(0[1-9]|[12][0-9]))/([0-9]{2}|[0-9]{4})'
29   then  # m/d/y (US Date)
30     y="${date##*/}" m="${date%%/*}" d="${date%/*}" d="${d#*/}"
31   elif printf %s "$date" \
32     | grep -xEq '((0?[1-9]|[12][0-9]|3[01])[\./](0?1|0?3|0?5|0?7|0?8|10|12)|(0?[1-9]|[12][0-9]|30)[\./](0?4|0?6|0?9|11)|(0[1-9]|[12][0-9])[\./]0?2)[\./]([0-9]{2}|[0-9]{4})'
33   then  # d/m/y or d.m.y (European Date / German Date)
34     y="${date##*.}" d="${date%%.*}" m="${date%.*}" m="${m#*.}"
35   else
36     return 1
37   fi
38   [ $y -lt 100 -a $y -gt 50 ] && y=$((y + 1900))
39   [ $y -lt 100 -a $y -le 50 ] && y=$((y + 2000))
40   date="$(printf "%04i-%02i-%02i" $y ${m#0} ${d#0})"
41
42   # leap year
43   if [ "$m" -eq 2 -a "$d" -eq 29 ]; then
44     if   [ "$((y % 400))" -eq 0 ]; then
45       :
46     elif [ "$((y % 100))" -eq 0 ]; then
47       return 1
48     elif [ "$((y % 4))" -eq 0 ]; then
49       :
50     else
51       return 1
52     fi
53   fi
54
55   printf '%04i-%02i-%02i\n' "$y" "${m#0}" "${d#0}"
56   return 0
57 }
58
59 istime(){
60   time="$1" h= m=
61
62   if   printf %s "$time" | grep -xEq '(0?[1-9]|1[012])(:[0-5][0-9])? ?(am|AM)\.?'; then
63     time="${time%?[aA][mM]}" h="${time%:*}" h="$(h % 12)"
64     [ "$h" != "$time" ] && m="${time#*:}" || m=0
65   elif printf %s "$time" | grep -xEq '(0?[1-9]|1[012])(:[0-5][0-9])? ?(pm|PM)\.?'; then
66     time="${time%?[aA][mM]}" h="${time%:*}" h="$(h % 12 + 12)"
67     [ "$h" != "$time" ] && m="${time#*:}" || m=0
68   elif printf %s "$time" | grep -xEq '(0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]'; then
69     time="${time%?[aA][mM]}" h="${time%:*}" m="${time#*:}"
70   else
71     return 1
72   fi
73
74   printf '%02i:%02i\n' "${h#0}" "${m#0}"
75   return 0
76 }
77
78 numdays(){
79   # return number of days in a month (i.e. 28, 29, 30, or 31)
80   local y="$1" m="${2#0}"
81
82   case $m in
83     1|3|5|7|10|12)
84       printf 31\\n
85       ;;
86     4|6|8|9|11)
87       printf 30\\n
88       ;;
89     2) if  [ "$((y % 400))" -eq 0 ]; then
90         printf 29\\n
91       elif [ "$((y % 100))" -eq 0 ]; then
92         printf 28\\n
93       elif [ "$((y % 4))" -eq 0 ]; then
94         printf 29\\n
95       else
96         printf 28\\n
97       fi
98       ;;
99     *) return 1;;
100   esac
101 }