feat(unitproc):validateInput:Add integer type
[BK-2020-03.git] / unitproc / bktemp-timeUntilNextHour
CommitLineData
f7301210
SBS
1#!/bin/bash
2# Desc: Template to report seconds until beginning of next hour
f7301210
SBS
3
4#==BEGIN Define script parameters==
5#===BEGIN Declare local script functions===
6yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
7die() { yell "$*"; exit 111; }
8try() { "$@" || die "cannot $*"; }
9timeUntilNextHour(){
10 # Desc: Report seconds until next hour
f54d9fe8 11 # Version 1.0.0
f7301210
SBS
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
f54d9fe8 16
b03c9c0c
SBS
17 local returnState TIME_CURRENT TIME_NEXT_HOUR SECONDS_UNTIL_NEXT_HOUR
18 TIME_CURRENT="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second.
19 TIME_NEXT_HOUR="$(date -d "$TIME_CURRENT next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second.
8f0a57f9 20 SECONDS_UNTIL_NEXT_HOUR="$(( $(date +%s -d "$TIME_NEXT_HOUR") - $(date +%s -d "$TIME_CURRENT") ))"; # Calculate seconds until next hour (res. 1 second).
f7301210
SBS
21 if [[ "$SECONDS_UNTIL_NEXT_HOUR" -gt 0 ]]; then
22 returnState="true";
23 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -eq 0 ]]; then
f54d9fe8 24 returnState="warning_zero";
f7301210
SBS
25 yell "WARNING:Reported time until next hour exactly zero.";
26 elif [[ "$SECONDS_UNTIL_NEXT_HOUR" -lt 0 ]]; then
f54d9fe8 27 returnState="warning_negative";
f7301210
SBS
28 yell "WARNING:Reported time until next hour is negative.";
29 fi
30
31 try echo "$SECONDS_UNTIL_NEXT_HOUR"; # Report
32
f54d9fe8 33 # Determine function return code
f7301210
SBS
34 if [[ "$returnState" = "true" ]]; then
35 return 0;
f54d9fe8 36 elif [[ "$returnState" = "warning_zero" ]]; then
f7301210 37 return 1;
f54d9fe8 38 elif [[ "$returnState" = "warning_negative" ]]; then
f7301210
SBS
39 return 2;
40 fi
8f0a57f9 41} # Report seconds until next hour
f7301210
SBS
42#===END Declare local script functions===
43#==END Define script parameters==
44
45
46#==BEGIN sample code==
47try echo "Time until next hour (seconds):$(timeUntilNextHour)" # simple report
48
49if ! 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==
f54d9fe8
SBS
51
52# Author: Steven Baltaktei Sandoval
53# License: GPLv3+