11use core:: num:: niche_types:: Nanoseconds ;
22
3- use crate :: sys :: AsInner ;
3+ use crate :: io ;
44use crate :: time:: Duration ;
5- use crate :: { fmt, io} ;
65
76const NSEC_PER_SEC : u64 = 1_000_000_000 ;
8- pub const UNIX_EPOCH : SystemTime = SystemTime { t : Timespec :: zero ( ) } ;
7+
98#[ allow( dead_code) ] // Used for pthread condvar timeouts
109pub const TIMESPEC_MAX : libc:: timespec =
1110 libc:: timespec { tv_sec : <libc:: time_t >:: MAX , tv_nsec : 1_000_000_000 - 1 } ;
@@ -18,60 +17,19 @@ pub(in crate::sys) const TIMESPEC_MAX_CAPPED: libc::timespec = libc::timespec {
1817 tv_nsec : ( u64:: MAX % NSEC_PER_SEC ) as i64 ,
1918} ;
2019
21- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
22- pub struct SystemTime {
23- pub ( crate ) t : Timespec ,
24- }
25-
2620#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
2721pub ( crate ) struct Timespec {
28- tv_sec : i64 ,
29- tv_nsec : Nanoseconds ,
30- }
31-
32- impl SystemTime {
33- pub const MAX : SystemTime = SystemTime { t : Timespec :: MAX } ;
34-
35- pub const MIN : SystemTime = SystemTime { t : Timespec :: MIN } ;
36-
37- #[ cfg_attr( any( target_os = "horizon" , target_os = "hurd" ) , allow( unused) ) ]
38- pub fn new ( tv_sec : i64 , tv_nsec : i64 ) -> Result < SystemTime , io:: Error > {
39- Ok ( SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) ? } )
40- }
41-
42- pub fn now ( ) -> SystemTime {
43- SystemTime { t : Timespec :: now ( libc:: CLOCK_REALTIME ) }
44- }
45-
46- pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
47- self . t . sub_timespec ( & other. t )
48- }
49-
50- pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
51- Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
52- }
53-
54- pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < SystemTime > {
55- Some ( SystemTime { t : self . t . checked_sub_duration ( other) ? } )
56- }
57- }
58-
59- impl fmt:: Debug for SystemTime {
60- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
61- f. debug_struct ( "SystemTime" )
62- . field ( "tv_sec" , & self . t . tv_sec )
63- . field ( "tv_nsec" , & self . t . tv_nsec )
64- . finish ( )
65- }
22+ pub tv_sec : i64 ,
23+ pub tv_nsec : Nanoseconds ,
6624}
6725
6826impl Timespec {
69- const MAX : Timespec = unsafe { Self :: new_unchecked ( i64:: MAX , 1_000_000_000 - 1 ) } ;
27+ pub const MAX : Timespec = unsafe { Self :: new_unchecked ( i64:: MAX , 1_000_000_000 - 1 ) } ;
7028
7129 // As described below, on Apple OS, dates before epoch are represented differently.
7230 // This is not an issue here however, because we are using tv_sec = i64::MIN,
7331 // which will cause the compatibility wrapper to not be executed at all.
74- const MIN : Timespec = unsafe { Self :: new_unchecked ( i64:: MIN , 0 ) } ;
32+ pub const MIN : Timespec = unsafe { Self :: new_unchecked ( i64:: MIN , 0 ) } ;
7533
7634 const unsafe fn new_unchecked ( tv_sec : i64 , tv_nsec : i64 ) -> Timespec {
7735 Timespec { tv_sec, tv_nsec : unsafe { Nanoseconds :: new_unchecked ( tv_nsec as u32 ) } }
@@ -81,7 +39,7 @@ impl Timespec {
8139 unsafe { Self :: new_unchecked ( 0 , 0 ) }
8240 }
8341
84- const fn new ( tv_sec : i64 , tv_nsec : i64 ) -> Result < Timespec , io:: Error > {
42+ pub const fn new ( tv_sec : i64 , tv_nsec : i64 ) -> Result < Timespec , io:: Error > {
8543 // On Apple OS, dates before epoch are represented differently than on other
8644 // Unix platforms: e.g. 1/10th of a second before epoch is represented as `seconds=-1`
8745 // and `nanoseconds=100_000_000` on other platforms, but is `seconds=0` and
@@ -263,64 +221,3 @@ impl __timespec64 {
263221 Self { tv_sec, tv_nsec, _padding : 0 }
264222 }
265223}
266-
267- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
268- pub struct Instant {
269- t : Timespec ,
270- }
271-
272- impl Instant {
273- #[ cfg( target_vendor = "apple" ) ]
274- pub ( crate ) const CLOCK_ID : libc:: clockid_t = libc:: CLOCK_UPTIME_RAW ;
275- #[ cfg( not( target_vendor = "apple" ) ) ]
276- pub ( crate ) const CLOCK_ID : libc:: clockid_t = libc:: CLOCK_MONOTONIC ;
277- pub fn now ( ) -> Instant {
278- // https://pubs.opengroup.org/onlinepubs/9799919799/functions/clock_getres.html
279- //
280- // CLOCK_UPTIME_RAW clock that increments monotonically, in the same man-
281- // ner as CLOCK_MONOTONIC_RAW, but that does not incre-
282- // ment while the system is asleep. The returned value
283- // is identical to the result of mach_absolute_time()
284- // after the appropriate mach_timebase conversion is
285- // applied.
286- //
287- // Instant on macos was historically implemented using mach_absolute_time;
288- // we preserve this value domain out of an abundance of caution.
289- Instant { t : Timespec :: now ( Self :: CLOCK_ID ) }
290- }
291-
292- pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
293- self . t . sub_timespec ( & other. t ) . ok ( )
294- }
295-
296- pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
297- Some ( Instant { t : self . t . checked_add_duration ( other) ? } )
298- }
299-
300- pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Instant > {
301- Some ( Instant { t : self . t . checked_sub_duration ( other) ? } )
302- }
303-
304- #[ cfg_attr(
305- not( target_os = "linux" ) ,
306- allow( unused, reason = "needed by the `sleep_until` on some unix platforms" )
307- ) ]
308- pub ( crate ) fn into_timespec ( self ) -> Timespec {
309- self . t
310- }
311- }
312-
313- impl AsInner < Timespec > for Instant {
314- fn as_inner ( & self ) -> & Timespec {
315- & self . t
316- }
317- }
318-
319- impl fmt:: Debug for Instant {
320- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
321- f. debug_struct ( "Instant" )
322- . field ( "tv_sec" , & self . t . tv_sec )
323- . field ( "tv_nsec" , & self . t . tv_nsec )
324- . finish ( )
325- }
326- }
0 commit comments