feat(user/mw_create_subpage_navlinks.sh):Add script
[BK-2020-03.git] / unitproc / bkdstcountdown
... / ...
CommitLineData
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
6
7# User config
8script_tz="America/Los_Angeles"; # Timezone to check for discontinuities
9
10yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
11die() { yell "$*"; exit 111; }
12must() { "$@" || die "cannot $*"; }
13main() {
14 declare -a datesDiscontArray; #array
15
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)";
21
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;
30
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");
36
37 # Sort datesDiscontArray
38 while read -r line; do
39 datesDiscontArray+=("$line");
40 done < <(printf "%s\n" "${datesDiscontArray[@]}" | sort);
41
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;
49
50 # Check that nextDiscontDate is not empty
51 if [[ -z "$nextDiscontDate" ]]; then
52 die "FATAL:nextDiscontDate empty";
53 fi;
54
55 # Calculate days until next discontinuity date
56 output="$(( ( nextDiscontDate - script_run_date_seconds )/86400 )) days";
57 printf "%s\n" "$output";
58
59}; # main program
60
61main "$@";
62
63# Author: Steven Baltakatei Sandoval
64# License: GPLv3+