|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -function clock::now() { |
| 3 | +_CLOCK_NOW_IMPL="" |
| 4 | + |
| 5 | +function clock::_choose_impl() { |
4 | 6 | local shell_time |
5 | 7 | local attempts=() |
6 | 8 |
|
7 | 9 | # 1. Try Perl with Time::HiRes |
8 | 10 | attempts+=("Perl") |
9 | 11 | 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 |
11 | 14 | fi |
12 | 15 |
|
13 | 16 | # 2. Try Python 3 with time module |
14 | 17 | attempts+=("Python") |
15 | 18 | 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" |
20 | 20 | return 0 |
21 | 21 | fi |
22 | 22 |
|
23 | 23 | # 3. Try Node.js |
24 | 24 | attempts+=("Node") |
25 | 25 | 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 |
27 | 28 | fi |
28 | 29 | # 4. Windows fallback with PowerShell |
29 | 30 | attempts+=("PowerShell") |
30 | 31 | 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 |
38 | 34 | fi |
39 | 35 |
|
40 | 36 | # 5. Unix fallback using `date +%s%N` (if not macOS or Alpine) |
|
43 | 39 | local result |
44 | 40 | result=$(date +%s%N 2>/dev/null) |
45 | 41 | if [[ "$result" != *N && "$result" =~ ^[0-9]+$ ]]; then |
46 | | - echo "$result" |
| 42 | + _CLOCK_NOW_IMPL="date" |
47 | 43 | return 0 |
48 | 44 | fi |
49 | 45 | fi |
50 | 46 |
|
51 | 47 | # 6. Try using native shell EPOCHREALTIME (if available) |
52 | 48 | attempts+=("EPOCHREALTIME") |
53 | 49 | 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" |
57 | 51 | return 0 |
58 | 52 | fi |
59 | 53 |
|
|
63 | 57 | return 1 |
64 | 58 | } |
65 | 59 |
|
| 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 | + |
66 | 104 | function clock::shell_time() { |
67 | 105 | # Get time directly from the shell variable EPOCHREALTIME (Bash 5+) |
68 | 106 | [[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME" |
|
0 commit comments