feat(.gitignore):Add ignore list
[BK-2020-03.git] / unitproc / bkdstcountdown
1 #!/bin/bash
2
3 # Description: This script outputs days until the next time zone
4 # discontinuity using 'zdump' and 'date'.
5
6 #=================ADJUST ME=======================
7 SCRIPT_TZ="America/Los_Angeles" # Timezone to check for discontinuities
8 #=================================================
9
10 yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
11 die() { yell "$*"; exit 111; }
12 try() { "$@" || die "cannot $*"; }
13
14 # Declare variables
15 declare -a datesDiscontArray #array
16
17 SCRIPT_YEAR=$(date +%Y)
18 SCRIPT_YEAR_NEXT=$(( $(date +%Y) + 1))
19 SCRIPT_YEAR_NEXT2=$(( $(date +%Y) + 2))
20 SCRIPT_RUN_DATE_SECONDS=$(date +%s)
21
22 # Save zdump output for SCRIPT_YEAR (for how multiline data saved in variable, see https://stackoverflow.com/a/613580 )
23 datesDiscont="$(zdump -c "$SCRIPT_YEAR","$SCRIPT_YEAR_NEXT2" -v $SCRIPT_TZ | grep "$SCRIPT_YEAR\|$SCRIPT_YEAR_NEXT" | awk '{ print $2 " " $3 " " $4 " " $5 " " $6 " " $7 }'; echo)"
24 if [ -z "$datesDiscont" ]; then
25 echo "No time discontinuity this year.";
26 exit 1;
27 fi;
28
29 ###yell "DEBUG:datesDiscont----------";
30 ###echo "$datesDiscont" 1>&2;
31 ###yell "DEBUG:-----------";
32
33 # Count lines in datesDiscont (for counting lines in a variable, see https://stackoverflow.com/a/6314682 )
34 #datesDiscontLineCount=$(echo "$datesDiscont" | wc -l)
35
36 # Convert datesDiscont into array (for how to process each line in a multiline variable, see https://superuser.com/a/284226 )
37 while IFS= read -r line; do
38 #echo "$line"
39 #datesDiscontArray+="$line"
40 datesDiscontArray+=($(date --date="$line" +%s))
41 #echo ${datesDiscontArray[-1]}
42 done <<< "$datesDiscont"
43 #yell ${datesDiscontArray[@]}
44
45 # Sort datesDiscontArray (see https://stackoverflow.com/a/11789688 )
46 IFS=$'\n' datesDiscontArray=($(sort <<<"${datesDiscontArray[*]}"))
47 unset IFS
48
49 ###yell "DEBUG:";
50 ###yell "DEBUG:datesDiscontArray:";
51 ###yell "${datesDiscontArray[@]}";
52 ###yell "DEBUG:";
53
54 # Get earliest date in datesDiscontArray that isn't in the past.
55 for discontinuityDate in "${datesDiscontArray[@]}"; do
56 ###yell "DEBUG:Discontinuity date (seconds):"$discontinuityDate
57 ###yell "DEBUG:SCRIPT_RUN_DATE_SECONDS :"$SCRIPT_RUN_DATE_SECONDS
58 if [ $discontinuityDate -gt $SCRIPT_RUN_DATE_SECONDS ]; then
59 ###yell "DEBUG:Date in future."
60 nextDiscontDate="$discontinuityDate"
61 break
62 fi
63 done
64
65 # Check that nextDiscontDate is not empty
66 if [ -z "$nextDiscontDate" ]; then
67 echo "ERROR:nextDiscontDate empty"
68 fi;
69
70
71 ###yell "DEBUG:nextDiscontDate:$nextDiscontDate";
72 ###yell "DEBUG:SCRIPT_RUN_DATE_SECONDS:$SCRIPT_RUN_DATE_SECONDS";
73
74 echo $(( ( $nextDiscontDate - $SCRIPT_RUN_DATE_SECONDS )/86400 ))" days" # Days until next discontinuity date