@@ -40,6 +40,10 @@ impl SystemTime {
4040 SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) }
4141 }
4242
43+ pub fn now ( ) -> SystemTime {
44+ SystemTime { t : Timespec :: now ( libc:: CLOCK_REALTIME ) }
45+ }
46+
4347 pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
4448 self . t . sub_timespec ( & other. t )
4549 }
@@ -79,6 +83,36 @@ impl Timespec {
7983 Timespec { tv_sec, tv_nsec : unsafe { Nanoseconds ( tv_nsec as u32 ) } }
8084 }
8185
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+
82116 pub fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
83117 if self >= other {
84118 // NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
@@ -224,7 +258,6 @@ impl From<__timespec64> for Timespec {
224258) ) ]
225259mod inner {
226260 use crate :: sync:: atomic:: { AtomicU64 , Ordering } ;
227- use crate :: sys:: cvt;
228261 use crate :: sys_common:: mul_div_u64;
229262 use crate :: time:: Duration ;
230263
@@ -268,16 +301,6 @@ mod inner {
268301 }
269302 }
270303
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-
281304 impl From < libc:: timeval > for Timespec {
282305 fn from ( t : libc:: timeval ) -> Timespec {
283306 Timespec :: new ( t. tv_sec as i64 , 1000 * t. tv_usec as i64 )
@@ -345,11 +368,9 @@ mod inner {
345368) ) ) ]
346369mod inner {
347370 use crate :: fmt;
348- use crate :: mem:: MaybeUninit ;
349- use crate :: sys:: cvt;
350371 use crate :: time:: Duration ;
351372
352- use super :: { SystemTime , Timespec } ;
373+ use super :: Timespec ;
353374
354375 #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
355376 pub struct Instant {
@@ -386,39 +407,4 @@ mod inner {
386407 . finish ( )
387408 }
388409 }
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- }
424410}
0 commit comments