6
6
# Summary
7
7
8
8
This RFC proposes several new types and associated APIs for working with times in Rust.
9
- The primary new types are ` Instance ` , for working with time that is guaranteed to be
9
+ The primary new types are ` Instant ` , for working with time that is guaranteed to be
10
10
monotonic, and ` SystemTime ` , for working with times across processes on a single system
11
11
(usually internally represented as a number of seconds since an epoch).
12
12
@@ -121,7 +121,7 @@ directly address time zones.
121
121
122
122
## Types
123
123
124
- ``` rs
124
+ ``` rust
125
125
pub struct Instant {
126
126
secs : u64 ,
127
127
nanos : u32
@@ -167,8 +167,11 @@ use for negative values. Rather than require each API that takes a `Duration`
167
167
to produce an ` Err ` (or ` panic! ` ) when receiving a negative value, this design
168
168
optimizes for the broadly useful positive ` Duration ` .
169
169
170
- ``` rs
170
+ ``` rust
171
171
impl Instant {
172
+ /// Returns an instant corresponding to "now".
173
+ pub fn now () -> Instant ;
174
+
172
175
/// Panics if `earlier` is later than &self.
173
176
/// Because Instant is monotonic, the only time that `earlier` should be
174
177
/// a later time is a bug in your code.
@@ -180,7 +183,7 @@ impl Instant {
180
183
}
181
184
182
185
impl Add <Duration > for Instant {
183
- type Output = SystemTime ;
186
+ type Output = Instant ;
184
187
}
185
188
186
189
impl Sub <Duration > for Instant {
@@ -202,7 +205,7 @@ The "standard" terminology comes from [JodaTime][joda-time-standard].
202
205
203
206
[ joda-time-standard ] : http://joda-time.sourceforge.net/apidocs/org/joda/time/Duration.html#standardDays(long)
204
207
205
- ``` rs
208
+ ``` rust
206
209
impl Duration {
207
210
/// a standard minute is 60 seconds
208
211
/// panics if the number of minutes is larger than u64 seconds
@@ -241,8 +244,11 @@ This design attempts to help the programmer catch the most egregious of these
241
244
kinds of mistakes (unexpected travel ** back in time** ) before the mistake
242
245
propagates.
243
246
244
- ``` rs
247
+ ``` rust
245
248
impl SystemTime {
249
+ /// Returns the system time corresponding to "now".
250
+ pub fn now () -> SystemTime ;
251
+
246
252
/// Returns an `Err` if `earlier` is later
247
253
pub fn duration_from_earlier (& self , earlier : SystemTime ) -> Result <Duration , SystemTimeError >;
248
254
@@ -258,13 +264,27 @@ impl Sub<Duration> for SystemTime {
258
264
type Output = SystemTime ;
259
265
}
260
266
267
+ // An anchor which can be used to generate new SystemTime instances from a known
268
+ // Duration or convert a SystemTime to a Duration which can later then be used
269
+ // again to recreate the SystemTime.
270
+ //
271
+ // Defined to be "1970-01-01 00:00:00 UTC" on all systems.
272
+ const UNIX_EPOCH : SystemTime = ... ;
273
+
261
274
// Note that none of these operations actually imply that the underlying system
262
275
// operation that produced these SystemTimes happened at the same time
263
276
// (for Eq) or before/after (for Ord) than the other system operation.
264
277
impl PartialEq for SystemTime ;
265
278
impl Eq for SystemTime ;
266
279
impl PartialOrd for SystemTime ;
267
280
impl Ord for SystemTime ;
281
+
282
+ impl SystemTimeError {
283
+ /// A SystemTimeError originates from attempting to subtract two SystemTime
284
+ /// instances, a and b. If a < b then an error is returned, and the duration
285
+ /// returned represents (b - a).
286
+ pub fn duration (& self ) -> Duration ;
287
+ }
268
288
```
269
289
270
290
The main difference from the design of ` Instant ` is that it is impossible to
@@ -326,7 +346,5 @@ use `duration_from_earlier` reliably to get a positive `Duration`.
326
346
327
347
# Unresolved Questions
328
348
329
- What should ` SystemTimeError ` look like?
330
-
331
349
This RFC leaves types related to human representations of dates and times
332
- to a future proposal.
350
+ to a future proposal.
0 commit comments