1+ use crate :: fmt;
12use crate :: time:: Duration ;
23
3- pub use self :: inner:: { Instant , SystemTime , UNIX_EPOCH } ;
4+ pub use self :: inner:: Instant ;
45use crate :: convert:: TryInto ;
56
67const NSEC_PER_SEC : u64 = 1_000_000_000 ;
8+ pub const UNIX_EPOCH : SystemTime = SystemTime { t : Timespec :: zero ( ) } ;
9+
10+ #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
11+ pub struct SystemTime {
12+ pub ( in crate :: sys:: unix) t : Timespec ,
13+ }
714
815#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
916pub ( in crate :: sys:: unix) struct Timespec {
1017 tv_sec : i64 ,
1118 tv_nsec : i64 ,
1219}
1320
21+ impl SystemTime {
22+ pub fn new ( tv_sec : i64 , tv_nsec : i64 ) -> SystemTime {
23+ SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) }
24+ }
25+
26+ pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
27+ self . t . sub_timespec ( & other. t )
28+ }
29+
30+ pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
31+ Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
32+ }
33+
34+ pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < SystemTime > {
35+ Some ( SystemTime { t : self . t . checked_sub_duration ( other) ? } )
36+ }
37+ }
38+
39+ impl From < libc:: timespec > for SystemTime {
40+ fn from ( t : libc:: timespec ) -> SystemTime {
41+ SystemTime { t : Timespec :: from ( t) }
42+ }
43+ }
44+
45+ impl fmt:: Debug for SystemTime {
46+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
47+ f. debug_struct ( "SystemTime" )
48+ . field ( "tv_sec" , & self . t . tv_sec )
49+ . field ( "tv_nsec" , & self . t . tv_nsec )
50+ . finish ( )
51+ }
52+ }
53+
1454impl Timespec {
1555 const fn zero ( ) -> Timespec {
1656 Timespec { tv_sec : 0 , tv_nsec : 0 }
@@ -85,31 +125,36 @@ impl Timespec {
85125 }
86126 Some ( Timespec :: new ( secs, nsec as i64 ) )
87127 }
128+
129+ pub fn to_timespec ( & self ) -> Option < libc:: timespec > {
130+ use crate :: convert:: TryInto ;
131+ Some ( libc:: timespec {
132+ tv_sec : self . tv_sec . try_into ( ) . ok ( ) ?,
133+ tv_nsec : self . tv_nsec . try_into ( ) . ok ( ) ?,
134+ } )
135+ }
136+ }
137+
138+ impl From < libc:: timespec > for Timespec {
139+ fn from ( t : libc:: timespec ) -> Timespec {
140+ Timespec :: new ( t. tv_sec as i64 , t. tv_nsec as i64 )
141+ }
88142}
89143
90144#[ cfg( any( target_os = "macos" , target_os = "ios" ) ) ]
91145mod inner {
92- use crate :: fmt;
93146 use crate :: sync:: atomic:: { AtomicU64 , Ordering } ;
94147 use crate :: sys:: cvt;
95148 use crate :: sys_common:: mul_div_u64;
96149 use crate :: time:: Duration ;
97150
98- use super :: Timespec ;
99- use super :: NSEC_PER_SEC ;
151+ use super :: { SystemTime , Timespec , NSEC_PER_SEC } ;
100152
101153 #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Debug , Hash ) ]
102154 pub struct Instant {
103155 t : u64 ,
104156 }
105157
106- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
107- pub struct SystemTime {
108- pub ( in crate :: sys:: unix) t : Timespec ,
109- }
110-
111- pub const UNIX_EPOCH : SystemTime = SystemTime { t : Timespec :: zero ( ) } ;
112-
113158 #[ repr( C ) ]
114159 #[ derive( Copy , Clone ) ]
115160 struct mach_timebase_info {
@@ -144,29 +189,13 @@ mod inner {
144189 }
145190
146191 impl SystemTime {
147- pub fn new ( tv_sec : i64 , tv_nsec : i64 ) -> SystemTime {
148- SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) }
149- }
150-
151192 pub fn now ( ) -> SystemTime {
152193 use crate :: ptr;
153194
154195 let mut s = libc:: timeval { tv_sec : 0 , tv_usec : 0 } ;
155196 cvt ( unsafe { libc:: gettimeofday ( & mut s, ptr:: null_mut ( ) ) } ) . unwrap ( ) ;
156197 return SystemTime :: from ( s) ;
157198 }
158-
159- pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
160- self . t . sub_timespec ( & other. t )
161- }
162-
163- pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
164- Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
165- }
166-
167- pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < SystemTime > {
168- Some ( SystemTime { t : self . t . checked_sub_duration ( other) ? } )
169- }
170199 }
171200
172201 impl From < libc:: timeval > for Timespec {
@@ -181,27 +210,6 @@ mod inner {
181210 }
182211 }
183212
184- impl From < libc:: timespec > for Timespec {
185- fn from ( t : libc:: timespec ) -> Timespec {
186- Timespec :: new ( t. tv_sec as i64 , t. tv_nsec as i64 )
187- }
188- }
189-
190- impl From < libc:: timespec > for SystemTime {
191- fn from ( t : libc:: timespec ) -> SystemTime {
192- SystemTime { t : Timespec :: from ( t) }
193- }
194- }
195-
196- impl fmt:: Debug for SystemTime {
197- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
198- f. debug_struct ( "SystemTime" )
199- . field ( "tv_sec" , & self . t . tv_sec )
200- . field ( "tv_nsec" , & self . t . tv_nsec )
201- . finish ( )
202- }
203- }
204-
205213 fn checked_dur2intervals ( dur : & Duration ) -> Option < u64 > {
206214 let nanos =
207215 dur. as_secs ( ) . checked_mul ( NSEC_PER_SEC ) ?. checked_add ( dur. subsec_nanos ( ) as u64 ) ?;
@@ -256,20 +264,13 @@ mod inner {
256264 use crate :: sys:: cvt;
257265 use crate :: time:: Duration ;
258266
259- use super :: Timespec ;
267+ use super :: { SystemTime , Timespec } ;
260268
261269 #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
262270 pub struct Instant {
263271 t : Timespec ,
264272 }
265273
266- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
267- pub struct SystemTime {
268- pub ( in crate :: sys:: unix) t : Timespec ,
269- }
270-
271- pub const UNIX_EPOCH : SystemTime = SystemTime { t : Timespec :: zero ( ) } ;
272-
273274 impl Instant {
274275 pub fn now ( ) -> Instant {
275276 Instant { t : Timespec :: now ( libc:: CLOCK_MONOTONIC ) }
@@ -298,46 +299,9 @@ mod inner {
298299 }
299300
300301 impl SystemTime {
301- pub fn new ( tv_sec : i64 , tv_nsec : i64 ) -> SystemTime {
302- SystemTime { t : Timespec :: new ( tv_sec, tv_nsec) }
303- }
304-
305302 pub fn now ( ) -> SystemTime {
306303 SystemTime { t : Timespec :: now ( libc:: CLOCK_REALTIME ) }
307304 }
308-
309- pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
310- self . t . sub_timespec ( & other. t )
311- }
312-
313- pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
314- Some ( SystemTime { t : self . t . checked_add_duration ( other) ? } )
315- }
316-
317- pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < SystemTime > {
318- Some ( SystemTime { t : self . t . checked_sub_duration ( other) ? } )
319- }
320- }
321-
322- impl From < libc:: timespec > for Timespec {
323- fn from ( t : libc:: timespec ) -> Timespec {
324- Timespec :: new ( t. tv_sec as i64 , t. tv_nsec as i64 )
325- }
326- }
327-
328- impl From < libc:: timespec > for SystemTime {
329- fn from ( t : libc:: timespec ) -> SystemTime {
330- SystemTime { t : Timespec :: from ( t) }
331- }
332- }
333-
334- impl fmt:: Debug for SystemTime {
335- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
336- f. debug_struct ( "SystemTime" )
337- . field ( "tv_sec" , & self . t . tv_sec )
338- . field ( "tv_nsec" , & self . t . tv_nsec )
339- . finish ( )
340- }
341305 }
342306
343307 #[ cfg( not( any( target_os = "dragonfly" , target_os = "espidf" ) ) ) ]
@@ -378,13 +342,5 @@ mod inner {
378342 cvt ( unsafe { libc:: clock_gettime ( clock, t. as_mut_ptr ( ) ) } ) . unwrap ( ) ;
379343 Timespec :: from ( unsafe { t. assume_init ( ) } )
380344 }
381-
382- pub fn to_timespec ( & self ) -> Option < libc:: timespec > {
383- use crate :: convert:: TryInto ;
384- Some ( libc:: timespec {
385- tv_sec : self . tv_sec . try_into ( ) . ok ( ) ?,
386- tv_nsec : self . tv_nsec . try_into ( ) . ok ( ) ?,
387- } )
388- }
389345 }
390346}
0 commit comments