diff --git a/vm/src/stdlib/select.rs b/vm/src/stdlib/select.rs index a0411ca049..47ef9ffbfb 100644 --- a/vm/src/stdlib/select.rs +++ b/vm/src/stdlib/select.rs @@ -171,7 +171,7 @@ mod decl { return Err(vm.new_value_error("timeout must be positive".to_owned())); } } - let deadline = timeout.map(|s| time::get_time(vm).unwrap() + s); + let deadline = timeout.map(|s| time::time(vm).unwrap() + s); let seq2set = |list| -> PyResult<(Vec, FdSet)> { let v = vm.extract_elements::(list)?; @@ -210,7 +210,7 @@ mod decl { vm.check_signals()?; if let Some(ref mut timeout) = timeout { - *timeout = deadline.unwrap() - time::get_time(vm).unwrap(); + *timeout = deadline.unwrap() - time::time(vm).unwrap(); if *timeout < 0.0 { r.clear(); w.clear(); diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 801c164b35..54a7409c45 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -2,7 +2,9 @@ // See also: // https://docs.python.org/3/library/time.html -use crate::{PyObjectRef, PyResult, VirtualMachine}; +use crate::{PyObjectRef, VirtualMachine}; + +pub use time::*; pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { let module = time::make_module(vm); @@ -14,32 +16,6 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef { module } -#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] -pub(crate) fn get_time(vm: &VirtualMachine) -> PyResult { - Ok(duration_since_system_now(vm)?.as_secs_f64()) -} - -#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] -pub(crate) fn get_time(_vm: &VirtualMachine) -> PyResult { - use wasm_bindgen::prelude::*; - #[wasm_bindgen] - extern "C" { - type Date; - #[wasm_bindgen(static_method_of = Date)] - fn now() -> f64; - } - // Date.now returns unix time in milliseconds, we want it in seconds - Ok(Date::now() / 1000.0) -} - -fn duration_since_system_now(vm: &VirtualMachine) -> PyResult { - use std::time::{SystemTime, UNIX_EPOCH}; - - SystemTime::now() - .duration_since(UNIX_EPOCH) - .map_err(|e| vm.new_value_error(format!("Time error: {:?}", e))) -} - #[pymodule(name = "time")] mod time { use crate::{ @@ -70,6 +46,14 @@ mod time { #[allow(dead_code)] pub(super) const NS_TO_US: i64 = 1000; + fn duration_since_system_now(vm: &VirtualMachine) -> PyResult { + use std::time::{SystemTime, UNIX_EPOCH}; + + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_err(|e| vm.new_value_error(format!("Time error: {:?}", e))) + } + #[cfg(not(unix))] #[pyfunction] fn sleep(dur: std::time::Duration) { @@ -79,19 +63,37 @@ mod time { #[cfg(not(target_os = "wasi"))] #[pyfunction] fn time_ns(vm: &VirtualMachine) -> PyResult { - Ok(super::duration_since_system_now(vm)?.as_nanos() as u64) + Ok(duration_since_system_now(vm)?.as_nanos() as u64) } #[pyfunction(name = "perf_counter")] // TODO: fix #[pyfunction] - fn time(vm: &VirtualMachine) -> PyResult { - super::get_time(vm) + pub fn time(vm: &VirtualMachine) -> PyResult { + _time(vm) + } + + #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] + fn _time(vm: &VirtualMachine) -> PyResult { + Ok(duration_since_system_now(vm)?.as_secs_f64()) + } + + #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] + fn _time(_vm: &VirtualMachine) -> PyResult { + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + extern "C" { + type Date; + #[wasm_bindgen(static_method_of = Date)] + fn now() -> f64; + } + // Date.now returns unix time in milliseconds, we want it in seconds + Ok(Date::now() / 1000.0) } #[pyfunction] fn monotonic(vm: &VirtualMachine) -> PyResult { // TODO: implement proper monotonic time! - Ok(super::duration_since_system_now(vm)?.as_secs_f64()) + Ok(duration_since_system_now(vm)?.as_secs_f64()) } fn pyobj_to_naive_date_time(