expose time::time as pub

This commit is contained in:
Jeong YunWon
2021-09-30 03:42:44 +09:00
parent 7fd2f0b5e7
commit 6da45da5e7
2 changed files with 35 additions and 33 deletions

View File

@@ -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<Selectable>, FdSet)> {
let v = vm.extract_elements::<Selectable>(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();

View File

@@ -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<f64> {
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<f64> {
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<std::time::Duration> {
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<std::time::Duration> {
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<u64> {
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<f64> {
super::get_time(vm)
pub fn time(vm: &VirtualMachine) -> PyResult<f64> {
_time(vm)
}
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
fn _time(vm: &VirtualMachine) -> PyResult<f64> {
Ok(duration_since_system_now(vm)?.as_secs_f64())
}
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
fn _time(_vm: &VirtualMachine) -> PyResult<f64> {
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<f64> {
// 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(