| 1 | #!/bin/bash |
| 2 | |
| 3 | # Date: 2020-02-13T23:14Z; baltakatei> |
| 4 | |
| 5 | # Description: This script outputs days until the next time zone |
| 6 | # discontinuity using 'zdump' and 'date'. |
| 7 | |
| 8 | #=================ADJUST ME======================= |
| 9 | SCRIPT_TZ="America/Los_Angeles" # Timezone to check for discontinuities |
| 10 | #================================================= |
| 11 | |
| 12 | echoerr() { |
| 13 | echo "$@" 1>&2; |
| 14 | } |
| 15 | |
| 16 | # Declare variables |
| 17 | declare -a datesDiscontArray #array |
| 18 | |
| 19 | SCRIPT_YEAR=$(date +%Y) |
| 20 | SCRIPT_YEAR_NEXT=$(( $(date +%Y) + 1)) |
| 21 | SCRIPT_RUN_DATE_SECONDS=$(date +%s) |
| 22 | |
| 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." |
| 27 | exit 1 |
| 28 | fi |
| 29 | |
| 30 | ###echoerr "datesDiscont:--""$datesDiscont""--" |
| 31 | |
| 32 | # Count lines in datesDiscont (for counting lines in a variable, see https://stackoverflow.com/a/6314682 ) |
| 33 | #datesDiscontLineCount=$(echo "$datesDiscont" | wc -l) |
| 34 | |
| 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 |
| 37 | #echo "$line" |
| 38 | #datesDiscontArray+="$line" |
| 39 | datesDiscontArray+=($(date --date="$line" +%s)) |
| 40 | #echo ${datesDiscontArray[-1]} |
| 41 | done <<< "$datesDiscont" |
| 42 | #echoerr ${datesDiscontArray[@]} |
| 43 | |
| 44 | # Sort datesDiscontArray (see https://stackoverflow.com/a/11789688 ) |
| 45 | IFS=$'\n' datesDiscontArray=($(sort <<<"${datesDiscontArray[*]}")) |
| 46 | unset IFS |
| 47 | |
| 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" |
| 55 | break |
| 56 | fi |
| 57 | done |
| 58 | |
| 59 | echo $(( ( $nextDiscontDate - $SCRIPT_RUN_DATE_SECONDS )/86400 ))" days" # Days until next discontinuity date |