Skip to content

Commit 30dbcd7

Browse files
committed
chore: support python and nodejs in clock::now()
1 parent 3a6d35d commit 30dbcd7

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

src/clock.sh

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
function clock::now() {
44
local shell_time
5+
local attempts=()
56

67
# 1. Try using native shell EPOCHREALTIME (if available)
8+
attempts+=("EPOCHREALTIME")
79
if shell_time="$(clock::shell_time)"; then
810
local seconds="${shell_time%%.*}"
911
local microseconds="${shell_time#*.}"
@@ -12,11 +14,28 @@ function clock::now() {
1214
fi
1315

1416
# 2. Try Perl with Time::HiRes
17+
attempts+=("Perl")
1518
if dependencies::has_perl && perl -MTime::HiRes -e "" &>/dev/null; then
1619
perl -MTime::HiRes -e 'printf("%.0f\n", Time::HiRes::time() * 1000000000)' && return 0
1720
fi
1821

19-
# 3. Windows fallback with PowerShell
22+
# 3. Try Python 3 with time module
23+
attempts+=("Python")
24+
if dependencies::has_python; then
25+
python - <<'EOF'
26+
import time, sys
27+
sys.stdout.write(str(int(time.time() * 1_000_000_000)))
28+
EOF
29+
return 0
30+
fi
31+
32+
# 4. Try Node.js
33+
attempts+=("Node")
34+
if dependencies::has_node; then
35+
node -e 'process.stdout.write((BigInt(Date.now()) * 1000000n).toString())' && return 0
36+
fi
37+
# 5. Windows fallback with PowerShell
38+
attempts+=("PowerShell")
2039
if check_os::is_windows && dependencies::has_powershell; then
2140
powershell -Command "
2241
\$unixEpoch = [DateTime]'1970-01-01 00:00:00';
@@ -27,7 +46,8 @@ function clock::now() {
2746
" && return 0
2847
fi
2948

30-
# 4. Unix fallback using `date +%s%N` (if not macOS or Alpine)
49+
# 6. Unix fallback using `date +%s%N` (if not macOS or Alpine)
50+
attempts+=("date")
3151
if ! check_os::is_macos && ! check_os::is_alpine; then
3252
local result
3353
result=$(date +%s%N 2>/dev/null)
@@ -37,7 +57,8 @@ function clock::now() {
3757
fi
3858
fi
3959

40-
# 5. All methods failed
60+
# 7. All methods failed
61+
printf "clock::now implementations tried: %s\n" "${attempts[*]}" >&2
4162
echo ""
4263
return 1
4364
}

src/dependencies.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ function dependencies::has_curl() {
3232
function dependencies::has_wget() {
3333
command -v wget >/dev/null 2>&1
3434
}
35+
36+
function dependencies::has_python() {
37+
command -v python >/dev/null 2>&1
38+
}
39+
40+
function dependencies::has_node() {
41+
command -v node >/dev/null 2>&1
42+
}

tests/unit/clock_test.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function mock_non_existing_fn() {
1717
function test_now_with_perl() {
1818
mock clock::shell_time mock_non_existing_fn
1919
mock perl echo "1720705883457"
20+
mock dependencies::has_python mock_false
21+
mock dependencies::has_node mock_false
2022

2123
assert_same "1720705883457" "$(clock::now)"
2224
}
@@ -26,6 +28,8 @@ function test_now_on_linux_unknown() {
2628
mock clock::shell_time mock_non_existing_fn
2729
mock perl mock_non_existing_fn
2830
mock date echo "1720705883457"
31+
mock dependencies::has_python mock_false
32+
mock dependencies::has_node mock_false
2933

3034
assert_same "1720705883457" "$(clock::now)"
3135
}
@@ -34,6 +38,8 @@ function test_now_on_linux_alpine() {
3438
mock_alpine_os
3539
mock clock::shell_time mock_non_existing_fn
3640
mock perl echo "1720705883457"
41+
mock dependencies::has_python mock_false
42+
mock dependencies::has_node mock_false
3743

3844
assert_same "1720705883457" "$(clock::now)"
3945
}
@@ -44,6 +50,8 @@ function test_now_on_windows_without_with_powershell() {
4450
mock dependencies::has_powershell mock_true
4551
mock powershell echo "1727768183281580800"
4652
mock clock::shell_time mock_non_existing_fn
53+
mock dependencies::has_python mock_false
54+
mock dependencies::has_node mock_false
4755

4856
assert_same "1727768183281580800" "$(clock::now)"
4957
}
@@ -54,6 +62,8 @@ function test_now_on_windows_without_without_powershell() {
5462
mock dependencies::has_powershell mock_false
5563
mock date echo "1727768951"
5664
mock clock::shell_time mock_non_existing_fn
65+
mock dependencies::has_python mock_false
66+
mock dependencies::has_node mock_false
5767

5868
assert_same "1727768951" "$(clock::now)"
5969
}
@@ -67,12 +77,16 @@ function test_now_on_osx_without_perl() {
6777
mock_macos
6878
mock dependencies::has_perl mock_false
6979
mock clock::shell_time echo "1727708708.326957"
80+
mock dependencies::has_python mock_false
81+
mock dependencies::has_node mock_false
7082

7183
assert_same "1727708708326957000" "$(clock::now)"
7284
}
7385

7486
function test_runtime_in_milliseconds_when_not_empty_time() {
7587
mock perl echo "1720705883457"
88+
mock dependencies::has_python mock_false
89+
mock dependencies::has_node mock_false
7690

7791
assert_not_empty "$(clock::total_runtime_in_milliseconds)"
7892
}
@@ -81,6 +95,8 @@ function test_runtime_in_milliseconds_when_empty_time() {
8195
mock_macos
8296
mock perl mock_non_existing_fn
8397
mock clock::shell_time mock_non_existing_fn
98+
mock dependencies::has_python mock_false
99+
mock dependencies::has_node mock_false
84100

85101
assert_empty "$(clock::total_runtime_in_milliseconds)"
86102
}

tests/unit/dependencies_test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,17 @@ function test_has_git() {
3535

3636
assert_have_been_called_with "-v git" command
3737
}
38+
39+
function test_has_python() {
40+
spy command
41+
dependencies::has_python
42+
43+
assert_have_been_called_with "-v python" command
44+
}
45+
46+
function test_has_node() {
47+
spy command
48+
dependencies::has_node
49+
50+
assert_have_been_called_with "-v node" command
51+
}

0 commit comments

Comments
 (0)