@@ -40,6 +40,10 @@ impl SystemTime {
40
40
SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) }
41
41
}
42
42
43
+ pub fn now ( ) -> SystemTime {
44
+ SystemTime { t : Timespec :: now ( libc:: CLOCK_REALTIME ) }
45
+ }
46
+
43
47
pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
44
48
self . t . sub_timespec ( & other. t )
45
49
}
@@ -79,6 +83,36 @@ impl Timespec {
79
83
Timespec { tv_sec, tv_nsec : unsafe { Nanoseconds ( tv_nsec as u32 ) } }
80
84
}
81
85
86
+ pub fn now ( clock : libc:: clockid_t ) -> Timespec {
87
+ use crate :: mem:: MaybeUninit ;
88
+ use crate :: sys:: cvt;
89
+
90
+ // Try to use 64-bit time in preparation for Y2038.
91
+ #[ cfg( all(
92
+ target_os = "linux" ,
93
+ target_env = "gnu" ,
94
+ target_pointer_width = "32" ,
95
+ not( target_arch = "riscv32" )
96
+ ) ) ]
97
+ {
98
+ use crate :: sys:: weak:: weak;
99
+
100
+ // __clock_gettime64 was added to 32-bit arches in glibc 2.34,
101
+ // and it handles both vDSO calls and ENOSYS fallbacks itself.
102
+ weak ! ( fn __clock_gettime64( libc:: clockid_t, * mut super :: __timespec64) -> libc:: c_int) ;
103
+
104
+ if let Some ( clock_gettime64) = __clock_gettime64. get ( ) {
105
+ let mut t = MaybeUninit :: uninit ( ) ;
106
+ cvt ( unsafe { clock_gettime64 ( clock, t. as_mut_ptr ( ) ) } ) . unwrap ( ) ;
107
+ return Timespec :: from ( unsafe { t. assume_init ( ) } ) ;
108
+ }
109
+ }
110
+
111
+ let mut t = MaybeUninit :: uninit ( ) ;
112
+ cvt ( unsafe { libc:: clock_gettime ( clock, t. as_mut_ptr ( ) ) } ) . unwrap ( ) ;
113
+ Timespec :: from ( unsafe { t. assume_init ( ) } )
114
+ }
115
+
82
116
pub fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
83
117
if self >= other {
84
118
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
@@ -224,7 +258,6 @@ impl From<__timespec64> for Timespec {
224
258
) ) ]
225
259
mod inner {
226
260
use crate :: sync:: atomic:: { AtomicU64 , Ordering } ;
227
- use crate :: sys:: cvt;
228
261
use crate :: sys_common:: mul_div_u64;
229
262
use crate :: time:: Duration ;
230
263
@@ -268,16 +301,6 @@ mod inner {
268
301
}
269
302
}
270
303
271
- impl SystemTime {
272
- pub fn now ( ) -> SystemTime {
273
- use crate :: ptr;
274
-
275
- let mut s = libc:: timeval { tv_sec : 0 , tv_usec : 0 } ;
276
- cvt ( unsafe { libc:: gettimeofday ( & mut s, ptr:: null_mut ( ) ) } ) . unwrap ( ) ;
277
- return SystemTime :: from ( s) ;
278
- }
279
- }
280
-
281
304
impl From < libc:: timeval > for Timespec {
282
305
fn from ( t : libc:: timeval ) -> Timespec {
283
306
Timespec :: new ( t. tv_sec as i64 , 1000 * t. tv_usec as i64 )
@@ -345,11 +368,9 @@ mod inner {
345
368
) ) ) ]
346
369
mod inner {
347
370
use crate :: fmt;
348
- use crate :: mem:: MaybeUninit ;
349
- use crate :: sys:: cvt;
350
371
use crate :: time:: Duration ;
351
372
352
- use super :: { SystemTime , Timespec } ;
373
+ use super :: Timespec ;
353
374
354
375
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
355
376
pub struct Instant {
@@ -386,39 +407,4 @@ mod inner {
386
407
. finish ( )
387
408
}
388
409
}
389
-
390
- impl SystemTime {
391
- pub fn now ( ) -> SystemTime {
392
- SystemTime { t : Timespec :: now ( libc:: CLOCK_REALTIME ) }
393
- }
394
- }
395
-
396
- impl Timespec {
397
- pub fn now ( clock : libc:: clockid_t ) -> Timespec {
398
- // Try to use 64-bit time in preparation for Y2038.
399
- #[ cfg( all(
400
- target_os = "linux" ,
401
- target_env = "gnu" ,
402
- target_pointer_width = "32" ,
403
- not( target_arch = "riscv32" )
404
- ) ) ]
405
- {
406
- use crate :: sys:: weak:: weak;
407
-
408
- // __clock_gettime64 was added to 32-bit arches in glibc 2.34,
409
- // and it handles both vDSO calls and ENOSYS fallbacks itself.
410
- weak ! ( fn __clock_gettime64( libc:: clockid_t, * mut super :: __timespec64) -> libc:: c_int) ;
411
-
412
- if let Some ( clock_gettime64) = __clock_gettime64. get ( ) {
413
- let mut t = MaybeUninit :: uninit ( ) ;
414
- cvt ( unsafe { clock_gettime64 ( clock, t. as_mut_ptr ( ) ) } ) . unwrap ( ) ;
415
- return Timespec :: from ( unsafe { t. assume_init ( ) } ) ;
416
- }
417
- }
418
-
419
- let mut t = MaybeUninit :: uninit ( ) ;
420
- cvt ( unsafe { libc:: clock_gettime ( clock, t. as_mut_ptr ( ) ) } ) . unwrap ( ) ;
421
- Timespec :: from ( unsafe { t. assume_init ( ) } )
422
- }
423
- }
424
410
}
0 commit comments