@@ -20,12 +20,11 @@ use crate::vm::VirtualMachine;
20
20
#[ cfg( unix) ]
21
21
fn time_sleep ( seconds : f64 , vm : & VirtualMachine ) -> PyResult < ( ) > {
22
22
// this is basically std::thread::sleep, but that catches interrupts and we don't want to
23
- let secs = seconds. trunc ( ) as u64 ;
24
- let nsecs = ( seconds. fract ( ) * 1e9 ) as i64 ;
23
+ let dur = Duration :: from_secs_f64 ( seconds) ;
25
24
26
25
let mut ts = libc:: timespec {
27
- tv_sec : std:: cmp:: min ( libc:: time_t:: max_value ( ) as u64 , secs ) as libc:: time_t ,
28
- tv_nsec : nsecs ,
26
+ tv_sec : std:: cmp:: min ( libc:: time_t:: max_value ( ) as u64 , dur . as_secs ( ) ) as libc:: time_t ,
27
+ tv_nsec : dur . subsec_nanos ( ) as i64 ,
29
28
} ;
30
29
let res = unsafe { libc:: nanosleep ( & ts, & mut ts) } ;
31
30
let interrupted = res == -1 && nix:: errno:: errno ( ) == libc:: EINTR ;
@@ -39,20 +38,13 @@ fn time_sleep(seconds: f64, vm: &VirtualMachine) -> PyResult<()> {
39
38
40
39
#[ cfg( not( unix) ) ]
41
40
fn time_sleep ( seconds : f64 , _vm : & VirtualMachine ) {
42
- let secs: u64 = seconds. trunc ( ) as u64 ;
43
- let nanos: u32 = ( seconds. fract ( ) * 1e9 ) as u32 ;
44
- let duration = Duration :: new ( secs, nanos) ;
45
- std:: thread:: sleep ( duration) ;
46
- }
47
-
48
- fn duration_to_f64 ( d : Duration ) -> f64 {
49
- ( d. as_secs ( ) as f64 ) + ( f64:: from ( d. subsec_nanos ( ) ) / 1e9 )
41
+ std:: thread:: sleep ( Duration :: from_secs_f64 ( seconds) ) ;
50
42
}
51
43
52
44
#[ cfg( any( not( target_arch = "wasm32" ) , target_os = "wasi" ) ) ]
53
45
pub fn get_time ( ) -> f64 {
54
46
match SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) {
55
- Ok ( v) => duration_to_f64 ( v ) ,
47
+ Ok ( v) => v . as_secs_f64 ( ) ,
56
48
Err ( err) => panic ! ( "Time error: {:?}" , err) ,
57
49
}
58
50
}
@@ -77,22 +69,17 @@ fn time_time(_vm: &VirtualMachine) -> f64 {
77
69
fn time_monotonic ( _vm : & VirtualMachine ) -> f64 {
78
70
// TODO: implement proper monotonic time!
79
71
match SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) {
80
- Ok ( v) => duration_to_f64 ( v ) ,
72
+ Ok ( v) => v . as_secs_f64 ( ) ,
81
73
Err ( err) => panic ! ( "Time error: {:?}" , err) ,
82
74
}
83
75
}
84
76
85
- fn pyfloat_to_secs_and_nanos ( seconds : f64 ) -> ( i64 , u32 ) {
86
- let secs: i64 = seconds. trunc ( ) as i64 ;
87
- let nanos: u32 = ( seconds. fract ( ) * 1e9 ) as u32 ;
88
- ( secs, nanos)
89
- }
90
-
91
77
fn pyobj_to_naive_date_time ( value : Either < f64 , i64 > ) -> NaiveDateTime {
92
78
match value {
93
79
Either :: A ( float) => {
94
- let ( seconds, nanos) = pyfloat_to_secs_and_nanos ( float) ;
95
- NaiveDateTime :: from_timestamp ( seconds, nanos)
80
+ let secs = float. trunc ( ) as i64 ;
81
+ let nsecs = ( float. fract ( ) * 1e9 ) as u32 ;
82
+ NaiveDateTime :: from_timestamp ( secs, nsecs)
96
83
}
97
84
Either :: B ( int) => NaiveDateTime :: from_timestamp ( int, 0 ) ,
98
85
}
0 commit comments