3 # Desc: Template to indicate time duration in ISO-8601 format
5 yell
() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
6 die
() { yell
"$*"; exit 111; }
7 try
() { "$@" || die
"cannot $*"; }
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
;
21 precision
=2; # set default precision
22 returnState
="true"; # set default return state
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
29 # Check that arg1 provided
30 if [[ $# -ge 1 ]]; then
31 # Check that arg1 is a positive integer
32 if [[ "$ARG1" =~ ^
[[:digit
:]]+$
]]; then
35 yell
"ERROR:ARG1 not a digit.";
36 returnState
="ERROR_INPUT";
40 yell
"ERROR:No argument provided. Exiting.";
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
51 yell
"ERROR:ARG2 not a positive integer. (is $ARG2 ). Leaving early.";
52 returnState
="ERROR_INPUT";
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
98 ## Determine which fields to display (see ISO-8601:2004 §4.4.3.2)
99 witherPrecision
="false"
102 if $hasYears && [[ $precision -gt 0 ]]; then
104 witherPrecision
="true";
106 displayYears
="false";
108 if $witherPrecision; then ((precision--
)); fi;
111 if $hasMonths && [[ $precision -gt 0 ]]; then
112 displayMonths
="true";
113 witherPrecision
="true";
115 displayMonths
="false";
117 if $witherPrecision && [[ $precision -gt 0 ]]; then
118 displayMonths
="true";
120 if $witherPrecision; then ((precision--
)); fi;
123 if $hasDays && [[ $precision -gt 0 ]]; then
125 witherPrecision
="true";
129 if $witherPrecision && [[ $precision -gt 0 ]]; then
132 if $witherPrecision; then ((precision--
)); fi;
135 if $hasHours && [[ $precision -gt 0 ]]; then
137 witherPrecision
="true";
139 displayHours
="false";
141 if $witherPrecision && [[ $precision -gt 0 ]]; then
144 if $witherPrecision; then ((precision--
)); fi;
147 if $hasMinutes && [[ $precision -gt 0 ]]; then
148 displayMinutes
="true";
149 witherPrecision
="true";
151 displayMinutes
="false";
153 if $witherPrecision && [[ $precision -gt 0 ]]; then
154 displayMinutes
="true";
156 if $witherPrecision; then ((precision--
)); fi;
160 if $hasSeconds && [[ $precision -gt 0 ]]; then
161 displaySeconds
="true";
162 witherPrecision
="true";
164 displaySeconds
="false";
166 if $witherPrecision && [[ $precision -gt 0 ]]; then
167 displaySeconds
="true";
169 if $witherPrecision; then ((precision--
)); fi;
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
178 ## Construct duration output string
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
195 ## Output duration string to stdout
196 if [[ "$returnState" = "true" ]]; then echo "$OUTPUT"; fi
199 # if [[ "$OPTION_DATEPARSE" = "true" ]]; then
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)";
207 # DAY_AGO=${DATE_AGO: -2}
209 # MONTH_AGO=${DATE_AGO: -4: -2}
211 # YEAR_AGO=${YEAR: 0: -4}
215 #===Determine function return code===
216 if [ "$returnState" = "true" ]; then
219 echo "$returnState" 1>&2;
223 } # Get duration (ex: PT10M4S )
225 #==BEGIN sample code==
226 echo "Precision 6 duration:$(timeDuration "$
(date +%s
)" 6)"
227 echo "Precision 5 duration:$(timeDuration "$
(date +%s
)" 5)"
228 echo "Precision 4 duration:$(timeDuration "$
(date +%s
)" 4)"
229 echo "Precision 3 duration:$(timeDuration "$
(date +%s
)" 3)"
230 echo "Precision 2 duration:$(timeDuration "$
(date +%s
)" 2)"
231 echo "Precision 1 duration:$(timeDuration "$
(date +%s
)" 1)"
232 echo "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)"
236 # Author: Steven Baltakatei Sandoval