3 . $_EXEC/cgilite/cgilite.sh
4 . $_EXEC/cgilite/session.sh
6 _(){ printf %s\\n "$*"; }
7 [ "${LANGUAGE}" -a -r "${_EXEC}/l10n/${LANGUAGE}.sh" ] && . "${_EXEC}/l10n/${LANGUAGE}.sh"
9 start_date= start_time= end_date= end_time=
10 rec_freq= rec_int= error_msg= rec_end=
12 start= end= nstart= nend=
17 if printf %s "$date" \
18 | 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]))'
19 then # y-m-d (ISO Date)
20 y="${date%%-*}" d="${date##*-}" m="${date%-*}" m="${m#*-}"
21 elif printf %s "$date" \
22 | 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})'
23 then # m/d/y (US Date)
24 y="${date##*/}" m="${date%%/*}" d="${date%/*}" d="${d#*/}"
25 elif printf %s "$date" \
26 | 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})'
27 then # d/m/y or d.m.y (European Date / German Date)
28 y="${date##*.}" d="${date%%.*}" m="${date%.*}" m="${m#*.}"
32 [ $y -lt 100 -a $y -gt 50 ] && y=$((y + 1900))
33 [ $y -lt 100 -a $y -le 50 ] && y=$((y + 2000))
34 date="$(printf "%04i-%02i-%02i" $y ${m#0} ${d#0})"
37 if [ "$m" -eq 2 -a "$d" -eq 29 ]; then
38 if [ "$((y % 400))" -eq 0 ]; then
40 elif [ "$((y % 100))" -eq 0 ]; then
42 elif [ "$((y % 4))" -eq 0 ]; then
49 printf '%04i-%02i-%02i\n' "$y" "${m#0}" "${d#0}"
56 if printf %s "$time" | grep -xEq '(0?[1-9]|1[012])(:[0-5][0-9])? ?(am|AM)\.?'; then
57 time="${time%?[aA][mM]}" h="${time%:*}" h="$(h % 12)"
58 [ "$h" != "$time" ] && m="${time#*:}" || m=0
59 elif printf %s "$time" | grep -xEq '(0?[1-9]|1[012])(:[0-5][0-9])? ?(pm|PM)\.?'; then
60 time="${time%?[aA][mM]}" h="${time%:*}" h="$(h % 12 + 12)"
61 [ "$h" != "$time" ] && m="${time#*:}" || m=0
62 elif printf %s "$time" | grep -xEq '(0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]'; then
63 time="${time%?[aA][mM]}" h="${time%:*}" m="${time#*:}"
68 printf '%02i:%02i\n' "${h#0}" "${m#0}"
72 while [ $# -gt 0 ]; do case $1 in
73 --from|from|--start|start)
74 if isdate "$2" && istime "$3" ; then
75 start_date="$(isdate "$2" )" start_time="$(istime "$3")"
77 elif isdate "${2%% *}" && istime "${2#* }" ; then
78 start_date="$(isdate "${2%% *}" )" start_time="$(istime "${2#* }")"
80 elif isdate "$2" ; then
81 start_date="$(isdate "$2")" start_time="00:00"
84 error_msg="Event start should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\""
89 if isdate "$2" && istime "$3"; then
90 end_date="$(isdate "$2")" end_time="$(istime "$3")"
92 elif isdate "${2%% *}" && istime "${2#* }"; then
93 end_date="$(isdate "${2%% *}")" end_time="$(istime "${2#* }")"
95 elif isdate "$2"; then
96 end_date="$(isdate "$2")" end_time="23:59"
98 elif istime "$2"; then
99 end_time="$(istime "$2")"
102 error_msg="Event end should be \"YYYY-MM-DD\" or \"YYYY-MM-DD hh:mm\" or \"hh:mm\""
106 --repeat|--recur|--recurrence|--every|every)
107 if expr "$2" : '^[0-9]\+$' &&
108 expr "$3" : '^\(days\|nights\|weeks\|months\|years\|weekday\)$'; then
109 rec_freq="$2" rec_int="$3"
111 elif expr "$2" : '^[0-9]\+ \+\(days\|nights\|weeks\|months\|years\|weekday\)$'; then
112 rec_freq="${2%% *}" rec_int="${2##* }"
114 elif expr "$2" : '^\(day\|daily\|night\|nightly\|week\|weekly\|month\|monthly\|year\|yearly\|annually\)$'; then
115 rec_freq="1" rec_int="$2"
117 elif expr "$2" : '^last weekday$'; then
118 rec_freq="-1" rec_int="weekday"
120 elif expr "$2 $3" : '^last weekday$'; then
121 rec_freq="-1" rec_int="weekday"
123 elif expr "$2" : '^\(biweekly\|bimonthly\)$'; then
124 rec_freq="2" rec_int="$2"
127 error_msg="Recurrence should be \"N days|weeks|months|years\" or \"N|last weekday\""
133 rec_end="$(isdate "$2")"
136 error_msg="Recurrence end should be \"YYYY-MM-DD\""
147 if [ ! "$end_time" ]; then
151 if [ ! "$end_date" -a "$end_time" -a "$start_time" ]; then
152 if [ "$((${end_time%:*} * 60 + ${end_time#*:}))" -gt "$((${start_time%:*} * 60 + ${start_time#*:}))" ]; then
153 end_date="$start_date"
155 end_date="$(date -d "@$(($(date -d "$start_date" +%s) + 86400))" +%F)"
159 if [ ! "$start_date" ]; then
160 error_msg="Event needs start date, e.g. --start \"YYYY-MM-DD\""
163 if [ ! "$title" ]; then
164 error_msg="Event needs title, e.g. --title \"Event Name\""
167 if [ "$error_msg" ]; then
173 day|daily|days|night|nightly|nights)
175 week|weeks|weekly|biweekly)
177 month|monthly|months|bimonthly)
179 year|yearly|years|annually)
183 if [ "$rec_end" ]; then
184 rec_end="$(date -d "$rec_end" +%s)"
187 if [ -s './#events' ]; then
188 t1="$(stat -c %Y './#page.md')"
189 read t2 junk <"./#events"
190 if [ "$t1" -ge "$t2" ]; then
191 truncate -s 0 "./#events"
195 printf '%i %i %i %i %s %i\n' \
196 "$_DATE" "$(date -d "$start_date $start_time" +%s)" "$(date -d "$end_date $end_time" +%s)" \
197 "${rec_freq:-0}" "${rec_int:-\\}" "${rec_end:--1}" \
200 start="$(date -d "$start_date $start_time" +%s)"
201 end="$(date -d "$end_date $end_time" +%s)"
203 if [ $_DATE -le $end ]; then
204 nstart="$start" nend="$end"
207 [ $_DATE -gt $end ] && case $rec_int in
209 nend=$(( rec_freq * 86400 - (_DATE - end) % (rec_freq * 86400) + _DATE ))
210 nstart=$(( start - end + nend))
213 nend=$(( rec_freq * 604800 - (_DATE - end) % (rec_freq * 604800) + _DATE ))
214 nstart=$(( start - end + nend))
217 { read _y _m _d; read y m d; } <<-EOF
218 $(date -d @$_DATE +"%Y %_m %_d"
219 date -d @$start +"%Y %_m %_d"
222 _m=$((_y * 12 + _m)) m=$((y * 12 + m))
224 mod=$(( (_m - m) % rec_freq )); [ $mod -eq 0 ] && mod="$rec_freq";
225 m=$(( rec_freq - mod + _m ))
226 y=$((m / 12)) m=$((m % 12)); [ $m -eq 0 ] && y=$((y - 1)) m=12;
227 nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
228 isdate "$nstart" >/dev/null && [ $(date -d "$nstart" +%s) -ge $_DATE ] && break
230 m=$((y * 12 + m)) _m="$((_m + rec_freq))"
232 nstart="$(date -d "$nstart $start_time" +%s)"
233 nend="$((end - start + nstart))"
238 nstart="$start" nend="$end"
242 "$_EXEC/cgilite/html-sh.sed" <<-EOF
244 [label . $(HTML "$title")]
246 [dt $(_ Start:)][dd $(date -d @$nstart +"%F %T")]
247 [dt $(_ End:)][dd $(date -d @$nend +"%F %T")]
253 # tzid="$(cat /etc/timezone || printf 'UTC')"
255 # cat >>"#events.ics" <<-EOF
258 # PRODID:ShellWiki Event Macro
260 # UID:$uid@$(HEADER Host)
261 # DTSTAMP:TZID=${tzid}:$(date +%Y%m%dT%H%M%S)
262 # DTSTART:TZID=${tzid}:$(date +%Y%m%dT%H%M%S -d "$start_date $start_time")
264 # RRULE:FREQ=$ec_freq;INTERVAL=$rec_int;UNTIL=$(date +%Y%m%dT000000Z -d "$rec_end")