Skip to content

Commit e3b5faa

Browse files
authored
Merge branch 'main' into fix/try-tput-first-and-stty-as-fallback
2 parents 3bb19fc + e0c0990 commit e3b5faa

2 files changed

Lines changed: 57 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix process time always shows as 0 ms
66
- Fixed terminal width detection first tput and fall back stty
7+
- Refactor clock optimizing the implementation used to get the time
78

89
## [0.21.0](https://github.com/TypedDevs/bashunit/compare/0.20.0...0.21.0) - 2025-06-18
910

src/clock.sh

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
11
#!/usr/bin/env bash
22

3-
function clock::now() {
3+
_CLOCK_NOW_IMPL=""
4+
5+
function clock::_choose_impl() {
46
local shell_time
57
local attempts=()
68

79
# 1. Try Perl with Time::HiRes
810
attempts+=("Perl")
911
if dependencies::has_perl && perl -MTime::HiRes -e "" &>/dev/null; then
10-
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000000000)' && return 0
12+
_CLOCK_NOW_IMPL="perl"
13+
return 0
1114
fi
1215

1316
# 2. Try Python 3 with time module
1417
attempts+=("Python")
1518
if dependencies::has_python; then
16-
python - <<'EOF'
17-
import time, sys
18-
sys.stdout.write(str(int(time.time() * 1_000_000_000)))
19-
EOF
19+
_CLOCK_NOW_IMPL="python"
2020
return 0
2121
fi
2222

2323
# 3. Try Node.js
2424
attempts+=("Node")
2525
if dependencies::has_node; then
26-
node -e 'process.stdout.write((BigInt(Date.now()) * 1000000n).toString())' && return 0
26+
_CLOCK_NOW_IMPL="node"
27+
return 0
2728
fi
2829
# 4. Windows fallback with PowerShell
2930
attempts+=("PowerShell")
3031
if check_os::is_windows && dependencies::has_powershell; then
31-
powershell -Command "
32-
\$unixEpoch = [DateTime]'1970-01-01 00:00:00';
33-
\$now = [DateTime]::UtcNow;
34-
\$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks;
35-
\$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100;
36-
Write-Output \$nanosecondsSinceEpoch
37-
" && return 0
32+
_CLOCK_NOW_IMPL="powershell"
33+
return 0
3834
fi
3935

4036
# 5. Unix fallback using `date +%s%N` (if not macOS or Alpine)
@@ -43,17 +39,15 @@ EOF
4339
local result
4440
result=$(date +%s%N 2>/dev/null)
4541
if [[ "$result" != *N && "$result" =~ ^[0-9]+$ ]]; then
46-
echo "$result"
42+
_CLOCK_NOW_IMPL="date"
4743
return 0
4844
fi
4945
fi
5046

5147
# 6. Try using native shell EPOCHREALTIME (if available)
5248
attempts+=("EPOCHREALTIME")
5349
if shell_time="$(clock::shell_time)"; then
54-
local seconds="${shell_time%%.*}"
55-
local microseconds="${shell_time#*.}"
56-
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
50+
_CLOCK_NOW_IMPL="shell"
5751
return 0
5852
fi
5953

@@ -63,6 +57,50 @@ EOF
6357
return 1
6458
}
6559

60+
function clock::now() {
61+
if [[ -z "$_CLOCK_NOW_IMPL" ]]; then
62+
clock::_choose_impl || return 1
63+
fi
64+
65+
case "$_CLOCK_NOW_IMPL" in
66+
perl)
67+
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000000000)'
68+
;;
69+
python)
70+
python - <<'EOF'
71+
import time, sys
72+
sys.stdout.write(str(int(time.time() * 1_000_000_000)))
73+
EOF
74+
;;
75+
node)
76+
node -e 'process.stdout.write((BigInt(Date.now()) * 1000000n).toString())'
77+
;;
78+
powershell)
79+
powershell -Command "\
80+
\$unixEpoch = [DateTime]'1970-01-01 00:00:00';\
81+
\$now = [DateTime]::UtcNow;\
82+
\$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks;\
83+
\$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100;\
84+
Write-Output \$nanosecondsSinceEpoch\
85+
"
86+
;;
87+
date)
88+
date +%s%N
89+
;;
90+
shell)
91+
# shellcheck disable=SC2155
92+
local shell_time="$(clock::shell_time)"
93+
local seconds="${shell_time%%.*}"
94+
local microseconds="${shell_time#*.}"
95+
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
96+
;;
97+
*)
98+
clock::_choose_impl || return 1
99+
clock::now
100+
;;
101+
esac
102+
}
103+
66104
function clock::shell_time() {
67105
# Get time directly from the shell variable EPOCHREALTIME (Bash 5+)
68106
[[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME"

0 commit comments

Comments
 (0)