Skip to content

Commit ee77f3c

Browse files
committed
Add monotonic_ns, perf_counter, perf_counter_ns.
1 parent 1817fd6 commit ee77f3c

File tree

2 files changed

+95
-5
lines changed

2 files changed

+95
-5
lines changed

Lib/test/test_time.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ def test_time(self):
6666
self.assertFalse(info.monotonic)
6767
self.assertTrue(info.adjustable)
6868

69-
# TODO: RUSTPYTHON, AttributeError: module 'time' has no attribute 'monotonic_ns'
70-
@unittest.expectedFailure
7169
def test_time_ns_type(self):
7270
def check_ns(sec, ns):
7371
self.assertIsInstance(ns, int)
@@ -533,8 +531,6 @@ def test_thread_time(self):
533531
self.assertTrue(info.monotonic)
534532
self.assertFalse(info.adjustable)
535533

536-
# TODO: RUSTPYTHON fix time.monotonic, currently calls System::now (i.e CLOCK_REALTIME)
537-
@unittest.expectedFailure
538534
@unittest.skipUnless(hasattr(time, 'clock_settime'),
539535
'need time.clock_settime')
540536
def test_monotonic_settime(self):

vm/src/stdlib/time.rs

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,58 @@ mod time {
9090
Ok(Date::now() / 1000.0)
9191
}
9292

93+
#[cfg(not(any(
94+
target_os = "macos",
95+
target_os = "android",
96+
target_os = "dragonfly",
97+
target_os = "freebsd",
98+
target_os = "linux",
99+
)))]
93100
#[pyfunction]
94101
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
95-
// TODO: implement proper monotonic time!
102+
// TODO: implement proper monotonic time for other platforms.
103+
Ok(duration_since_system_now(vm)?.as_secs_f64())
104+
}
105+
106+
#[cfg(not(any(
107+
target_os = "macos",
108+
target_os = "android",
109+
target_os = "dragonfly",
110+
target_os = "freebsd",
111+
target_os = "linux",
112+
)))]
113+
#[pyfunction]
114+
fn monotonic_ns(vm: &VirtualMachine) -> PyResult<u128> {
115+
// TODO: implement proper monotonic time for other platforms.
116+
Ok(duration_since_system_now(vm)?.as_nanos())
117+
}
118+
119+
#[cfg(not(any(
120+
target_os = "macos",
121+
target_os = "android",
122+
target_os = "dragonfly",
123+
target_os = "freebsd",
124+
target_os = "linux",
125+
)))]
126+
#[pyfunction]
127+
fn perf_counter(vm: &VirtualMachine) -> PyResult<f64> {
128+
// TODO: implement proper monotonic time for other platforms.
96129
Ok(duration_since_system_now(vm)?.as_secs_f64())
97130
}
98131

132+
#[cfg(not(any(
133+
target_os = "macos",
134+
target_os = "android",
135+
target_os = "dragonfly",
136+
target_os = "freebsd",
137+
target_os = "linux",
138+
)))]
139+
#[pyfunction]
140+
fn perf_counter_ns(vm: &VirtualMachine) -> PyResult<u128> {
141+
// TODO: implement proper monotonic time for other platforms.
142+
Ok(duration_since_system_now(vm)?.as_nanos())
143+
}
144+
99145
fn pyobj_to_naive_date_time(
100146
value: Either<f64, i64>,
101147
vm: &VirtualMachine,
@@ -581,6 +627,54 @@ mod unix {
581627
Err(vm.new_not_implemented_error("get_clock_info unsupported on this system".to_owned()))
582628
}
583629

630+
#[cfg(any(
631+
target_os = "macos",
632+
target_os = "android",
633+
target_os = "dragonfly",
634+
target_os = "freebsd",
635+
target_os = "linux",
636+
))]
637+
#[pyfunction]
638+
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
639+
clock_gettime(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
640+
}
641+
642+
#[cfg(any(
643+
target_os = "macos",
644+
target_os = "android",
645+
target_os = "dragonfly",
646+
target_os = "freebsd",
647+
target_os = "linux",
648+
))]
649+
#[pyfunction]
650+
fn monotonic_ns(vm: &VirtualMachine) -> PyResult<u128> {
651+
clock_gettime_ns(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
652+
}
653+
654+
#[cfg(any(
655+
target_os = "macos",
656+
target_os = "android",
657+
target_os = "dragonfly",
658+
target_os = "freebsd",
659+
target_os = "linux",
660+
))]
661+
#[pyfunction]
662+
fn perf_counter(vm: &VirtualMachine) -> PyResult<f64> {
663+
clock_gettime(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
664+
}
665+
666+
#[cfg(any(
667+
target_os = "macos",
668+
target_os = "android",
669+
target_os = "dragonfly",
670+
target_os = "freebsd",
671+
target_os = "linux",
672+
))]
673+
#[pyfunction]
674+
fn perf_counter_ns(vm: &VirtualMachine) -> PyResult<u128> {
675+
clock_gettime_ns(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
676+
}
677+
584678
#[pyfunction]
585679
fn sleep(dur: Duration, vm: &VirtualMachine) -> PyResult<()> {
586680
// this is basically std::thread::sleep, but that catches interrupts and we don't want to;

0 commit comments

Comments
 (0)