-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathclock.sh
More file actions
145 lines (130 loc) · 3.42 KB
/
clock.sh
File metadata and controls
145 lines (130 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
_CLOCK_NOW_IMPL=""
function clock::_choose_impl() {
local shell_time
local attempts
attempts=()
# 1. Try Perl with Time::HiRes
attempts+=("Perl")
if dependencies::has_perl && perl -MTime::HiRes -e "" &>/dev/null; then
_CLOCK_NOW_IMPL="perl"
return 0
fi
# 2. Try Python 3 with time module
attempts+=("Python")
if dependencies::has_python; then
_CLOCK_NOW_IMPL="python"
return 0
fi
# 3. Try Node.js
attempts+=("Node")
if dependencies::has_node; then
_CLOCK_NOW_IMPL="node"
return 0
fi
# 4. Windows fallback with PowerShell
attempts+=("PowerShell")
if check_os::is_windows && dependencies::has_powershell; then
_CLOCK_NOW_IMPL="powershell"
return 0
fi
# 5. Unix fallback using `date +%s%N` (if not macOS or Alpine)
attempts+=("date")
if ! check_os::is_macos && ! check_os::is_alpine; then
local result
local number_pattern='^[0-9]+$'
result=$(date +%s%N 2>/dev/null)
if [[ "$result" != *N && "$result" =~ $number_pattern ]]; then
_CLOCK_NOW_IMPL="date"
return 0
fi
fi
# 6. Try using native shell EPOCHREALTIME (if available)
attempts+=("EPOCHREALTIME")
if shell_time="$(clock::shell_time)"; then
_CLOCK_NOW_IMPL="shell"
return 0
fi
# 7. Very last fallback: seconds resolution only
attempts[${#attempts[@]}]="date-seconds"
if date +%s &>/dev/null; then
_CLOCK_NOW_IMPL="date-seconds"
return 0
fi
# 8. All methods failed
printf "clock::now implementations tried: %s\n" "${attempts[*]}" >&2
echo ""
return 1
}
function clock::now() {
if [[ -z "$_CLOCK_NOW_IMPL" ]]; then
clock::_choose_impl || return 1
fi
case "$_CLOCK_NOW_IMPL" in
perl)
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000000000)'
;;
python)
python - <<'EOF'
import time, sys
sys.stdout.write(str(int(time.time() * 1_000_000_000)))
EOF
;;
node)
node -e 'process.stdout.write((BigInt(Date.now()) * 1000000n).toString())'
;;
powershell)
powershell -Command "\
\$unixEpoch = [DateTime]'1970-01-01 00:00:00';\
\$now = [DateTime]::UtcNow;\
\$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks;\
\$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100;\
Write-Output \$nanosecondsSinceEpoch\
"
;;
date)
date +%s%N
;;
date-seconds)
local seconds
seconds=$(date +%s)
math::calculate "$seconds * 1000000000"
;;
shell)
# shellcheck disable=SC2155
local shell_time="$(clock::shell_time)"
local seconds="${shell_time%%.*}"
local microseconds="${shell_time#*.}"
math::calculate "($seconds * 1000000000) + ($microseconds * 1000)"
;;
*)
clock::_choose_impl || return 1
clock::now
;;
esac
}
function clock::shell_time() {
# Get time directly from the shell variable EPOCHREALTIME (Bash 5+)
[[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME"
}
function clock::total_runtime_in_milliseconds() {
local end_time
end_time=$(clock::now)
if [[ -n $end_time ]]; then
math::calculate "($end_time - $_START_TIME) / 1000000"
else
echo ""
fi
}
function clock::total_runtime_in_nanoseconds() {
local end_time
end_time=$(clock::now)
if [[ -n $end_time ]]; then
math::calculate "$end_time - $_START_TIME"
else
echo ""
fi
}
function clock::init() {
_START_TIME=$(clock::now)
}