Commit | Line | Data |
---|---|---|
58c4ea21 SBS |
1 | #!/usr/bin/env bash |
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() | |
5 | # Version: 0.1.0 | |
fdf917e1 | 6 | |
58c4ea21 SBS |
7 | # User config |
8 | script_tz="America/Los_Angeles"; # Timezone to check for discontinuities | |
fdf917e1 | 9 | |
2b426998 SBS |
10 | yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370 |
11 | die() { yell "$*"; exit 111; } | |
58c4ea21 SBS |
12 | must() { "$@" || die "cannot $*"; } |
13 | main() { | |
14 | declare -a datesDiscontArray; #array | |
fdf917e1 | 15 | |
58c4ea21 SBS |
16 | # plumbing |
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)"; | |
fdf917e1 | 21 | |
58c4ea21 SBS |
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."; | |
28 | exit 1; | |
29 | fi; | |
fdf917e1 | 30 | |
58c4ea21 SBS |
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"); | |
fdf917e1 | 36 | |
58c4ea21 SBS |
37 | # Sort datesDiscontArray |
38 | while read -r line; do | |
39 | datesDiscontArray+=("$line"); | |
40 | done < <(printf "%s\n" "${datesDiscontArray[@]}" | sort); | |
fdf917e1 | 41 | |
58c4ea21 SBS |
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"; | |
46 | break; | |
47 | fi; | |
48 | done; | |
fdf917e1 | 49 | |
58c4ea21 SBS |
50 | # Check that nextDiscontDate is not empty |
51 | if [[ -z "$nextDiscontDate" ]]; then | |
52 | die "FATAL:nextDiscontDate empty"; | |
53 | fi; | |
2b426998 | 54 | |
58c4ea21 SBS |
55 | # Calculate days until next discontinuity date |
56 | output="$(( ( nextDiscontDate - script_run_date_seconds )/86400 )) days"; | |
57 | printf "%s\n" "$output"; | |
fdf917e1 | 58 | |
58c4ea21 | 59 | }; # main program |
2b426998 | 60 | |
58c4ea21 | 61 | main "$@"; |
2b426998 | 62 | |
58c4ea21 SBS |
63 | # Author: Steven Baltakatei Sandoval |
64 | # License: GPLv3+ |