Skip to content

Commit 532e322

Browse files
committed
refactor: clock::now
1 parent fa35329 commit 532e322

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/clock.sh

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,66 @@
22

33
function clock::now() {
44
local shell_time
5+
6+
# 1. Try using native shell EPOCHREALTIME (if available)
57
if shell_time="$(clock::shell_time)"; then
68
local seconds="${shell_time%%.*}"
79
local microseconds="${shell_time#*.}"
8-
910
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
1011
return 0
1112
fi
1213

13-
if dependencies::has_perl && perl -MTime::HiRes -e "" > /dev/null 2>&1; then
14-
if perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000000000)'; then
15-
return 0
16-
fi
14+
# 2. Try Perl with Time::HiRes
15+
if dependencies::has_perl && perl -MTime::HiRes -e "" &>/dev/null; then
16+
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000000000)' && return 0
1717
fi
1818

19+
# 3. Windows fallback with PowerShell
1920
if check_os::is_windows && dependencies::has_powershell; then
20-
powershell -Command "
21-
\$unixEpoch = [DateTime]'1970-01-01 00:00:00';
22-
\$now = [DateTime]::UtcNow;
23-
\$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks;
24-
\$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100;
25-
Write-Output \$nanosecondsSinceEpoch
26-
"
27-
return 0
21+
powershell -Command "
22+
\$unixEpoch = [DateTime]'1970-01-01 00:00:00';
23+
\$now = [DateTime]::UtcNow;
24+
\$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks;
25+
\$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100;
26+
Write-Output \$nanosecondsSinceEpoch
27+
" && return 0
2828
fi
2929

30+
# 4. Unix fallback using `date +%s%N` (if not macOS or Alpine)
3031
if ! check_os::is_macos && ! check_os::is_alpine; then
3132
local result
32-
result=$(date +%s%N)
33-
if [[ "$result" != *N ]] && [[ "$result" -gt 0 ]]; then
33+
result=$(date +%s%N 2>/dev/null)
34+
if [[ "$result" != *N && "$result" =~ ^[0-9]+$ ]]; then
3435
echo "$result"
3536
return 0
3637
fi
3738
fi
3839

40+
# 5. All methods failed
3941
echo ""
4042
return 1
4143
}
4244

4345
function clock::shell_time() {
44-
# Get time directly from the shell rather than a program.
46+
# Get time directly from the shell variable EPOCHREALTIME (Bash 5+)
4547
[[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME"
4648
}
4749

48-
4950
function clock::total_runtime_in_milliseconds() {
51+
local end_time
5052
end_time=$(clock::now)
5153
if [[ -n $end_time ]]; then
52-
math::calculate "($end_time-$_START_TIME)/1000000"
54+
math::calculate "($end_time - $_START_TIME) / 1000000"
5355
else
5456
echo ""
5557
fi
5658
}
5759

5860
function clock::total_runtime_in_nanoseconds() {
61+
local end_time
5962
end_time=$(clock::now)
6063
if [[ -n $end_time ]]; then
61-
math::calculate "($end_time-$_START_TIME)"
64+
math::calculate "$end_time - $_START_TIME"
6265
else
6366
echo ""
6467
fi

0 commit comments

Comments
 (0)