88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11- //! Temporal quantification
12-
13- #![ unstable( feature = "duration" , reason = "recently added API per RFC 1040" ) ]
14-
1511#[ cfg( stage0) ]
1612use prelude:: v1:: * ;
1713
18- use fmt;
1914use ops:: { Add , Sub , Mul , Div } ;
2015use sys:: time:: SteadyTime ;
2116
@@ -43,11 +38,12 @@ const MILLIS_PER_SEC: u64 = 1_000;
4338/// let five_seconds = Duration::new(5, 0);
4439/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
4540///
46- /// assert_eq!(five_seconds_and_five_nanos.secs (), 5);
47- /// assert_eq!(five_seconds_and_five_nanos.extra_nanos (), 5);
41+ /// assert_eq!(five_seconds_and_five_nanos.as_secs (), 5);
42+ /// assert_eq!(five_seconds_and_five_nanos.subsec_nanos (), 5);
4843///
4944/// let ten_millis = Duration::from_millis(10);
5045/// ```
46+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
5147#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
5248pub struct Duration {
5349 secs : u64 ,
@@ -60,6 +56,7 @@ impl Duration {
6056 ///
6157 /// If the nanoseconds is greater than 1 billion (the number of nanoseconds
6258 /// in a second), then it will carry over into the seconds provided.
59+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
6360 pub fn new ( secs : u64 , nanos : u32 ) -> Duration {
6461 let secs = secs + ( nanos / NANOS_PER_SEC ) as u64 ;
6562 let nanos = nanos % NANOS_PER_SEC ;
@@ -79,11 +76,13 @@ impl Duration {
7976 }
8077
8178 /// Creates a new `Duration` from the specified number of seconds.
79+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8280 pub fn from_secs ( secs : u64 ) -> Duration {
8381 Duration { secs : secs, nanos : 0 }
8482 }
8583
8684 /// Creates a new `Duration` from the specified number of milliseconds.
85+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8786 pub fn from_millis ( millis : u64 ) -> Duration {
8887 let secs = millis / MILLIS_PER_SEC ;
8988 let nanos = ( ( millis % MILLIS_PER_SEC ) as u32 ) * NANOS_PER_MILLI ;
@@ -94,14 +93,16 @@ impl Duration {
9493 ///
9594 /// The extra precision represented by this duration is ignored (e.g. extra
9695 /// nanoseconds are not represented in the returned value).
97- pub fn secs ( & self ) -> u64 { self . secs }
96+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
97+ pub fn as_secs ( & self ) -> u64 { self . secs }
9898
9999 /// Returns the nanosecond precision represented by this duration.
100100 ///
101101 /// This method does **not** return the length of the duration when
102102 /// represented by nanoseconds. The returned number always represents a
103103 /// fractional portion of a second (e.g. it is less than one billion).
104- pub fn extra_nanos ( & self ) -> u32 { self . nanos }
104+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
105+ pub fn subsec_nanos ( & self ) -> u32 { self . nanos }
105106}
106107
107108impl Add for Duration {
@@ -167,20 +168,6 @@ impl Div<u32> for Duration {
167168 }
168169}
169170
170- impl fmt:: Display for Duration {
171- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
172- match ( self . secs , self . nanos ) {
173- ( s, 0 ) => write ! ( f, "{}s" , s) ,
174- ( 0 , n) if n % NANOS_PER_MILLI == 0 => write ! ( f, "{}ms" ,
175- n / NANOS_PER_MILLI ) ,
176- ( 0 , n) if n % 1_000 == 0 => write ! ( f, "{}µs" , n / 1_000 ) ,
177- ( 0 , n) => write ! ( f, "{}ns" , n) ,
178- ( s, n) => write ! ( f, "{}.{}s" , s,
179- format!( "{:09}" , n) . trim_right_matches( '0' ) )
180- }
181- }
182- }
183-
184171#[ cfg( test) ]
185172mod tests {
186173 use prelude:: v1:: * ;
@@ -198,20 +185,20 @@ mod tests {
198185
199186 #[ test]
200187 fn secs ( ) {
201- assert_eq ! ( Duration :: new( 0 , 0 ) . secs ( ) , 0 ) ;
202- assert_eq ! ( Duration :: from_secs( 1 ) . secs ( ) , 1 ) ;
203- assert_eq ! ( Duration :: from_millis( 999 ) . secs ( ) , 0 ) ;
204- assert_eq ! ( Duration :: from_millis( 1001 ) . secs ( ) , 1 ) ;
188+ assert_eq ! ( Duration :: new( 0 , 0 ) . as_secs ( ) , 0 ) ;
189+ assert_eq ! ( Duration :: from_secs( 1 ) . as_secs ( ) , 1 ) ;
190+ assert_eq ! ( Duration :: from_millis( 999 ) . as_secs ( ) , 0 ) ;
191+ assert_eq ! ( Duration :: from_millis( 1001 ) . as_secs ( ) , 1 ) ;
205192 }
206193
207194 #[ test]
208195 fn nanos ( ) {
209- assert_eq ! ( Duration :: new( 0 , 0 ) . extra_nanos ( ) , 0 ) ;
210- assert_eq ! ( Duration :: new( 0 , 5 ) . extra_nanos ( ) , 5 ) ;
211- assert_eq ! ( Duration :: new( 0 , 1_000_000_001 ) . extra_nanos ( ) , 1 ) ;
212- assert_eq ! ( Duration :: from_secs( 1 ) . extra_nanos ( ) , 0 ) ;
213- assert_eq ! ( Duration :: from_millis( 999 ) . extra_nanos ( ) , 999 * 1_000_000 ) ;
214- assert_eq ! ( Duration :: from_millis( 1001 ) . extra_nanos ( ) , 1 * 1_000_000 ) ;
196+ assert_eq ! ( Duration :: new( 0 , 0 ) . subsec_nanos ( ) , 0 ) ;
197+ assert_eq ! ( Duration :: new( 0 , 5 ) . subsec_nanos ( ) , 5 ) ;
198+ assert_eq ! ( Duration :: new( 0 , 1_000_000_001 ) . subsec_nanos ( ) , 1 ) ;
199+ assert_eq ! ( Duration :: from_secs( 1 ) . subsec_nanos ( ) , 0 ) ;
200+ assert_eq ! ( Duration :: from_millis( 999 ) . subsec_nanos ( ) , 999 * 1_000_000 ) ;
201+ assert_eq ! ( Duration :: from_millis( 1001 ) . subsec_nanos ( ) , 1 * 1_000_000 ) ;
215202 }
216203
217204 #[ test]
@@ -258,18 +245,4 @@ mod tests {
258245 assert_eq ! ( Duration :: new( 99 , 999_999_000 ) / 100 ,
259246 Duration :: new( 0 , 999_999_990 ) ) ;
260247 }
261-
262- #[ test]
263- fn display ( ) {
264- assert_eq ! ( Duration :: new( 0 , 2 ) . to_string( ) , "2ns" ) ;
265- assert_eq ! ( Duration :: new( 0 , 2_000_000 ) . to_string( ) , "2ms" ) ;
266- assert_eq ! ( Duration :: new( 2 , 0 ) . to_string( ) , "2s" ) ;
267- assert_eq ! ( Duration :: new( 2 , 2 ) . to_string( ) , "2.000000002s" ) ;
268- assert_eq ! ( Duration :: new( 2 , 2_000_000 ) . to_string( ) ,
269- "2.002s" ) ;
270- assert_eq ! ( Duration :: new( 0 , 2_000_002 ) . to_string( ) ,
271- "2000002ns" ) ;
272- assert_eq ! ( Duration :: new( 2 , 2_000_002 ) . to_string( ) ,
273- "2.002000002s" ) ;
274- }
275248}
0 commit comments