|
2 | 2 |
|
3 | 3 | function clock::now() { |
4 | 4 | local shell_time |
| 5 | + |
| 6 | + # 1. Try using native shell EPOCHREALTIME (if available) |
5 | 7 | if shell_time="$(clock::shell_time)"; then |
6 | 8 | local seconds="${shell_time%%.*}" |
7 | 9 | local microseconds="${shell_time#*.}" |
8 | | - |
9 | 10 | math::calculate "($seconds * 1000000000) + ($microseconds * 1000)" |
10 | 11 | return 0 |
11 | 12 | fi |
12 | 13 |
|
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 |
17 | 17 | fi |
18 | 18 |
|
| 19 | + # 3. Windows fallback with PowerShell |
19 | 20 | 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 |
28 | 28 | fi |
29 | 29 |
|
| 30 | + # 4. Unix fallback using `date +%s%N` (if not macOS or Alpine) |
30 | 31 | if ! check_os::is_macos && ! check_os::is_alpine; then |
31 | 32 | 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 |
34 | 35 | echo "$result" |
35 | 36 | return 0 |
36 | 37 | fi |
37 | 38 | fi |
38 | 39 |
|
| 40 | + # 5. All methods failed |
39 | 41 | echo "" |
40 | 42 | return 1 |
41 | 43 | } |
42 | 44 |
|
43 | 45 | 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+) |
45 | 47 | [[ -n ${EPOCHREALTIME+x} && -n "$EPOCHREALTIME" ]] && LC_ALL=C echo "$EPOCHREALTIME" |
46 | 48 | } |
47 | 49 |
|
48 | | - |
49 | 50 | function clock::total_runtime_in_milliseconds() { |
| 51 | + local end_time |
50 | 52 | end_time=$(clock::now) |
51 | 53 | if [[ -n $end_time ]]; then |
52 | | - math::calculate "($end_time-$_START_TIME)/1000000" |
| 54 | + math::calculate "($end_time - $_START_TIME) / 1000000" |
53 | 55 | else |
54 | 56 | echo "" |
55 | 57 | fi |
56 | 58 | } |
57 | 59 |
|
58 | 60 | function clock::total_runtime_in_nanoseconds() { |
| 61 | + local end_time |
59 | 62 | end_time=$(clock::now) |
60 | 63 | if [[ -n $end_time ]]; then |
61 | | - math::calculate "($end_time-$_START_TIME)" |
| 64 | + math::calculate "$end_time - $_START_TIME" |
62 | 65 | else |
63 | 66 | echo "" |
64 | 67 | fi |
|
0 commit comments