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-
11+ #[ cfg( stage0) ]
1512use prelude:: v1:: * ;
1613
17- use fmt;
1814use ops:: { Add , Sub , Mul , Div } ;
1915use sys:: time:: SteadyTime ;
2016
@@ -42,11 +38,12 @@ const MILLIS_PER_SEC: u64 = 1_000;
4238/// let five_seconds = Duration::new(5, 0);
4339/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
4440///
45- /// assert_eq!(five_seconds_and_five_nanos.secs (), 5);
46- /// 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);
4743///
4844/// let ten_millis = Duration::from_millis(10);
4945/// ```
46+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
5047#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
5148pub struct Duration {
5249 secs : u64 ,
@@ -59,6 +56,7 @@ impl Duration {
5956 ///
6057 /// If the nanoseconds is greater than 1 billion (the number of nanoseconds
6158 /// in a second), then it will carry over into the seconds provided.
59+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
6260 pub fn new ( secs : u64 , nanos : u32 ) -> Duration {
6361 let secs = secs + ( nanos / NANOS_PER_SEC ) as u64 ;
6462 let nanos = nanos % NANOS_PER_SEC ;
@@ -78,11 +76,13 @@ impl Duration {
7876 }
7977
8078 /// Creates a new `Duration` from the specified number of seconds.
79+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8180 pub fn from_secs ( secs : u64 ) -> Duration {
8281 Duration { secs : secs, nanos : 0 }
8382 }
8483
8584 /// Creates a new `Duration` from the specified number of milliseconds.
85+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8686 pub fn from_millis ( millis : u64 ) -> Duration {
8787 let secs = millis / MILLIS_PER_SEC ;
8888 let nanos = ( ( millis % MILLIS_PER_SEC ) as u32 ) * NANOS_PER_MILLI ;
@@ -93,14 +93,16 @@ impl Duration {
9393 ///
9494 /// The extra precision represented by this duration is ignored (e.g. extra
9595 /// nanoseconds are not represented in the returned value).
96- pub fn secs ( & self ) -> u64 { self . secs }
96+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
97+ pub fn as_secs ( & self ) -> u64 { self . secs }
9798
9899 /// Returns the nanosecond precision represented by this duration.
99100 ///
100101 /// This method does **not** return the length of the duration when
101102 /// represented by nanoseconds. The returned number always represents a
102103 /// fractional portion of a second (e.g. it is less than one billion).
103- 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 }
104106}
105107
106108impl Add for Duration {
@@ -166,20 +168,6 @@ impl Div<u32> for Duration {
166168 }
167169}
168170
169- impl fmt:: Display for Duration {
170- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
171- match ( self . secs , self . nanos ) {
172- ( s, 0 ) => write ! ( f, "{}s" , s) ,
173- ( 0 , n) if n % NANOS_PER_MILLI == 0 => write ! ( f, "{}ms" ,
174- n / NANOS_PER_MILLI ) ,
175- ( 0 , n) if n % 1_000 == 0 => write ! ( f, "{}µs" , n / 1_000 ) ,
176- ( 0 , n) => write ! ( f, "{}ns" , n) ,
177- ( s, n) => write ! ( f, "{}.{}s" , s,
178- format!( "{:09}" , n) . trim_right_matches( '0' ) )
179- }
180- }
181- }
182-
183171#[ cfg( test) ]
184172mod tests {
185173 use prelude:: v1:: * ;
@@ -197,20 +185,20 @@ mod tests {
197185
198186 #[ test]
199187 fn secs ( ) {
200- assert_eq ! ( Duration :: new( 0 , 0 ) . secs ( ) , 0 ) ;
201- assert_eq ! ( Duration :: from_secs( 1 ) . secs ( ) , 1 ) ;
202- assert_eq ! ( Duration :: from_millis( 999 ) . secs ( ) , 0 ) ;
203- 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 ) ;
204192 }
205193
206194 #[ test]
207195 fn nanos ( ) {
208- assert_eq ! ( Duration :: new( 0 , 0 ) . extra_nanos ( ) , 0 ) ;
209- assert_eq ! ( Duration :: new( 0 , 5 ) . extra_nanos ( ) , 5 ) ;
210- assert_eq ! ( Duration :: new( 0 , 1_000_000_001 ) . extra_nanos ( ) , 1 ) ;
211- assert_eq ! ( Duration :: from_secs( 1 ) . extra_nanos ( ) , 0 ) ;
212- assert_eq ! ( Duration :: from_millis( 999 ) . extra_nanos ( ) , 999 * 1_000_000 ) ;
213- 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 ) ;
214202 }
215203
216204 #[ test]
@@ -257,18 +245,4 @@ mod tests {
257245 assert_eq ! ( Duration :: new( 99 , 999_999_000 ) / 100 ,
258246 Duration :: new( 0 , 999_999_990 ) ) ;
259247 }
260-
261- #[ test]
262- fn display ( ) {
263- assert_eq ! ( Duration :: new( 0 , 2 ) . to_string( ) , "2ns" ) ;
264- assert_eq ! ( Duration :: new( 0 , 2_000_000 ) . to_string( ) , "2ms" ) ;
265- assert_eq ! ( Duration :: new( 2 , 0 ) . to_string( ) , "2s" ) ;
266- assert_eq ! ( Duration :: new( 2 , 2 ) . to_string( ) , "2.000000002s" ) ;
267- assert_eq ! ( Duration :: new( 2 , 2_000_000 ) . to_string( ) ,
268- "2.002s" ) ;
269- assert_eq ! ( Duration :: new( 0 , 2_000_002 ) . to_string( ) ,
270- "2000002ns" ) ;
271- assert_eq ! ( Duration :: new( 2 , 2_000_002 ) . to_string( ) ,
272- "2.002000002s" ) ;
273- }
274248}
0 commit comments