It is much simpler to measure time in seconds since epoch (
+%s) Then elapsed time in seconds in found by merely substracting and conversion from seconds to minute just requires dividing by 60:
export etool_start=$(date +"%s")
export etool_stop=$(date +"%s")
echo "elapsed time=$(( (etool_stop - etool_start)/60 )) minutes"
If you do need the times in human form, you can convert them. Using GNU
$ date -d "@$etool_start"
Tue Jul 5 21:30:19 PDT 2016
$ date -d "@$etool_start" "+%Y%m%d%H%M"
More on formatting
To express the elapsed time in minutes in different formats, we can use
printf. For example, to produce the elapsed time with four digits, we can use the
$ printf '%04i\n' "$(( ($etool_stop - $etool_start)/60 ))"
A format of
%4i would give us an integer (i) in four characters. If we want leading zeros instead of leading spaces, then we use
If we want the opposite sign for the number:
$ printf '%05i\n' "$(( ($etool_start - $etool_stop)/60 ))"
Because the minus sign requires an extra character, we have increased the allotted space from 4 to 5:
Because the above example used bash's arithmetic which is integer-only. If you want elapsed time in minutes with fractions of a minute, another tool, such as
bc, can be used:
$ echo "($etool_stop - $etool_start)/60" | bc -l
What is seconds since epoch?
man date, seconds since epoch is defined by:
seconds since 1970-01-01 00:00:00 UTC
Since it is defined this way, local time zone is irrelevant. Consequently, computing it under the "local time zone" or under universal time,
-u, makes no difference:
$ date +%s; date -u +%s