8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- //! Temporal quantification
12
-
13
- #![ unstable( feature = "duration" , reason = "recently added API per RFC 1040" ) ]
14
-
11
+ #[ cfg( stage0) ]
15
12
use prelude:: v1:: * ;
16
13
17
- use fmt;
18
14
use ops:: { Add , Sub , Mul , Div } ;
19
15
use sys:: time:: SteadyTime ;
20
16
@@ -36,17 +32,17 @@ const MILLIS_PER_SEC: u64 = 1_000;
36
32
/// # Examples
37
33
///
38
34
/// ```
39
- /// #![feature(duration)]
40
35
/// use std::time::Duration;
41
36
///
42
37
/// let five_seconds = Duration::new(5, 0);
43
38
/// let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
44
39
///
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);
47
42
///
48
43
/// let ten_millis = Duration::from_millis(10);
49
44
/// ```
45
+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
50
46
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Debug ) ]
51
47
pub struct Duration {
52
48
secs : u64 ,
@@ -59,6 +55,7 @@ impl Duration {
59
55
///
60
56
/// If the nanoseconds is greater than 1 billion (the number of nanoseconds
61
57
/// in a second), then it will carry over into the seconds provided.
58
+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
62
59
pub fn new ( secs : u64 , nanos : u32 ) -> Duration {
63
60
let secs = secs + ( nanos / NANOS_PER_SEC ) as u64 ;
64
61
let nanos = nanos % NANOS_PER_SEC ;
@@ -78,11 +75,13 @@ impl Duration {
78
75
}
79
76
80
77
/// Creates a new `Duration` from the specified number of seconds.
78
+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
81
79
pub fn from_secs ( secs : u64 ) -> Duration {
82
80
Duration { secs : secs, nanos : 0 }
83
81
}
84
82
85
83
/// Creates a new `Duration` from the specified number of milliseconds.
84
+ #[ stable( feature = "duration" , since = "1.3.0" ) ]
86
85
pub fn from_millis ( millis : u64 ) -> Duration {
87
86
let secs = millis / MILLIS_PER_SEC ;
88
87
let nanos = ( ( millis % MILLIS_PER_SEC ) as u32 ) * NANOS_PER_MILLI ;
@@ -93,14 +92,33 @@ impl Duration {
93
92
///
94
93
/// The extra precision represented by this duration is ignored (e.g. extra
95
94
/// 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 }
97
113
114
+ #[ deprecated( reason = "renamed to `subsec_nanos`" , since = "1.3.0" ) ]
115
+ #[ unstable( feature = "duration_deprecated" ) ]
98
116
/// Returns the nanosecond precision represented by this duration.
99
117
///
100
118
/// This method does **not** return the length of the duration when
101
119
/// represented by nanoseconds. The returned number always represents a
102
120
/// 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 ( ) }
104
122
}
105
123
106
124
impl Add for Duration {
@@ -166,20 +184,6 @@ impl Div<u32> for Duration {
166
184
}
167
185
}
168
186
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
-
183
187
#[ cfg( test) ]
184
188
mod tests {
185
189
use prelude:: v1:: * ;
@@ -197,20 +201,20 @@ mod tests {
197
201
198
202
#[ test]
199
203
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 ) ;
204
208
}
205
209
206
210
#[ test]
207
211
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 ) ;
214
218
}
215
219
216
220
#[ test]
@@ -257,18 +261,4 @@ mod tests {
257
261
assert_eq ! ( Duration :: new( 99 , 999_999_000 ) / 100 ,
258
262
Duration :: new( 0 , 999_999_990 ) ) ;
259
263
}
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
- }
274
264
}
0 commit comments