feature(unitproc) Add legacy unit process scripts
[BK-2020-03.git] / unitproc / bkdstcountdown
diff --git a/unitproc/bkdstcountdown b/unitproc/bkdstcountdown
new file mode 100755 (executable)
index 0000000..ee3b094
--- /dev/null
@@ -0,0 +1,59 @@
+#!/bin/bash
+
+# Date: 2020-02-13T23:14Z; baltakatei>
+
+# Description: This script outputs days until the next time zone
+# discontinuity using 'zdump' and 'date'.
+
+#=================ADJUST ME=======================
+SCRIPT_TZ="America/Los_Angeles" # Timezone to check for discontinuities
+#=================================================
+
+echoerr() {
+    echo "$@" 1>&2;
+}
+
+# Declare variables
+declare -a datesDiscontArray #array
+
+SCRIPT_YEAR=$(date +%Y)
+SCRIPT_YEAR_NEXT=$(( $(date +%Y) + 1))
+SCRIPT_RUN_DATE_SECONDS=$(date +%s)
+
+# Save zdump output for SCRIPT_YEAR (for how multiline data saved in variable, see https://stackoverflow.com/a/613580 )
+datesDiscont="$(zdump -c $SCRIPT_YEAR_NEXT -v $SCRIPT_TZ | grep $SCRIPT_YEAR |  awk '{ print $2 " " $3 " " $4 " " $5 " " $6 " " $7 }'; echo)"
+if [ -z "$datesDiscont" ]; then
+    echo "No time discontinuity this year."
+    exit 1
+fi
+
+###echoerr "datesDiscont:--""$datesDiscont""--"
+
+# Count lines in datesDiscont (for counting lines in a variable, see https://stackoverflow.com/a/6314682 )
+#datesDiscontLineCount=$(echo "$datesDiscont" | wc -l)
+
+# Convert datesDiscont into array (for how to process each line in a multiline variable, see https://superuser.com/a/284226 )
+while IFS= read -r line; do
+    #echo "$line"
+    #datesDiscontArray+="$line"
+    datesDiscontArray+=($(date --date="$line" +%s))
+    #echo ${datesDiscontArray[-1]}
+done <<< "$datesDiscont"
+#echoerr ${datesDiscontArray[@]}
+
+# Sort datesDiscontArray (see https://stackoverflow.com/a/11789688 )
+IFS=$'\n' datesDiscontArray=($(sort <<<"${datesDiscontArray[*]}"))
+unset IFS
+
+# Get earliest date in datesDiscontArray that isn't in the past.
+for discontinuityDate in "${datesDiscontArray[@]}"; do
+    ###echoerr "Discontinuity date (seconds):"$discontinuityDate
+    ###echoerr "SCRIPT_RUN_DATE_SECONDS     :"$SCRIPT_RUN_DATE_SECONDS
+    if [ $discontinuityDate -gt $SCRIPT_RUN_DATE_SECONDS ]; then
+       ###echoerr "DEBUG:Date in future."
+       nextDiscontDate="$discontinuityDate"
+       break
+    fi
+done
+
+echo $(( ( $nextDiscontDate - $SCRIPT_RUN_DATE_SECONDS )/86400 ))" days" # Days until next discontinuity date