Skip to content

Commit 1959033

Browse files
committed
Duration Reform RFC
1 parent d46a957 commit 1959033

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

text/0000-duration-reform.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
- Feature Name: Duration Reform
2+
- Start Date: 2015-03-24
3+
- RFC PR: (leave this empty)
4+
- Rust Issue: (leave this empty)
5+
6+
# Summary
7+
8+
This RFC suggests stabilizing a reduced-scope `Duration` type that is appropriate for interoperating with various system calls that require timeouts. It does not stabilize a large number of conversion methods in `Duration` that have subtle caveats, with the intent of revisiting those conversions more holistically in the future.
9+
10+
# Motivation
11+
12+
There are a number of different notions of "time", each of which has a different set of caveats, and each of which can be designed for optimal ergonomics for its domain. This proposal focuses on one particular one: an amount of time in high-precision units.
13+
14+
Eventually, there are a number of concepts of time that deserve fleshed out APIs. Using the terminology from the popular Java time library [JodaTime][joda-time]:
15+
16+
* `Duration`: an amount of time, described in terms of a high
17+
precision unit.
18+
* `Period`: an amount of time described in human terms ("5 minutes,
19+
27 seconds"), and which can only be resolved into a `Duration`
20+
relative to a moment in time.
21+
* `Instant`: a moment in time represented in terms of a `Duration`
22+
since some epoch.
23+
24+
[joda-time]: http://www.joda.org/joda-time/
25+
26+
Human complications such as leap seconds, days in a month, and leap years, and machine complications such as NTP adjustments make these concepts and their full APIs more complicated than they would at first appear. This proposal focuses on fleshing out a design for `Duration` that is sufficient for use as a timeout, leaving the other concepts of time to a future proposal.
27+
28+
---
29+
30+
For the most part, the system APIs that this type is used to communicate with either use `timespec` (`u64` seconds plus `u32` nanos) or take a timeout in milliseconds (`u32` on Windows).
31+
32+
> For example, [`GetQueuedCompletionStatus`][iocp-ms-example], one of
33+
> the primary APIs in the Windows IOCP API, takes a `dwMilliseconds`
34+
> parameter as a [`DWORD`][msdn-dword], which is a `u32`. Some Windows
35+
> APIs use "ticks" or 100-nanosecond units.
36+
37+
[iocp-ms-example]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa364986%28v=vs.85%29.aspx
38+
[msdn-dword]: https://msdn.microsoft.com/en-us/library/cc230318.aspx
39+
40+
In light of that, this proposal has two primary goals:
41+
42+
* to define a type that can describe portable timeouts for cross-
43+
platform APIs
44+
* to describe what should happen if a large `Duration` is passed into
45+
an API that does not accept timeouts that large
46+
47+
In general, this proposal considers it acceptable to reduce the granularity of timeouts (eliminating nanosecond granularity if only milliseconds are supported) and to truncate very large timeouts.
48+
49+
This proposal retains the two fields in the existing `Duration`:
50+
51+
* a `u64` of seconds
52+
* a `u32` of additional nanosecond precision
53+
54+
Timeout APIs defined in terms of milliseconds will truncate `Duration`s that are more than `u32::MAX` in milliseconds, and will reduce the granularity of the nanosecond field.
55+
56+
> A `u32` of milliseconds supports a timeout longer than 45 days.
57+
58+
Future APIs to support a broader set of [Durations][joda-duration] APIs, a [Period][joda-period] and [Instant][joda-instant] type, as well as coercions between these types, would be useful, compatible follow-ups to this RFC.
59+
60+
[joda-duration]: http://www.joda.org/joda-time/key_duration.html
61+
[joda-period]: http://www.joda.org/joda-time/key_period.html
62+
[joda-instant]: http://www.joda.org/joda-time/key_instant.html
63+
64+
# Detailed design
65+
66+
A `Duration` represents a period of time represented in terms of nanosecond granularity. It has `u64` seconds and an additional `u32` nanoseconds. There is no concept of a negative `Duration`.
67+
68+
> A negative `Duration` has no meaning for many APIs that may wish
69+
> to take a `Duration`, which means that all such APIs would need
70+
> to decide what to do when confronted with a negative `Duration`.
71+
> As a result, this proposal focuses on the predominant use-cases for
72+
> `Duration`, where unsigned types remove a number of caveats and
73+
> ambiguities.
74+
75+
```rust
76+
pub struct Duration {
77+
secs: u64,
78+
nanos: u32 // may not be more than 1 billion
79+
}
80+
81+
impl Duration {
82+
/// create a Duration from a number of seconds and an
83+
/// additional nanosecond precision
84+
pub fn new(secs: u64, nanos: u32) -> Timeout;
85+
86+
/// create a Duration from a number of seconds
87+
pub fn from_secs(secs: u64) -> Timeout;
88+
89+
/// create a Duration from a number of milliseconds
90+
pub fn from_millis(millis: u64) -> Timeout;
91+
92+
/// the number of seconds represented by the Timeout
93+
pub fn secs(self) -> u64;
94+
95+
/// the number of additional nanosecond precision
96+
pub fn nanos(self) -> u32;
97+
}
98+
```
99+
100+
When `Duration` is used with a system API that expects `u32` milliseconds, the nanosecond precision is dropped, and the time is truncated to `u32::MAX`.
101+
102+
`Duration` implements:
103+
104+
* `Add`, `Sub`, `Mul`, `Div` which follow the overflow and underflow
105+
rules for `u64` when applied to the `secs` field. Nanoseconds
106+
can never exceed 1 billion or be less than 0, and carry into the
107+
`secs` field.
108+
* `Display`, which prints a number of seconds, milliseconds and
109+
nanoseconds (if more than 0).
110+
* `Debug`, `Ord` (and `PartialOrd`), `Eq` (and `PartialEq`), `Copy`
111+
and `Clone`, which are derived.
112+
113+
This proposal does not, at this time, include mechanisms for instantiating a `Duration` from `weeks`, `days`, `hours` or `minutes`, because there are caveats to each of those units. In particular, the existence of leap seconds means that it is only possible to properly understand them relative to a particular starting point.
114+
115+
The Joda-Time library in Java explains the problem well [in their documentation][joda-period-confusion]:
116+
117+
[joda-period-confusion]: http://www.joda.org/joda-time/key_period.html
118+
119+
> A duration in Joda-Time represents a duration of time measured in milliseconds. The duration is often obtained from an interval. Durations are a very simple concept, and the implementation is also simple. They have no chronology or time zone, **and consist solely of the millisecond duration.**
120+
121+
> A period in Joda-Time represents a period of time defined in terms of fields, for example, 3 years 5 months 2 days and 7 hours. This differs from a duration in that it is inexact in terms of milliseconds. **A period can only be resolved to an exact number of milliseconds by specifying the instant (including chronology and time zone) it is relative to**.
122+
123+
In short, this is saying that people expect "23:50:00 + 10 minutes" to equal "00:00:00", but it's impossible to know for sure whether that's true unless you know the exact starting point so you can take leap seconds into consideration.
124+
125+
In order to address this confusion, Joda-Time's Duration has methods like `standardDays`/`toStandardDays` and `standardHours`/`toStandardHours`, which are meant to indicate to the user that the number of milliseconds is based on the standard number of milliseconds in an hour, rather than the colloquial notion of an "hour".
126+
127+
An approach like this could work for Rust, but this RFC is intentionally limited in scope to areas without substantial tradeoffs in an attempt to allow a minimal solution to progress more quickly.
128+
129+
This proposal does not include a method to get a number of milliseconds from a `Duration`, because the number of milliseconds could exceed `u64`, and we would have to decide whether to return an `Option`, panic, or wait for a standard bignum. In the interest of limiting this proposal to APIs with a straight-forward design, this proposal defers such a method.
130+
131+
# Drawbacks
132+
133+
The main drawback to this proposal is that it is significantly more minimal than the existing `Duration` API. However, this API is quite sufficient for timeouts, and without the caveats in the existing `Duration` API.
134+
135+
# Alternatives
136+
137+
We could stabilize the existing `Duration` API. However, it has a number of serious caveats:
138+
139+
* The caveats described above about some of the units it supports.
140+
* It supports converting a `Duration` into a number of microseconds or
141+
nanoseconds. Because that cannot be done reliably, those methods
142+
return `Option`s, and APIs that need to convert `Duration` into
143+
nanoseconds have to re-surface the `Option` (unergonomic) or panic.
144+
* More generally, it has a fairly large API surface area, and almost
145+
every method has some caveat that would need to be explored in order
146+
to stabilize it.
147+
148+
---
149+
150+
We could also include a number of convenience APIs that convert from other units into `Duration`s. This proposal assumes that some of those conveniences will eventually be added. However, the design of each of those conveniences is ambiguous, so they are not included in this initial proposal.
151+
152+
---
153+
154+
Finally, we could avoid any API for timeouts, and simply take milliseconds throughout the standard library. However, this has two drawbacks.
155+
156+
First, it does not allow us to represent higher-precision timeouts on systems that could support them.
157+
158+
Second, while this proposal does not yet include conveniences, it assumes that some conveniences should be added in the future once the design space is more fully explored. Starting with a simple type gives us space to grow into.
159+
160+
# Unresolved questions
161+
162+
* Should we implement all of the listed traits? Others?

0 commit comments

Comments
 (0)