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