]>
zdv2.bktei.com Git - BK-2020-03.git/blob - unitproc/bkdstcountdown
   3 # Description: This script outputs days until the next time zone 
   4 # discontinuity using 'zdump' and 'date'. 
   6 #=================ADJUST ME======================= 
   7 SCRIPT_TZ
="America/Los_Angeles" # Timezone to check for discontinuities 
   8 #================================================= 
  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 $*"; } 
  15 declare -a datesDiscontArray 
#array 
  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
) 
  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."; 
  29 ###yell "DEBUG:datesDiscont----------"; 
  30 ###echo "$datesDiscont" 1>&2; 
  31 ###yell "DEBUG:-----------"; 
  33 # Count lines in datesDiscont (for counting lines in a variable, see https://stackoverflow.com/a/6314682 ) 
  34 #datesDiscontLineCount=$(echo "$datesDiscont" | wc -l) 
  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 
  39     #datesDiscontArray+="$line" 
  40     datesDiscontArray
+=($
(date --date="$line" +%s
)) 
  41     #echo ${datesDiscontArray[-1]} 
  42 done <<< "$datesDiscont" 
  43 #yell ${datesDiscontArray[@]} 
  45 # Sort datesDiscontArray (see https://stackoverflow.com/a/11789688 ) 
  46 IFS
=$
'\n' datesDiscontArray
=($
(sort <<<"${datesDiscontArray[*]}")) 
  50 ###yell "DEBUG:datesDiscontArray:"; 
  51 ###yell "${datesDiscontArray[@]}"; 
  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" 
  65 # Check that nextDiscontDate is not empty 
  66 if [ -z "$nextDiscontDate" ]; then 
  67     echo "ERROR:nextDiscontDate empty" 
  71 ###yell "DEBUG:nextDiscontDate:$nextDiscontDate"; 
  72 ###yell "DEBUG:SCRIPT_RUN_DATE_SECONDS:$SCRIPT_RUN_DATE_SECONDS"; 
  74 echo $
(( ( $nextDiscontDate - $SCRIPT_RUN_DATE_SECONDS )/86400 ))" days" # Days until next discontinuity date