Skip to content

Commit ca20acf

Browse files
committed
Change time module to use primitives directly
1 parent 5cde75b commit ca20acf

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed

vm/src/stdlib/time_module.rs

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
77

88
use chrono::naive::NaiveDateTime;
99
use chrono::{Datelike, Timelike};
10-
use num_traits::cast::ToPrimitive;
1110

1211
use crate::function::OptionalArg;
13-
use crate::obj::objfloat::{self, PyFloatRef};
14-
use crate::obj::objint::{self, PyInt};
12+
use crate::obj::objint::PyInt;
1513
use crate::obj::objsequence::{get_sequence_index, PySliceableSequence};
1614
use crate::obj::objslice::PySlice;
1715
use crate::obj::objstr::PyStringRef;
18-
use crate::obj::objtype::{self, PyClassRef};
19-
use crate::pyobject::{PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
16+
use crate::obj::objtype::PyClassRef;
17+
use crate::pyobject::{Either, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol};
2018
use crate::vm::VirtualMachine;
2119

2220
#[cfg(unix)]
23-
fn time_sleep(seconds: PyFloatRef, vm: &VirtualMachine) -> PyResult<()> {
21+
fn time_sleep(seconds: f64, vm: &VirtualMachine) -> PyResult<()> {
2422
// this is basically std::thread::sleep, but that catches interrupts and we don't want to
25-
let seconds = seconds.to_f64();
2623
let secs = seconds.trunc() as u64;
2724
let nsecs = (seconds.fract() * 1e9) as i64;
2825

@@ -41,8 +38,7 @@ fn time_sleep(seconds: PyFloatRef, vm: &VirtualMachine) -> PyResult<()> {
4138
}
4239

4340
#[cfg(not(unix))]
44-
fn time_sleep(seconds: PyFloatRef, _vm: &VirtualMachine) {
45-
let seconds = seconds.to_f64();
41+
fn time_sleep(seconds: f64, _vm: &VirtualMachine) {
4642
let secs: u64 = seconds.trunc() as u64;
4743
let nanos: u32 = (seconds.fract() * 1e9) as u32;
4844
let duration = Duration::new(secs, nanos);
@@ -82,47 +78,41 @@ fn time_monotonic(_vm: &VirtualMachine) -> f64 {
8278
}
8379
}
8480

85-
fn pyfloat_to_secs_and_nanos(seconds: &PyObjectRef) -> (i64, u32) {
86-
let seconds = objfloat::get_value(seconds);
81+
fn pyfloat_to_secs_and_nanos(seconds: f64) -> (i64, u32) {
8782
let secs: i64 = seconds.trunc() as i64;
8883
let nanos: u32 = (seconds.fract() * 1e9) as u32;
8984
(secs, nanos)
9085
}
9186

92-
fn pyobj_to_naive_date_time(
93-
value: &PyObjectRef,
94-
vm: &VirtualMachine,
95-
) -> PyResult<Option<NaiveDateTime>> {
96-
if objtype::isinstance(value, &vm.ctx.float_type()) {
97-
let (seconds, nanos) = pyfloat_to_secs_and_nanos(&value);
98-
let dt = NaiveDateTime::from_timestamp(seconds, nanos);
99-
Ok(Some(dt))
100-
} else if objtype::isinstance(&value, &vm.ctx.int_type()) {
101-
let seconds = objint::get_value(&value).to_i64().unwrap();
102-
let dt = NaiveDateTime::from_timestamp(seconds, 0);
103-
Ok(Some(dt))
104-
} else {
105-
Err(vm.new_type_error("Expected float, int or None".to_string()))
87+
fn pyobj_to_naive_date_time(value: Either<f64, i64>) -> NaiveDateTime {
88+
match value {
89+
Either::A(float) => {
90+
let (seconds, nanos) = pyfloat_to_secs_and_nanos(float);
91+
NaiveDateTime::from_timestamp(seconds, nanos)
92+
}
93+
Either::B(int) => NaiveDateTime::from_timestamp(int, 0),
10694
}
10795
}
10896

10997
/// https://docs.python.org/3/library/time.html?highlight=gmtime#time.gmtime
110-
fn time_gmtime(secs: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<PyStructTime> {
98+
fn time_gmtime(
99+
secs: OptionalArg<Either<f64, i64>>,
100+
_vm: &VirtualMachine,
101+
) -> PyResult<PyStructTime> {
111102
let default = chrono::offset::Utc::now().naive_utc();
112103
let instant = match secs {
113-
OptionalArg::Present(secs) => pyobj_to_naive_date_time(&secs, vm)?.unwrap_or(default),
104+
OptionalArg::Present(secs) => pyobj_to_naive_date_time(secs),
114105
OptionalArg::Missing => default,
115106
};
116107
let value = PyStructTime::new(instant, 0);
117108
Ok(value)
118109
}
119110

120-
fn time_localtime(secs: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<PyStructTime> {
121-
let instant = optional_or_localtime(secs, vm)?;
111+
fn time_localtime(secs: OptionalArg<Either<f64, i64>>, _vm: &VirtualMachine) -> PyStructTime {
112+
let instant = optional_or_localtime(secs);
122113
// TODO: isdst flag must be valid value here
123114
// https://docs.python.org/3/library/time.html#time.localtime
124-
let value = PyStructTime::new(instant, -1);
125-
Ok(value)
115+
PyStructTime::new(instant, -1)
126116
}
127117

128118
fn time_mktime(t: PyStructTimeRef, vm: &VirtualMachine) -> PyResult {
@@ -132,16 +122,12 @@ fn time_mktime(t: PyStructTimeRef, vm: &VirtualMachine) -> PyResult {
132122
}
133123

134124
/// Construct a localtime from the optional seconds, or get the current local time.
135-
fn optional_or_localtime(
136-
secs: OptionalArg<PyObjectRef>,
137-
vm: &VirtualMachine,
138-
) -> PyResult<NaiveDateTime> {
125+
fn optional_or_localtime(secs: OptionalArg<Either<f64, i64>>) -> NaiveDateTime {
139126
let default = chrono::offset::Local::now().naive_local();
140-
let instant = match secs {
141-
OptionalArg::Present(secs) => pyobj_to_naive_date_time(&secs, vm)?.unwrap_or(default),
127+
match secs {
128+
OptionalArg::Present(secs) => pyobj_to_naive_date_time(secs),
142129
OptionalArg::Missing => default,
143-
};
144-
Ok(instant)
130+
}
145131
}
146132

147133
const CFMT: &str = "%a %b %e %H:%M:%S %Y";
@@ -156,10 +142,9 @@ fn time_asctime(t: OptionalArg<PyStructTimeRef>, vm: &VirtualMachine) -> PyResul
156142
Ok(vm.ctx.new_str(formatted_time))
157143
}
158144

159-
fn time_ctime(secs: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<String> {
160-
let instant = optional_or_localtime(secs, vm)?;
161-
let formatted_time = instant.format(&CFMT).to_string();
162-
Ok(formatted_time)
145+
fn time_ctime(secs: OptionalArg<Either<f64, i64>>, _vm: &VirtualMachine) -> String {
146+
let instant = optional_or_localtime(secs);
147+
instant.format(&CFMT).to_string()
163148
}
164149

165150
fn time_strftime(

0 commit comments

Comments
 (0)