]>
zdv2.bktei.com Git - BK-2020-03.git/blob - unitproc/bkdstcountdown
   2 # Description: Outputs days until next timezone discontinuity 
   3 # Depends: GNU Coreutils 8.32 (date), zdump 2.35, Bash 5.1.16 
   4 #   BK-2020-03: yell(), die(), must() 
   8 script_tz
="America/Los_Angeles"; # Timezone to check for discontinuities 
  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 must
() { "$@" || die 
"cannot $*"; } 
  14     declare -a datesDiscontArray
; #array 
  17     script_year
="$(date +%Y)"; 
  18     script_year_next
="$(( script_year + 1 ))"; 
  19     script_year_next2
="$(( script_year + 2 ))"; 
  20     script_run_date_seconds
="$(date +%s)"; 
  22     # Get discontinuity dates from zdump (see https://stackoverflow.com/a/613580 ) 
  23     datesDiscont
="$(zdump -c "$script_year","$script_year_next2" -v $script_tz | \ 
  24       grep "$script_year\|
$script_year_next" | \ 
  25       awk '{ print $2 " " $3 " " $4 " " $5 " " $6 " " $7 }'; echo)"; # get dates 
  26     if [[ -z "$datesDiscont" ]]; then 
  27         yell 
"STATUS:No time discontinuity this year."; 
  31     # Convert datesDiscont into array 
  32     while read -r line
; do 
  33         tunix
="$(date --date="$line" +%s)"; # get unix epoch seconds 
  34         datesDiscontArray
+=("$tunix"); 
  35     done < <(printf "%s\n" "$datesDiscont"); 
  37     # Sort datesDiscontArray 
  38     while read -r line
; do 
  39         datesDiscontArray
+=("$line"); 
  40     done < <(printf "%s\n" "${datesDiscontArray[@]}" | 
sort); 
  42     # # Get earliest date in datesDiscontArray that isn't in the past. 
  43     for discontinuityDate 
in "${datesDiscontArray[@]}"; do 
  44         if [[ $discontinuityDate -gt $script_run_date_seconds ]]; then 
  45             nextDiscontDate
="$discontinuityDate"; 
  50     # Check that nextDiscontDate is not empty 
  51     if [[ -z "$nextDiscontDate" ]]; then 
  52         die 
"FATAL:nextDiscontDate empty"; 
  55     # Calculate days until next discontinuity date 
  56     output
="$(( ( nextDiscontDate - script_run_date_seconds )/86400 )) days"; 
  57     printf "%s\n" "$output"; 
  63 # Author: Steven Baltakatei Sandoval