chore(unitproc):timeDuration: remove debug code
[BK-2020-03.git] / unitproc / bktemp-timeDuration
CommitLineData
7ad273d8
SBS
1#!/bin/bash
2
3# Desc: Template to indicate time duration in ISO-8601 format
4
5yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
6die() { yell "$*"; exit 111; }
7try() { "$@" || die "cannot $*"; }
8timeDuration(){
9 # Desc: Output approximate time duration string before given time (default:current date)
10 # Ref/Attrib: ISO-8601:2004(E), §4.4.4.2 Representations of time intervals by duration and context information
11 # Note: "1 month" ("P1M") is assumed to be "30 days" (see ISO-8601:2004(E), §2.2.1.2)
12 # Usage: timeDuration [arg1] ([arg2])
13 # Input: arg1: seconds as base 10 integer >= 0 (ex: 3601)
14 # arg2: precision level (optional; default=2)
15 # Output: stdout: ISO-8601 duration string (ex: "P1H1S", "P2Y10M15DT10H30M20S")
16 # Example: 'timeDuration 111111 3' yields 'P1DT6H51M'
17 # Depends: date 8 (gnucoreutils)
18 local returnState fullHours fullMinutes fullSeconds;
19 ARG1="$1";
20 ARG2="$2";
21 precision=2; # set default precision
22 returnState="true"; # set default return state
23
24 # Check that between one and two arguments is supplied
25 if ! { [[ $# -ge 1 ]] && [[ $# -le 2 ]]; }; then
26 yell "ERROR:Invalid number of arguments:$# . Exiting.";
27 returnState="ERROR_INPUT"; fi
28
29 # Check that arg1 provided
30 if [[ $# -ge 1 ]]; then
31 # Check that arg1 is a positive integer
32 if [[ "$ARG1" =~ ^[[:digit:]]+$ ]]; then
33 arg1Valid="true";
34 else
35 yell "ERROR:ARG1 not a digit.";
36 returnState="ERROR_INPUT";
37 arg1Valid="false";
38 fi
39 else
40 yell "ERROR:No argument provided. Exiting.";
41 exit 1;
42 fi
43
44 # Consider whether arg2 was provided
45 if [[ $# -eq 2 ]]; then
46 # Check that the second arg is a positive integer
47 if [[ "$ARG2" =~ ^[[:digit:]]+$ ]] && [[ "ARG2" -gt 0 ]]; then
48 arg2Valid="true";
49 precision="$ARG2";
50 else
51 yell "ERROR:ARG2 not a positive integer. (is $ARG2 ). Leaving early.";
52 returnState="ERROR_INPUT";
53 arg2Valid="false";
54 fi;
55 else
56 arg2Valid="false";
57 fi;
58
59 remainder="$ARG1" ; # seconds
60 ## Calculate full years Y, update remainder
61 fullYears=$(( remainder / (365*24*60*60) ));
62 remainder=$(( remainder - (fullYears*365*24*60*60) ));
7ad273d8
SBS
63 ## Calculate full months M, update remainder
64 fullMonths=$(( remainder / (30*24*60*60) ));
65 remainder=$(( remainder - (fullMonths*30*24*60*60) ));
7ad273d8
SBS
66 ## Calculate full days D, update remainder
67 fullDays=$(( remainder / (24*60*60) ));
68 remainder=$(( remainder - (fullDays*24*60*60) ));
7ad273d8
SBS
69 ## Calculate full hours H, update remainder
70 fullHours=$(( remainder / (60*60) ));
71 remainder=$(( remainder - (fullHours*60*60) ));
7ad273d8
SBS
72 ## Calculate full minutes M, update remainder
73 fullMinutes=$(( remainder / (60) ));
74 remainder=$(( remainder - (fullMinutes*60) ));
7ad273d8
SBS
75 ## Calculate full seconds S, update remainder
76 fullSeconds=$(( remainder / (1) ));
77 remainder=$(( remainder - (remainder*1) ));
7ad273d8
SBS
78 ## Check which fields filled
79 if [[ $fullYears -gt 0 ]]; then hasYears="true"; else hasYears="false"; fi
80 if [[ $fullMonths -gt 0 ]]; then hasMonths="true"; else hasMonths="false"; fi
81 if [[ $fullDays -gt 0 ]]; then hasDays="true"; else hasDays="false"; fi
82 if [[ $fullHours -gt 0 ]]; then hasHours="true"; else hasHours="false"; fi
83 if [[ $fullMinutes -gt 0 ]]; then hasMinutes="true"; else hasMinutes="false"; fi
84 if [[ $fullSeconds -gt 0 ]]; then hasSeconds="true"; else hasSeconds="false"; fi
85
86 ## Determine which fields to display (see ISO-8601:2004 §4.4.3.2)
87 witherPrecision="false"
88
89 ### Years
90 if $hasYears && [[ $precision -gt 0 ]]; then
91 displayYears="true";
92 witherPrecision="true";
93 else
94 displayYears="false";
95 fi;
96 if $witherPrecision; then ((precision--)); fi;
97
98 ### Months
99 if $hasMonths && [[ $precision -gt 0 ]]; then
100 displayMonths="true";
101 witherPrecision="true";
102 else
103 displayMonths="false";
104 fi;
105 if $witherPrecision && [[ $precision -gt 0 ]]; then
106 displayMonths="true";
107 fi;
108 if $witherPrecision; then ((precision--)); fi;
109
110 ### Days
111 if $hasDays && [[ $precision -gt 0 ]]; then
112 displayDays="true";
113 witherPrecision="true";
114 else
115 displayDays="false";
116 fi;
117 if $witherPrecision && [[ $precision -gt 0 ]]; then
118 displayDays="true";
119 fi;
120 if $witherPrecision; then ((precision--)); fi;
121
122 ### Hours
123 if $hasHours && [[ $precision -gt 0 ]]; then
124 displayHours="true";
125 witherPrecision="true";
126 else
127 displayHours="false";
128 fi;
129 if $witherPrecision && [[ $precision -gt 0 ]]; then
130 displayHours="true";
131 fi;
132 if $witherPrecision; then ((precision--)); fi;
133
134 ### Minutes
135 if $hasMinutes && [[ $precision -gt 0 ]]; then
136 displayMinutes="true";
137 witherPrecision="true";
138 else
139 displayMinutes="false";
140 fi;
141 if $witherPrecision && [[ $precision -gt 0 ]]; then
142 displayMinutes="true";
143 fi;
144 if $witherPrecision; then ((precision--)); fi;
145
146 ### Seconds
147
148 if $hasSeconds && [[ $precision -gt 0 ]]; then
149 displaySeconds="true";
150 witherPrecision="true";
151 else
152 displaySeconds="false";
153 fi;
154 if $witherPrecision && [[ $precision -gt 0 ]]; then
155 displaySeconds="true";
156 fi;
157 if $witherPrecision; then ((precision--)); fi;
158
159
160
161 ## Determine whether or not the "T" separator is needed to separate date and time elements
7ad273d8
SBS
162 if ( $displayHours || $displayMinutes || $displaySeconds); then
163 displayDateTime="true"; else displayDateTime="false"; fi
164
165 ## Construct duration output string
166 OUTPUT="P"
167 if $displayYears; then
168 OUTPUT=$OUTPUT$fullYears"Y"; fi
169 if $displayMonths; then
170 OUTPUT=$OUTPUT$fullMonths"M"; fi
171 if $displayDays; then
172 OUTPUT=$OUTPUT$fullDays"D"; fi
173 if $displayDateTime; then
174 OUTPUT=$OUTPUT"T"; fi
175 if $displayHours; then
176 OUTPUT=$OUTPUT$fullHours"H"; fi
177 if $displayMinutes; then
178 OUTPUT=$OUTPUT$fullMinutes"M"; fi
179 if $displaySeconds; then
180 OUTPUT=$OUTPUT$fullSeconds"S"; fi
181
182 ## Output duration string to stdout
183 if [[ "$returnState" = "true" ]]; then echo "$OUTPUT"; fi
7ad273d8
SBS
184
185 #===Determine function return code===
186 if [ "$returnState" = "true" ]; then
187 return 0;
188 else
189 echo "$returnState" 1>&2;
190 return 1;
191 fi
192
193} # Get duration (ex: PT10M4S )
194
195#==BEGIN sample code==
196echo "Precision 6 duration:$(timeDuration "$(date +%s)" 6)"
197echo "Precision 5 duration:$(timeDuration "$(date +%s)" 5)"
198echo "Precision 4 duration:$(timeDuration "$(date +%s)" 4)"
199echo "Precision 3 duration:$(timeDuration "$(date +%s)" 3)"
200echo "Precision 2 duration:$(timeDuration "$(date +%s)" 2)"
201echo "Precision 1 duration:$(timeDuration "$(date +%s)" 1)"
202echo "Precision 6 duration:$(timeDuration $((60+60*60+60*60*24+60*60*24*30+60*60*24*365 - (60+60*60+60*60*24+60*60*24*30) )) 6)"
203timeDuration "$@"
204#==END sample code==
205
206# Author: Steven Baltakatei Sandoval
207# License: GPLv3+