1
#!/bin/sh
2
#shellcheck disable=SC2004
3

4 0
"$SHELLSPEC_TRAP" : INT
5

6 0
if [ "$BASH_VERSION" ] || [ "$KSH_VERSION" ]; then
7 0
  time -p "$@"
8 0
  exit $?
9
fi
10

11 0
set -eu
12

13 0
read -r sec_start millisec_start <<HERE
14
$(date +"%s %2N")
15
HERE
16

17 0
case $millisec_start in (*[!0-9]*)
18 0
  millisec_start=0
19
esac
20

21 0
status=0
22

23 0
if [ $# -gt 0 ]; then
24 0
  "$@" &&:
25 0
  status=$?
26
fi
27

28 0
read -r sec_end millisec_end <<HERE
29
$(date +"%s %2N")
30
HERE
31

32 0
case $millisec_end in (*[!0-9]*)
33 0
  millisec_end=0
34
esac
35

36 0
sec=$(($sec_end - $sec_start))
37 0
millisec=$((1$millisec_end - 1$millisec_start))
38 0
if [ $millisec -lt 0 ]; then
39 0
  millisec=$(($millisec + 100))
40 0
  sec=$(($sec - 1))
41
fi
42 0
[ $millisec -lt 10 ] && millisec="0$millisec"
43

44 0
echo "real $sec.$millisec" >&2
45

46 0
exit $status

Read our documentation on viewing source code .

Loading