|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
3 | 3 | function clock::now() {
|
4 |
| - if dependencies::has_perl && perl -MTime::HiRes -e "" > /dev/null 2>&1; then |
5 |
| - if perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000000000)'; then |
6 |
| - return 0 |
7 |
| - fi |
| 4 | + local shell_time |
| 5 | + |
| 6 | + # 1. Try using native shell EPOCHREALTIME (if available) |
| 7 | + if shell_time="$(clock::shell_time)"; then |
| 8 | + local seconds="${shell_time%%.*}" |
| 9 | + local microseconds="${shell_time#*.}" |
| 10 | + math::calculate "($seconds * 1000000000) + ($microseconds * 1000)" |
| 11 | + return 0 |
8 | 12 | fi
|
9 | 13 |
|
| 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 |
| 17 | + fi |
| 18 | + |
| 19 | + # 3. Windows fallback with PowerShell |
10 | 20 | if check_os::is_windows && dependencies::has_powershell; then
|
11 |
| - powershell -Command " |
12 |
| - \$unixEpoch = [DateTime]'1970-01-01 00:00:00'; |
13 |
| - \$now = [DateTime]::UtcNow; |
14 |
| - \$ticksSinceEpoch = (\$now - \$unixEpoch).Ticks; |
15 |
| - \$nanosecondsSinceEpoch = \$ticksSinceEpoch * 100; |
16 |
| - Write-Output \$nanosecondsSinceEpoch |
17 |
| - " |
18 |
| - 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 |
19 | 28 | fi
|
20 | 29 |
|
| 30 | + # 4. Unix fallback using `date +%s%N` (if not macOS or Alpine) |
21 | 31 | if ! check_os::is_macos && ! check_os::is_alpine; then
|
22 | 32 | local result
|
23 |
| - result=$(date +%s%N) |
24 |
| - if [[ "$result" != *N ]] && [[ "$result" -gt 0 ]]; then |
| 33 | + result=$(date +%s%N 2>/dev/null) |
| 34 | + if [[ "$result" != *N && "$result" =~ ^[0-9]+$ ]]; then |
25 | 35 | echo "$result"
|
26 | 36 | return 0
|
27 | 37 | fi
|
28 | 38 | fi
|
29 | 39 |
|
30 |
| - local shell_time has_shell_time |
31 |
| - shell_time="$(clock::shell_time)" |
32 |
| - has_shell_time="$?" |
33 |
| - if [[ "$has_shell_time" -eq 0 ]]; then |
34 |
| - local seconds microseconds |
35 |
| - seconds=$(echo "$shell_time" | cut -f 1 -d '.') |
36 |
| - microseconds=$(echo "$shell_time" | cut -f 2 -d '.') |
37 |
| - |
38 |
| - math::calculate "($seconds * 1000000000) + ($microseconds * 1000)" |
39 |
| - return 0 |
40 |
| - fi |
41 |
| - |
| 40 | + # 5. All methods failed |
42 | 41 | echo ""
|
43 | 42 | return 1
|
44 | 43 | }
|
45 | 44 |
|
46 | 45 | function clock::shell_time() {
|
47 |
| - # Get time directly from the shell rather than a program. |
| 46 | + # Get time directly from the shell variable EPOCHREALTIME (Bash 5+) |
48 | 47 | [[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME"
|
49 | 48 | }
|
50 | 49 |
|
51 |
| - |
52 | 50 | function clock::total_runtime_in_milliseconds() {
|
| 51 | + local end_time |
53 | 52 | end_time=$(clock::now)
|
54 | 53 | if [[ -n $end_time ]]; then
|
55 |
| - math::calculate "($end_time-$_START_TIME)/1000000" |
| 54 | + math::calculate "($end_time - $_START_TIME) / 1000000" |
56 | 55 | else
|
57 | 56 | echo ""
|
58 | 57 | fi
|
59 | 58 | }
|
60 | 59 |
|
61 | 60 | function clock::total_runtime_in_nanoseconds() {
|
| 61 | + local end_time |
62 | 62 | end_time=$(clock::now)
|
63 | 63 | if [[ -n $end_time ]]; then
|
64 |
| - math::calculate "($end_time-$_START_TIME)" |
| 64 | + math::calculate "$end_time - $_START_TIME" |
65 | 65 | else
|
66 | 66 | echo ""
|
67 | 67 | fi
|
|
0 commit comments