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