Skip to content

Commit 41bdc27

Browse files
authored
Merge pull request #437 from TypedDevs/fix/436-clock-time
Fix process time always shows as '0 ms'
2 parents c1b0e17 + 69a3526 commit 41bdc27

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Fix process time always shows as 0 ms
6+
37
## [0.21.0](https://github.com/TypedDevs/bashunit/compare/0.20.0...0.21.0) - 2025-06-18
48

59
- Fix typo "to has been called"

src/clock.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,13 @@ function clock::now() {
44
local shell_time
55
local attempts=()
66

7-
# 1. Try using native shell EPOCHREALTIME (if available)
8-
attempts+=("EPOCHREALTIME")
9-
if shell_time="$(clock::shell_time)"; then
10-
local seconds="${shell_time%%.*}"
11-
local microseconds="${shell_time#*.}"
12-
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
13-
return 0
14-
fi
15-
16-
# 2. Try Perl with Time::HiRes
7+
# 1. Try Perl with Time::HiRes
178
attempts+=("Perl")
189
if dependencies::has_perl && perl -MTime::HiRes -e "" &>/dev/null; then
1910
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000000000)' && return 0
2011
fi
2112

22-
# 3. Try Python 3 with time module
13+
# 2. Try Python 3 with time module
2314
attempts+=("Python")
2415
if dependencies::has_python; then
2516
python - <<'EOF'
@@ -29,12 +20,12 @@ EOF
2920
return 0
3021
fi
3122

32-
# 4. Try Node.js
23+
# 3. Try Node.js
3324
attempts+=("Node")
3425
if dependencies::has_node; then
3526
node -e 'process.stdout.write((BigInt(Date.now()) * 1000000n).toString())' && return 0
3627
fi
37-
# 5. Windows fallback with PowerShell
28+
# 4. Windows fallback with PowerShell
3829
attempts+=("PowerShell")
3930
if check_os::is_windows && dependencies::has_powershell; then
4031
powershell -Command "
@@ -46,7 +37,7 @@ EOF
4637
" && return 0
4738
fi
4839

49-
# 6. Unix fallback using `date +%s%N` (if not macOS or Alpine)
40+
# 5. Unix fallback using `date +%s%N` (if not macOS or Alpine)
5041
attempts+=("date")
5142
if ! check_os::is_macos && ! check_os::is_alpine; then
5243
local result
@@ -57,6 +48,15 @@ EOF
5748
fi
5849
fi
5950

51+
# 6. Try using native shell EPOCHREALTIME (if available)
52+
attempts+=("EPOCHREALTIME")
53+
if shell_time="$(clock::shell_time)"; then
54+
local seconds="${shell_time%%.*}"
55+
local microseconds="${shell_time#*.}"
56+
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
57+
return 0
58+
fi
59+
6060
# 7. All methods failed
6161
printf "clock::now implementations tried: %s\n" "${attempts[*]}" >&2
6262
echo ""

tests/unit/clock_test.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ function test_runtime_in_milliseconds_when_not_empty_time() {
9191
assert_not_empty "$(clock::total_runtime_in_milliseconds)"
9292
}
9393

94+
function test_now_prefers_perl_over_shell_time() {
95+
mock clock::shell_time echo "1234.0"
96+
mock perl echo "999999999999"
97+
mock dependencies::has_python mock_false
98+
mock dependencies::has_node mock_false
99+
100+
assert_same "999999999999" "$(clock::now)"
101+
}
102+
103+
function test_now_prefers_python_over_node() {
104+
mock perl mock_non_existing_fn
105+
mock dependencies::has_python mock_true
106+
mock python echo "777777777777"
107+
mock dependencies::has_node mock_true
108+
mock node echo "888888888888"
109+
110+
assert_same "777777777777" "$(clock::now)"
111+
}
112+
94113
function test_runtime_in_milliseconds_when_empty_time() {
95114
mock_macos
96115
mock perl mock_non_existing_fn

0 commit comments

Comments
 (0)