feat(unitprod):Created timeDuration function template
[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) ));
63 #yell "DEBUG:post-Y remainder:$remainder"
64 #yell "DEBUG:fullYears:$fullYears"
65 ## Calculate full months M, update remainder
66 fullMonths=$(( remainder / (30*24*60*60) ));
67 remainder=$(( remainder - (fullMonths*30*24*60*60) ));
68 #yell "DEBUG:post-m remainder:$remainder"
69 #yell "DEBUG:fullMonths:$fullMonths"
70 ## Calculate full days D, update remainder
71 fullDays=$(( remainder / (24*60*60) ));
72 remainder=$(( remainder - (fullDays*24*60*60) ));
73 #yell "DEBUG:post-d remainder:$remainder"
74 #yell "DEBUG:fullMonths:$fullDays"
75 ## Calculate full hours H, update remainder
76 fullHours=$(( remainder / (60*60) ));
77 remainder=$(( remainder - (fullHours*60*60) ));
78 #yell "DEBUG:post-H remainder:$remainder"
79 #yell "DEBUG:fullHours:$fullHours"
80 ## Calculate full minutes M, update remainder
81 fullMinutes=$(( remainder / (60) ));
82 remainder=$(( remainder - (fullMinutes*60) ));
83 #yell "DEBUG:post-M remainder:$remainder"
84 #yell "DEBUG:fullMinutes:$fullMinutes"
85 ## Calculate full seconds S, update remainder
86 fullSeconds=$(( remainder / (1) ));
87 remainder=$(( remainder - (remainder*1) ));
88 #yell "DEBUG:post-S remainder:$remainder"
89 #yell "DEBUG:fullSeconds:$fullSeconds"
90 ## Check which fields filled
91 if [[ $fullYears -gt 0 ]]; then hasYears="true"; else hasYears="false"; fi
92 if [[ $fullMonths -gt 0 ]]; then hasMonths="true"; else hasMonths="false"; fi
93 if [[ $fullDays -gt 0 ]]; then hasDays="true"; else hasDays="false"; fi
94 if [[ $fullHours -gt 0 ]]; then hasHours="true"; else hasHours="false"; fi
95 if [[ $fullMinutes -gt 0 ]]; then hasMinutes="true"; else hasMinutes="false"; fi
96 if [[ $fullSeconds -gt 0 ]]; then hasSeconds="true"; else hasSeconds="false"; fi
97
98 ## Determine which fields to display (see ISO-8601:2004 §4.4.3.2)
99 witherPrecision="false"
100
101 ### Years
102 if $hasYears && [[ $precision -gt 0 ]]; then
103 displayYears="true";
104 witherPrecision="true";
105 else
106 displayYears="false";
107 fi;
108 if $witherPrecision; then ((precision--)); fi;
109
110 ### Months
111 if $hasMonths && [[ $precision -gt 0 ]]; then
112 displayMonths="true";
113 witherPrecision="true";
114 else
115 displayMonths="false";
116 fi;
117 if $witherPrecision && [[ $precision -gt 0 ]]; then
118 displayMonths="true";
119 fi;
120 if $witherPrecision; then ((precision--)); fi;
121
122 ### Days
123 if $hasDays && [[ $precision -gt 0 ]]; then
124 displayDays="true";
125 witherPrecision="true";
126 else
127 displayDays="false";
128 fi;
129 if $witherPrecision && [[ $precision -gt 0 ]]; then
130 displayDays="true";
131 fi;
132 if $witherPrecision; then ((precision--)); fi;
133
134 ### Hours
135 if $hasHours && [[ $precision -gt 0 ]]; then
136 displayHours="true";
137 witherPrecision="true";
138 else
139 displayHours="false";
140 fi;
141 if $witherPrecision && [[ $precision -gt 0 ]]; then
142 displayHours="true";
143 fi;
144 if $witherPrecision; then ((precision--)); fi;
145
146 ### Minutes
147 if $hasMinutes && [[ $precision -gt 0 ]]; then
148 displayMinutes="true";
149 witherPrecision="true";
150 else
151 displayMinutes="false";
152 fi;
153 if $witherPrecision && [[ $precision -gt 0 ]]; then
154 displayMinutes="true";
155 fi;
156 if $witherPrecision; then ((precision--)); fi;
157
158 ### Seconds
159
160 if $hasSeconds && [[ $precision -gt 0 ]]; then
161 displaySeconds="true";
162 witherPrecision="true";
163 else
164 displaySeconds="false";
165 fi;
166 if $witherPrecision && [[ $precision -gt 0 ]]; then
167 displaySeconds="true";
168 fi;
169 if $witherPrecision; then ((precision--)); fi;
170
171
172
173 ## Determine whether or not the "T" separator is needed to separate date and time elements
174 # if ( $displayYears || $displayMonths || $displayDays ) && ( $displayHours || $displayMinutes || $displaySeconds); then
175 if ( $displayHours || $displayMinutes || $displaySeconds); then
176 displayDateTime="true"; else displayDateTime="false"; fi
177
178 ## Construct duration output string
179 OUTPUT="P"
180 if $displayYears; then
181 OUTPUT=$OUTPUT$fullYears"Y"; fi
182 if $displayMonths; then
183 OUTPUT=$OUTPUT$fullMonths"M"; fi
184 if $displayDays; then
185 OUTPUT=$OUTPUT$fullDays"D"; fi
186 if $displayDateTime; then
187 OUTPUT=$OUTPUT"T"; fi
188 if $displayHours; then
189 OUTPUT=$OUTPUT$fullHours"H"; fi
190 if $displayMinutes; then
191 OUTPUT=$OUTPUT$fullMinutes"M"; fi
192 if $displaySeconds; then
193 OUTPUT=$OUTPUT$fullSeconds"S"; fi
194
195 ## Output duration string to stdout
196 if [[ "$returnState" = "true" ]]; then echo "$OUTPUT"; fi
197
198
199 # if [[ "$OPTION_DATEPARSE" = "true" ]]; then
200 # echo "asdf";
201 # # Get current time for parsing year/month/day idiosyncrasies
202 # DATE_NOW="$(TZ=":UTC" date --iso-8601 | tr -d '[:punct:]')"; # Produces YYYYMMDD
203 # #TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
204 # # Get date : Given YYYY-MM-DDTHH:MM:SS+ZZ, outputs YYYYMMDD (also supports YYYYYY)
205 # DATE_AGO="$(TZ="UTC" date -d '$ARG1 seconds ago' --iso-8601=seconds | tr -d '[:punct:]' | cut -dT -f1)";
206 # # Get day
207 # DAY_AGO=${DATE_AGO: -2}
208 # # Get month
209 # MONTH_AGO=${DATE_AGO: -4: -2}
210 # # Get year
211 # YEAR_AGO=${YEAR: 0: -4}
212
213 # fi
214
215 #===Determine function return code===
216 if [ "$returnState" = "true" ]; then
217 return 0;
218 else
219 echo "$returnState" 1>&2;
220 return 1;
221 fi
222
223} # Get duration (ex: PT10M4S )
224
225#==BEGIN sample code==
226echo "Precision 6 duration:$(timeDuration "$(date +%s)" 6)"
227echo "Precision 5 duration:$(timeDuration "$(date +%s)" 5)"
228echo "Precision 4 duration:$(timeDuration "$(date +%s)" 4)"
229echo "Precision 3 duration:$(timeDuration "$(date +%s)" 3)"
230echo "Precision 2 duration:$(timeDuration "$(date +%s)" 2)"
231echo "Precision 1 duration:$(timeDuration "$(date +%s)" 1)"
232echo "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)"
233timeDuration "$@"
234#==END sample code==
235
236# Author: Steven Baltakatei Sandoval
237# License: GPLv3+