feat(user/bkytpldl-generic):Limit channel name to 32 bytes
[BK-2020-03.git] / unitproc / bkt-timeUntilNextHour
1 #!/bin/bash
2 # Desc: Template to report seconds until beginning of next hour
3
4 #==BEGIN Define script parameters==
5 #===BEGIN Declare local script functions===
6 yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
7 die() { yell "$*"; exit 111; }
8 try() { "$@" || die "cannot $*"; }
9 timeUntilNextHour(){
10 # Desc: Report seconds until next hour
11 # Version 1.0.1
12 # Output: stdout: integer seconds until next hour
13 # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0
14 # Usage: timeUntilNextHour
15 # Usage: if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement"; exit 1; fi
16
17 local returnState timeCurrent timeNextHour secondsUntilNextHour
18 timeCurrent="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second.
19 timeNextHour="$(date -d "$timeCurrent next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second.
20 secondsUntilNextHour="$(( $(date +%s -d "$timeNextHour") - $(date +%s -d "$timeCurrent") ))"; # Calculate seconds until next hour (res. 1 second).
21 if [[ "$secondsUntilNextHour" -gt 0 ]]; then
22 returnState="true";
23 elif [[ "$secondsUntilNextHour" -eq 0 ]]; then
24 returnState="warning_zero";
25 yell "WARNING:Reported time until next hour exactly zero.";
26 elif [[ "$secondsUntilNextHour" -lt 0 ]]; then
27 returnState="warning_negative";
28 yell "WARNING:Reported time until next hour is negative.";
29 fi;
30
31 try echo "$secondsUntilNextHour"; # Report
32
33 # Determine function return code
34 if [[ "$returnState" = "true" ]]; then
35 return 0;
36 elif [[ "$returnState" = "warning_zero" ]]; then
37 return 1;
38 elif [[ "$returnState" = "warning_negative" ]]; then
39 return 2;
40 fi;
41 } # Report seconds until next hour
42 #===END Declare local script functions===
43 #==END Define script parameters==
44
45
46 #==BEGIN sample code==
47 try echo "Time until next hour (seconds):$(timeUntilNextHour)" # simple report
48
49 if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement for myTTL:$myTTL"; exit 1; fi # Use of exit code to exit early if time <= 0.
50 #==END sample code==
51
52 # Author: Steven Baltaktei Sandoval
53 # License: GPLv3+