feat(unitproc):Add bkuptime
[BK-2020-03.git] / unitproc / bkdstcountdown
... / ...
CommitLineData
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=======================
7SCRIPT_TZ="America/Los_Angeles" # Timezone to check for discontinuities
8#=================================================
9
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 $*"; }
13
14# Declare variables
15declare -a datesDiscontArray #array
16
17SCRIPT_YEAR=$(date +%Y)
18SCRIPT_YEAR_NEXT=$(( $(date +%Y) + 1))
19SCRIPT_YEAR_NEXT2=$(( $(date +%Y) + 2))
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 )
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)"
24if [ -z "$datesDiscont" ]; then
25 echo "No time discontinuity this year.";
26 exit 1;
27fi;
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 )
37while IFS= read -r line; do
38 #echo "$line"
39 #datesDiscontArray+="$line"
40 datesDiscontArray+=($(date --date="$line" +%s))
41 #echo ${datesDiscontArray[-1]}
42done <<< "$datesDiscont"
43#yell ${datesDiscontArray[@]}
44
45# Sort datesDiscontArray (see https://stackoverflow.com/a/11789688 )
46IFS=$'\n' datesDiscontArray=($(sort <<<"${datesDiscontArray[*]}"))
47unset 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.
55for 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
63done
64
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
74echo $(( ( $nextDiscontDate - $SCRIPT_RUN_DATE_SECONDS )/86400 ))" days" # Days until next discontinuity date