Skip to content

Commit ad2b558

Browse files
authored
Merge pull request #2172 from davidpfarrell/bug/duration_locale
bug: Use en_US when fetching EPOCHREALTIME
2 parents ec3c06f + 7c7e4f9 commit ad2b558

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lib/command_duration.bash

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22
#
33
# Functions for measuring and reporting how long a command takes to run.
44

5-
: "${COMMAND_DURATION_START_SECONDS:=${EPOCHREALTIME:-$SECONDS}}"
5+
# Get shell duration in decimal format regardless of runtime locale.
6+
# Notice: This function runs as a sub-shell - notice '(' vs '{'.
7+
function _shell_duration_en() (
8+
# DFARREL You would think LC_NUMERIC would do it, but not working in my local
9+
LC_ALL='en_US.UTF-8'
10+
printf "%s" "${EPOCHREALTIME:-$SECONDS}"
11+
)
12+
13+
: "${COMMAND_DURATION_START_SECONDS:=$(_shell_duration_en)}"
614
: "${COMMAND_DURATION_ICON:=🕘}"
715
: "${COMMAND_DURATION_MIN_SECONDS:=1}"
816

917
function _command_duration_pre_exec() {
10-
COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
18+
COMMAND_DURATION_START_SECONDS="$(_shell_duration_en)"
19+
}
20+
21+
function _command_duration_pre_cmd() {
22+
COMMAND_DURATION_START_SECONDS=""
1123
}
1224

1325
function _dynamic_clock_icon {
@@ -20,13 +32,15 @@ function _dynamic_clock_icon {
2032

2133
function _command_duration() {
2234
[[ -n "${BASH_IT_COMMAND_DURATION:-}" ]] || return
35+
[[ -n "${COMMAND_DURATION_START_SECONDS:-}" ]] || return
2336

2437
local command_duration=0 command_start="${COMMAND_DURATION_START_SECONDS:-0}"
2538
local -i minutes=0 seconds=0 deciseconds=0
2639
local -i command_start_seconds="${command_start%.*}"
2740
local -i command_start_deciseconds=$((10#${command_start##*.}))
2841
command_start_deciseconds="${command_start_deciseconds:0:1}"
29-
local current_time="${EPOCHREALTIME:-$SECONDS}"
42+
local current_time
43+
current_time="$(_shell_duration_en)"
3044
local -i current_time_seconds="${current_time%.*}"
3145
local -i current_time_deciseconds="$((10#${current_time##*.}))"
3246
current_time_deciseconds="${current_time_deciseconds:0:1}"
@@ -59,3 +73,4 @@ function _command_duration() {
5973
}
6074

6175
_bash_it_library_finalize_hook+=("safe_append_preexec '_command_duration_pre_exec'")
76+
_bash_it_library_finalize_hook+=("safe_append_prompt_command '_command_duration_pre_cmd'")

plugins/available/cmd-returned-notify.plugin.bash

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ about-plugin 'Alert (BEL) when process ends after a threshold of seconds'
44

55
function precmd_return_notification() {
66
local command_start="${COMMAND_DURATION_START_SECONDS:=0}"
7-
local current_time="${EPOCHREALTIME:-$SECONDS}"
7+
local current_time
8+
current_time="$(_shell_duration_en)"
89
local -i command_duration="$((${current_time%.*} - ${command_start%.*}))"
910
if [[ "${command_duration}" -gt "${NOTIFY_IF_COMMAND_RETURNS_AFTER:-5}" ]]; then
1011
printf '\a'

test/plugins/cmd-returned-notify.plugin.bats

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function local_setup_file() {
99

1010
@test "plugins cmd-returned-notify: notify after elapsed time" {
1111
export NOTIFY_IF_COMMAND_RETURNS_AFTER=0
12-
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
12+
export COMMAND_DURATION_START_SECONDS="$(_shell_duration_en)"
1313
sleep 1
1414
run precmd_return_notification
1515
assert_success
@@ -18,7 +18,7 @@ function local_setup_file() {
1818

1919
@test "plugins cmd-returned-notify: do not notify before elapsed time" {
2020
export NOTIFY_IF_COMMAND_RETURNS_AFTER=10
21-
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
21+
export COMMAND_DURATION_START_SECONDS="$(_shell_duration_en)"
2222
sleep 1
2323
run precmd_return_notification
2424
assert_success
@@ -34,7 +34,7 @@ function local_setup_file() {
3434
@test "lib command_duration: preexec set COMMAND_DURATION_START_SECONDS" {
3535
export COMMAND_DURATION_START_SECONDS=
3636
assert_equal "${COMMAND_DURATION_START_SECONDS}" ""
37-
NOW="${EPOCHREALTIME:-$SECONDS}"
37+
NOW="$(_shell_duration_en)"
3838
_command_duration_pre_exec
3939
# We need to make sure to account for nanoseconds...
4040
assert_equal "${COMMAND_DURATION_START_SECONDS%.*}" "${NOW%.*}"

0 commit comments

Comments
 (0)