3 # Date: 2020-02-13T23:14Z; baltakatei>
5 # Description: This script outputs days until the next time zone
6 # discontinuity using 'zdump' and 'date'.
8 #=================ADJUST ME=======================
9 SCRIPT_TZ
="America/Los_Angeles" # Timezone to check for discontinuities
10 #=================================================
17 declare -a datesDiscontArray
#array
19 SCRIPT_YEAR
=$
(date +%Y
)
20 SCRIPT_YEAR_NEXT
=$
(( $
(date +%Y
) + 1))
21 SCRIPT_RUN_DATE_SECONDS
=$
(date +%s
)
23 # Save zdump output for SCRIPT_YEAR (for how multiline data saved in variable, see https://stackoverflow.com/a/613580 )
24 datesDiscont
="$(zdump -c $SCRIPT_YEAR_NEXT -v $SCRIPT_TZ | grep $SCRIPT_YEAR | awk '{ print $2 " " $3 " " $4 " " $5 " " $6 " " $7 }'; echo)"
25 if [ -z "$datesDiscont" ]; then
26 echo "No time discontinuity this year."
30 ###echoerr "datesDiscont:--""$datesDiscont""--"
32 # Count lines in datesDiscont (for counting lines in a variable, see https://stackoverflow.com/a/6314682 )
33 #datesDiscontLineCount=$(echo "$datesDiscont" | wc -l)
35 # Convert datesDiscont into array (for how to process each line in a multiline variable, see https://superuser.com/a/284226 )
36 while IFS
= read -r line
; do
38 #datesDiscontArray+="$line"
39 datesDiscontArray
+=($
(date --date="$line" +%s
))
40 #echo ${datesDiscontArray[-1]}
41 done <<< "$datesDiscont"
42 #echoerr ${datesDiscontArray[@]}
44 # Sort datesDiscontArray (see https://stackoverflow.com/a/11789688 )
45 IFS
=$
'\n' datesDiscontArray
=($
(sort <<<"${datesDiscontArray[*]}"))
48 # Get earliest date in datesDiscontArray that isn't in the past.
49 for 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"
59 echo $
(( ( $nextDiscontDate - $SCRIPT_RUN_DATE_SECONDS )/86400 ))" days" # Days until next discontinuity date