Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix locales with commas in $EPOCHREALTIME and occasional failures #7

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions bash-timer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ bashtimer_preexec() {
# https://www.reddit.com/r/bash/comments/ivz276/tired_of_typing_time_all_the_time_try_bashtimer/g5wui2l/
if [ ! -z "$EPOCHREALTIME" ]; then
# Replace "," decimal separator with ".". This is needed for European locales, among others.
EPOCHREALTIME="${EPOCHREALTIME/,/.}"
local _EPOCHREALTIME="${EPOCHREALTIME/,/.}"

begin_s=${EPOCHREALTIME%.*}
begin_ns=${EPOCHREALTIME#*.}
begin_s=${_EPOCHREALTIME%.*}
begin_ns=${_EPOCHREALTIME#*.}
begin_ns="${begin_ns#0}"
else
read begin_s begin_ns <<< $(date +"%s %N")
Expand All @@ -85,20 +85,20 @@ bashtimer_precmd() {
# https://www.reddit.com/r/bash/comments/ivz276/tired_of_typing_time_all_the_time_try_bashtimer/g5wui2l/
if [ ! -z "$EPOCHREALTIME" ]; then
# Replace "," decimal separator with ".". This is needed for European locales, among others.
EPOCHREALTIME="${EPOCHREALTIME/,/.}"
local _EPOCHREALTIME="${EPOCHREALTIME/,/.}"

end_s=${EPOCHREALTIME%.*}
end_s=${_EPOCHREALTIME%.*}
# echo "Begin Seconds: $begin_s | End Seconds: $end_s"
end_ns=${EPOCHREALTIME#*.}
end_ns=${_EPOCHREALTIME#*.}
end_ns="${end_ns#0}"

# Convert strings with leading zeros to base 10 integers
begin_ns=$((10#$begin_ns))
end_ns=$((10#$end_ns))
if [ $end_ns -lt $begin_ns ]; then
end_ns=$((1000000 + $end_ns))
end_s=$(($end_s - 1))
fi
# Convert strings with leading zeros to base 10 integers
begin_ns=$((10#$begin_ns))
end_ns=$((10#$end_ns))

s=$((end_s - begin_s))
if [ "$end_ns" -ge "$begin_ns" ]; then
Expand Down