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 ## Calculate full months M, update remainder
64 fullMonths
=$
(( remainder
/ (30*24*60*60) ));
65 remainder
=$
(( remainder
- (fullMonths
*30*24*60*60) ));
66 ## Calculate full days D, update remainder
67 fullDays
=$
(( remainder
/ (24*60*60) ));
68 remainder
=$
(( remainder
- (fullDays
*24*60*60) ));
69 ## Calculate full hours H, update remainder
70 fullHours
=$
(( remainder
/ (60*60) ));
71 remainder
=$
(( remainder
- (fullHours
*60*60) ));
72 ## Calculate full minutes M, update remainder
73 fullMinutes
=$
(( remainder
/ (60) ));
74 remainder
=$
(( remainder
- (fullMinutes
*60) ));
75 ## Calculate full seconds S, update remainder
76 fullSeconds
=$
(( remainder
/ (1) ));
77 remainder
=$
(( remainder
- (remainder
*1) ));
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
86 ## Determine which fields to display (see ISO-8601:2004 §4.4.3.2)
87 witherPrecision
="false"
90 if $hasYears && [[ $precision -gt 0 ]]; then
92 witherPrecision
="true";
96 if $witherPrecision; then ((precision--
)); fi;
99 if $hasMonths && [[ $precision -gt 0 ]]; then
100 displayMonths
="true";
101 witherPrecision
="true";
103 displayMonths
="false";
105 if $witherPrecision && [[ $precision -gt 0 ]]; then
106 displayMonths
="true";
108 if $witherPrecision; then ((precision--
)); fi;
111 if $hasDays && [[ $precision -gt 0 ]]; then
113 witherPrecision
="true";
117 if $witherPrecision && [[ $precision -gt 0 ]]; then
120 if $witherPrecision; then ((precision--
)); fi;
123 if $hasHours && [[ $precision -gt 0 ]]; then
125 witherPrecision
="true";
127 displayHours
="false";
129 if $witherPrecision && [[ $precision -gt 0 ]]; then
132 if $witherPrecision; then ((precision--
)); fi;
135 if $hasMinutes && [[ $precision -gt 0 ]]; then
136 displayMinutes
="true";
137 witherPrecision
="true";
139 displayMinutes
="false";
141 if $witherPrecision && [[ $precision -gt 0 ]]; then
142 displayMinutes
="true";
144 if $witherPrecision; then ((precision--
)); fi;
148 if $hasSeconds && [[ $precision -gt 0 ]]; then
149 displaySeconds
="true";
150 witherPrecision
="true";
152 displaySeconds
="false";
154 if $witherPrecision && [[ $precision -gt 0 ]]; then
155 displaySeconds
="true";
157 if $witherPrecision; then ((precision--
)); fi;
161 ## Determine whether or not the "T" separator is needed to separate date and time elements
162 if ( $displayHours ||
$displayMinutes ||
$displaySeconds); then
163 displayDateTime
="true"; else displayDateTime
="false"; fi
165 ## Construct duration output string
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
182 ## Output duration string to stdout
183 if [[ "$returnState" = "true" ]]; then echo "$OUTPUT"; fi
185 #===Determine function return code===
186 if [ "$returnState" = "true" ]; then
189 echo "$returnState" 1>&2;
193 } # Get duration (ex: PT10M4S )
195 #==BEGIN sample code==
196 echo "Precision 6 duration:$(timeDuration "$
(date +%s
)" 6)"
197 echo "Precision 5 duration:$(timeDuration "$
(date +%s
)" 5)"
198 echo "Precision 4 duration:$(timeDuration "$
(date +%s
)" 4)"
199 echo "Precision 3 duration:$(timeDuration "$
(date +%s
)" 3)"
200 echo "Precision 2 duration:$(timeDuration "$
(date +%s
)" 2)"
201 echo "Precision 1 duration:$(timeDuration "$
(date +%s
)" 1)"
202 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)"
206 # Author: Steven Baltakatei Sandoval