-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-implement shellspec-time for locale support
- Loading branch information
Showing
8 changed files
with
193 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
|
||
real 1.23 | ||
user 0.11 | ||
sys 12.45 | ||
real:1.23 user:0.11 sys:12.45 type:external |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,185 @@ | ||
#!/bin/sh | ||
#shellcheck disable=SC2004 | ||
# shellcheck disable=SC2004 | ||
# Write it to work in a Bourne shell if possible. | ||
|
||
set -f | ||
|
||
: "${SHELLSPEC_TRAP:=trap}" | ||
: "${SHELLSPEC_TIME_TYPE:=auto}" | ||
LF=' | ||
' | ||
|
||
"$SHELLSPEC_TRAP" : INT | ||
|
||
if [ "$BASH_VERSION" ] || [ "$KSH_VERSION" ]; then | ||
time -p "$@" | ||
exit $? | ||
fi | ||
datetime2unixtime() { | ||
set -- "$1" "${2%%-*}" "${2%%T*}" "${2##*T}" | ||
set -- "$1" "${2#"${2%%[!0]*}"}" "${3#*-}" "${4%%:*}" "${4#*:}" | ||
set -- "$1" "$2" "${3%%-*}" "${3#*-}" "$4" "${5%%:*}" "${5#*:}" | ||
set -- "$1" "${2:-0}" "${3#0}" "${4#0}" "${5#0}" "${6#0}" "${7#0}" | ||
[ "$3" -lt 3 ] && set -- "$1" $(($2-1)) $(($3+12)) "$4" "$5" "$6" "$7" | ||
set -- "$1" $((365*$2+$2/4-$2/100+$2/400)) "$3" "$4" "$5" "$6" "$7" | ||
set -- "$1" "$2" $(((306*($3+1)/10)-428)) "$4" "$5" "$6" "${7%.*}" "${7#*.}" | ||
eval "$1=$((($2+$3+$4-719163)*86400+$5*3600+$6*60+$7))${8:+.}${8}" | ||
} | ||
|
||
time_using_date() { | ||
date +'start %Y-%m-%dT%H:%M:%S.%N' | ||
"$@" | ||
set -- $? | ||
date +'end %Y-%m-%dT%H:%M:%S.%N' | ||
return "$1" | ||
} | ||
|
||
detect_time_type() { | ||
[ "$SHELLSPEC_TIME_TYPE" = auto ] || return 0 | ||
|
||
if ! { time true; } >/dev/null 2>&1; then | ||
SHELLSPEC_TIME_TYPE=external-date && return 0 | ||
fi | ||
|
||
if [ ! "${KSH_VERSION:-}" ]; then | ||
# shellcheck disable=SC2006 | ||
KSH_VERSION=`eval 'echo ${.sh.version}'` | ||
fi 2>/dev/null | ||
|
||
if [ "${BASH_VERSION:-}" ]; then | ||
SHELLSPEC_TIME_TYPE=bash-builtin && return 0 | ||
fi | ||
|
||
if [ "${KSH_VERSION:-}" ]; then | ||
case $KSH_VERSION in | ||
*PD\ KSH*) SHELLSPEC_TIME_TYPE=pdksh-builtin ;; | ||
*MIRBSD\ KSH*) SHELLSPEC_TIME_TYPE=mksh-builtin ;; | ||
*LEGACY\ KSH*) SHELLSPEC_TIME_TYPE=lksh-builtin ;; | ||
*) SHELLSPEC_TIME_TYPE=ksh93-builtin ;; | ||
esac | ||
return 0 | ||
fi | ||
|
||
if [ "${ZSH_VERSION:-}" ]; then | ||
SHELLSPEC_TIME_TYPE=zsh-builtin && return 0 | ||
fi | ||
|
||
if { time -p true; } >/dev/null 2>&1; then | ||
SHELLSPEC_TIME_TYPE=external-time | ||
else | ||
SHELLSPEC_TIME_TYPE=legacy-time | ||
fi | ||
} | ||
|
||
detect_time_type | ||
|
||
{ | ||
( | ||
"$SHELLSPEC_TRAP" : INT | ||
|
||
set -- -- "$@" | ||
|
||
set -eu | ||
case ${LC_ALL+x} in | ||
?) set -- -s LC_ALL "$LC_ALL" "$@" ;; | ||
*) set -- -u LC_ALL "$@" ;; | ||
esac | ||
LC_ALL=C && export LC_ALL | ||
|
||
read -r sec_start millisec_start <<HERE | ||
$(date +"%s %2N") | ||
HERE | ||
# bash or ksh93 | ||
case ${TIMEFORMAT+x} in | ||
?) set -- -s TIMEFORMAT "$TIMEFORMAT" "$@" ;; | ||
*) set -- -u TIMEFORMAT "$@" ;; | ||
esac | ||
TIMEFORMAT="real %R${LF}user %U${LF}sys %S" | ||
|
||
case $millisec_start in (*[!0-9]*) | ||
millisec_start=0 | ||
esac | ||
# zsh | ||
case ${TIMEFMT+x} in | ||
?) set -- -s TIMEFMT "$TIMEFMT" "$@" ;; | ||
*) set -- -u TIMEFMT "$@" ;; | ||
esac | ||
TIMEFMT="real %*E${LF}user %*U${LF}sys %*S" | ||
|
||
status=0 | ||
# GNU time | ||
case ${TIME+x} in | ||
?) set -- -s TIME "$TIME" "$@" ;; | ||
*) set -- -u TIME "$@" ;; | ||
esac | ||
TIME="real %e${LF}user %U${LF}sys %S" && export TIME | ||
|
||
if [ $# -gt 0 ]; then | ||
"$@" &&: | ||
status=$? | ||
fi | ||
# shellcheck disable=SC2016 | ||
set -- sh -c ' | ||
# Use Bourne shell syntax as sh may be a Bourne shell | ||
while [ $# -gt 0 ]; do | ||
case $1 in | ||
-s) eval "$2=\"\$3\""; export "$2"; shift 2 ;; | ||
-u) unset "$2"; shift ;; | ||
--) shift; break ;; | ||
*) break ;; | ||
esac | ||
shift | ||
done | ||
exec "$@" 2>&3 3>&- >&4 | ||
' "$0" "$@" | ||
|
||
read -r sec_end millisec_end <<HERE | ||
$(date +"%s %2N") | ||
HERE | ||
case $SHELLSPEC_TIME_TYPE in | ||
external-date) | ||
time_using_date "$@" | ||
;; | ||
external-bash) | ||
bash -c 'TIMEFORMAT=$1; shift; time "$@"' "$0" "$TIMEFORMAT" "$@" | ||
;; | ||
external-ksh) | ||
ksh -c 'TIMEFORMAT=$1; shift; time "$@"' "$0" "$TIMEFORMAT" "$@" | ||
;; | ||
external-zsh) | ||
zsh -c 'TIMEFMT=$1; shift; time "$@"' "$0" "$TIMEFMT" "$@" | ||
;; | ||
bash-builtin | ksh93-builtin | zsh-builtin | legacy-time) | ||
time "$@" | ||
;; | ||
mksh-builtin | lksh-builtin | pdksh-builtin | external-time | *) | ||
time -p "$@" | ||
;; | ||
esac | ||
echo "status $?" >&2 | ||
) 2>&1 | ( | ||
"$SHELLSPEC_TRAP" '' INT | ||
|
||
case $millisec_end in (*[!0-9]*) | ||
millisec_end=0 | ||
esac | ||
real='' user='' sys='' type=$SHELLSPEC_TIME_TYPE ex='' | ||
# shellcheck disable=SC2162 | ||
|
||
sec=$(($sec_end - $sec_start)) | ||
millisec=$((1$millisec_end - 1$millisec_start)) | ||
if [ $millisec -lt 0 ]; then | ||
millisec=$(($millisec + 100)) | ||
sec=$(($sec - 1)) | ||
fi | ||
[ $millisec -lt 10 ] && millisec="0$millisec" | ||
while read name time; do | ||
# ksh88: 1m2.34s | ||
case $time in *m*s) | ||
type=ksh88 | ||
min=${time%m*} && time=${time#*m} | ||
sec=${time%.*} && time=${time#*.} && time=${time%s} | ||
time="$(($min * 60 + $sec)).$time" | ||
esac | ||
|
||
echo "real $sec.$millisec" >&2 | ||
case $name in | ||
real) real=$time ;; | ||
user) user=$time ;; | ||
sys) sys=$time ;; | ||
start) start=$time ;; | ||
end) end=$time ;; | ||
status) ex=$time ;; | ||
esac | ||
done | ||
|
||
exit $status | ||
if [ ! "$real" ]; then | ||
datetime2unixtime start "$start" | ||
datetime2unixtime end "$end" | ||
real=$((${end%.*} - ${start%.*})) | ||
case $start in | ||
*[!0-9.]*) ;; | ||
*) | ||
start="${start#*.}00" end="${end#*.}00" | ||
start="1${start%"${start#??}"}" end="3${end%"${end#??}"}" | ||
diff=$(($end - $start)) | ||
real="$(( $real - ($diff < 200) )).${diff#[12]}" | ||
esac | ||
fi | ||
if [ "${SHELLSPEC_TIME_LOG:+x}" ]; then | ||
exec 2>"$SHELLSPEC_TIME_LOG" | ||
fi | ||
echo "real:${real:-0} user:$user sys:$sys type:$type" >&2 | ||
exit "${ex:-1}" | ||
) | ||
} 3>&2 4>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters