feat(unitprod):Created timeDuration function template
[BK-2020-03.git] / unitproc / bkdstcountdown
... / ...
CommitLineData
1#!/bin/bash
2
3# Date: 2020-02-13T23:14Z; baltakatei>
4
5# Description: This script outputs days until the next time zone
6# discontinuity using 'zdump' and 'date'.
7
8#=================ADJUST ME=======================
9SCRIPT_TZ="America/Los_Angeles" # Timezone to check for discontinuities
10#=================================================
11
12echoerr() {
13 echo "$@" 1>&2;
14}
15
16# Declare variables
17declare -a datesDiscontArray #array
18
19SCRIPT_YEAR=$(date +%Y)
20SCRIPT_YEAR_NEXT=$(( $(date +%Y) + 1))
21SCRIPT_RUN_DATE_SECONDS=$(date +%s)
22
23# Save zdump output for SCRIPT_YEAR (for how multiline data saved in variable, see https://stackoverflow.com/a/613580 )
24datesDiscont="$(zdump -c $SCRIPT_YEAR_NEXT -v $SCRIPT_TZ | grep $SCRIPT_YEAR | awk '{ print $2 " " $3 " " $4 " " $5 " " $6 " " $7 }'; echo)"
25if [ -z "$datesDiscont" ]; then
26 echo "No time discontinuity this year."
27 exit 1
28fi
29
30###echoerr "datesDiscont:--""$datesDiscont""--"
31
32# Count lines in datesDiscont (for counting lines in a variable, see https://stackoverflow.com/a/6314682 )
33#datesDiscontLineCount=$(echo "$datesDiscont" | wc -l)
34
35# Convert datesDiscont into array (for how to process each line in a multiline variable, see https://superuser.com/a/284226 )
36while IFS= read -r line; do
37 #echo "$line"
38 #datesDiscontArray+="$line"
39 datesDiscontArray+=($(date --date="$line" +%s))
40 #echo ${datesDiscontArray[-1]}
41done <<< "$datesDiscont"
42#echoerr ${datesDiscontArray[@]}
43
44# Sort datesDiscontArray (see https://stackoverflow.com/a/11789688 )
45IFS=$'\n' datesDiscontArray=($(sort <<<"${datesDiscontArray[*]}"))
46unset IFS
47
48# Get earliest date in datesDiscontArray that isn't in the past.
49for discontinuityDate in "${datesDiscontArray[@]}"; do
50 ###echoerr "Discontinuity date (seconds):"$discontinuityDate
51 ###echoerr "SCRIPT_RUN_DATE_SECONDS :"$SCRIPT_RUN_DATE_SECONDS
52 if [ $discontinuityDate -gt $SCRIPT_RUN_DATE_SECONDS ]; then
53 ###echoerr "DEBUG:Date in future."
54 nextDiscontDate="$discontinuityDate"
55 break
56 fi
57done
58
59echo $(( ( $nextDiscontDate - $SCRIPT_RUN_DATE_SECONDS )/86400 ))" days" # Days until next discontinuity date