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
@@ -37,17 +32,17 @@ const MILLIS_PER_SEC: u64 = 1_000;
3732/// # Examples
3833///
3934/// ```
40- /// #![feature(duration)]
4135/// use std::time::Duration;
4236///
4337/// let five_seconds = Duration::new(5, 0);
4438/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
4539///
46- /// assert_eq!(five_seconds_and_five_nanos.secs (), 5);
47- /// assert_eq!(five_seconds_and_five_nanos.extra_nanos (), 5);
40+ /// assert_eq!(five_seconds_and_five_nanos.as_secs (), 5);
41+ /// assert_eq!(five_seconds_and_five_nanos.subsec_nanos (), 5);
4842///
4943/// let ten_millis = Duration::from_millis(10);
5044/// ```
45+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
5146#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
5247pub struct Duration {
5348 secs : u64 ,
@@ -60,6 +55,7 @@ impl Duration {
6055 ///
6156 /// If the nanoseconds is greater than 1 billion (the number of nanoseconds
6257 /// in a second), then it will carry over into the seconds provided.
58+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
6359 pub fn new ( secs : u64 , nanos : u32 ) -> Duration {
6460 let secs = secs + ( nanos / NANOS_PER_SEC ) as u64 ;
6561 let nanos = nanos % NANOS_PER_SEC ;
@@ -79,11 +75,13 @@ impl Duration {
7975 }
8076
8177 /// Creates a new `Duration` from the specified number of seconds.
78+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8279 pub fn from_secs ( secs : u64 ) -> Duration {
8380 Duration { secs : secs, nanos : 0 }
8481 }
8582
8683 /// Creates a new `Duration` from the specified number of milliseconds.
84+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8785 pub fn from_millis ( millis : u64 ) -> Duration {
8886 let secs = millis / MILLIS_PER_SEC ;
8987 let nanos = ( ( millis % MILLIS_PER_SEC ) as u32 ) * NANOS_PER_MILLI ;
@@ -94,14 +92,33 @@ impl Duration {
9492 ///
9593 /// The extra precision represented by this duration is ignored (e.g. extra
9694 /// nanoseconds are not represented in the returned value).
97- pub fn secs ( & self ) -> u64 { self . secs }
95+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
96+ pub fn as_secs ( & self ) -> u64 { self . secs }
97+
98+ #[ deprecated( reason = "renamed to `as_secs`" , since = "1.3.0" ) ]
99+ #[ unstable( feature = "duration_deprecated" ) ]
100+ /// Returns the number of whole seconds represented by this duration.
101+ ///
102+ /// The extra precision represented by this duration is ignored (e.g. extra
103+ /// nanoseconds are not represented in the returned value).
104+ pub fn secs ( & self ) -> u64 { self . as_secs ( ) }
105+
106+ /// Returns the nanosecond precision represented by this duration.
107+ ///
108+ /// This method does **not** return the length of the duration when
109+ /// represented by nanoseconds. The returned number always represents a
110+ /// fractional portion of a second (e.g. it is less than one billion).
111+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
112+ pub fn subsec_nanos ( & self ) -> u32 { self . nanos }
98113
114+ #[ deprecated( reason = "renamed to `subsec_nanos`" , since = "1.3.0" ) ]
115+ #[ unstable( feature = "duration_deprecated" ) ]
99116 /// Returns the nanosecond precision represented by this duration.
100117 ///
101118 /// This method does **not** return the length of the duration when
102119 /// represented by nanoseconds. The returned number always represents a
103120 /// fractional portion of a second (e.g. it is less than one billion).
104- pub fn extra_nanos ( & self ) -> u32 { self . nanos }
121+ pub fn extra_nanos ( & self ) -> u32 { self . subsec_nanos ( ) }
105122}
106123
107124impl Add for Duration {
@@ -167,20 +184,6 @@ impl Div<u32> for Duration {
167184 }
168185}
169186
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-
184187#[ cfg( test) ]
185188mod tests {
186189 use prelude:: v1:: * ;
@@ -198,20 +201,20 @@ mod tests {
198201
199202 #[ test]
200203 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 ) ;
204+ assert_eq ! ( Duration :: new( 0 , 0 ) . as_secs ( ) , 0 ) ;
205+ assert_eq ! ( Duration :: from_secs( 1 ) . as_secs ( ) , 1 ) ;
206+ assert_eq ! ( Duration :: from_millis( 999 ) . as_secs ( ) , 0 ) ;
207+ assert_eq ! ( Duration :: from_millis( 1001 ) . as_secs ( ) , 1 ) ;
205208 }
206209
207210 #[ test]
208211 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 ) ;
212+ assert_eq ! ( Duration :: new( 0 , 0 ) . subsec_nanos ( ) , 0 ) ;
213+ assert_eq ! ( Duration :: new( 0 , 5 ) . subsec_nanos ( ) , 5 ) ;
214+ assert_eq ! ( Duration :: new( 0 , 1_000_000_001 ) . subsec_nanos ( ) , 1 ) ;
215+ assert_eq ! ( Duration :: from_secs( 1 ) . subsec_nanos ( ) , 0 ) ;
216+ assert_eq ! ( Duration :: from_millis( 999 ) . subsec_nanos ( ) , 999 * 1_000_000 ) ;
217+ assert_eq ! ( Duration :: from_millis( 1001 ) . subsec_nanos ( ) , 1 * 1_000_000 ) ;
215218 }
216219
217220 #[ test]
@@ -258,18 +261,4 @@ mod tests {
258261 assert_eq ! ( Duration :: new( 99 , 999_999_000 ) / 100 ,
259262 Duration :: new( 0 , 999_999_990 ) ) ;
260263 }
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- }
275264}
0 commit comments