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
@@ -36,17 +32,17 @@ const MILLIS_PER_SEC: u64 = 1_000;
3632/// # Examples
3733///
3834/// ```
39- /// #![feature(duration)]
4035/// use std::time::Duration;
4136///
4237/// let five_seconds = Duration::new(5, 0);
4338/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
4439///
45- /// assert_eq!(five_seconds_and_five_nanos.secs (), 5);
46- /// 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);
4742///
4843/// let ten_millis = Duration::from_millis(10);
4944/// ```
45+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
5046#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
5147pub struct Duration {
5248 secs : u64 ,
@@ -59,6 +55,7 @@ impl Duration {
5955 ///
6056 /// If the nanoseconds is greater than 1 billion (the number of nanoseconds
6157 /// in a second), then it will carry over into the seconds provided.
58+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
6259 pub fn new ( secs : u64 , nanos : u32 ) -> Duration {
6360 let secs = secs + ( nanos / NANOS_PER_SEC ) as u64 ;
6461 let nanos = nanos % NANOS_PER_SEC ;
@@ -78,11 +75,13 @@ impl Duration {
7875 }
7976
8077 /// Creates a new `Duration` from the specified number of seconds.
78+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8179 pub fn from_secs ( secs : u64 ) -> Duration {
8280 Duration { secs : secs, nanos : 0 }
8381 }
8482
8583 /// Creates a new `Duration` from the specified number of milliseconds.
84+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
8685 pub fn from_millis ( millis : u64 ) -> Duration {
8786 let secs = millis / MILLIS_PER_SEC ;
8887 let nanos = ( ( millis % MILLIS_PER_SEC ) as u32 ) * NANOS_PER_MILLI ;
@@ -93,14 +92,33 @@ impl Duration {
9392 ///
9493 /// The extra precision represented by this duration is ignored (e.g. extra
9594 /// nanoseconds are not represented in the returned value).
96- 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 }
97113
114+ #[ deprecated( reason = "renamed to `subsec_nanos`" , since = "1.3.0" ) ]
115+ #[ unstable( feature = "duration_deprecated" ) ]
98116 /// Returns the nanosecond precision represented by this duration.
99117 ///
100118 /// This method does **not** return the length of the duration when
101119 /// represented by nanoseconds. The returned number always represents a
102120 /// fractional portion of a second (e.g. it is less than one billion).
103- pub fn extra_nanos ( & self ) -> u32 { self . nanos }
121+ pub fn extra_nanos ( & self ) -> u32 { self . subsec_nanos ( ) }
104122}
105123
106124impl Add for Duration {
@@ -166,20 +184,6 @@ impl Div<u32> for Duration {
166184 }
167185}
168186
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-
183187#[ cfg( test) ]
184188mod tests {
185189 use prelude:: v1:: * ;
@@ -197,20 +201,20 @@ mod tests {
197201
198202 #[ test]
199203 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 ) ;
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 ) ;
204208 }
205209
206210 #[ test]
207211 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 ) ;
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 ) ;
214218 }
215219
216220 #[ test]
@@ -257,18 +261,4 @@ mod tests {
257261 assert_eq ! ( Duration :: new( 99 , 999_999_000 ) / 100 ,
258262 Duration :: new( 0 , 999_999_990 ) ) ;
259263 }
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- }
274264}
0 commit comments