-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improvements to the Time APIs #1288
Conversation
|
||
It provides convenience methods for constructing Durations from larger units | ||
of time (minutes, hours, days), but gives them names like | ||
`Duration.from_standard_hour`. A standard hour is 3600 seconds, regardless of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regardless of…?
@nagisa thanks for the early feedback. I'll correct these typos ASAP! |
the timeframe of the process it was created in. | ||
|
||
> In this context, monotonic means that a timestamp created later in real-world | ||
> time will always be larger than a timestamp created earlier in real-world |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pedantically speaking, "no smaller than", since you might pull the time twice within the same tick.
Where should By the way, should |
I like having the distinction between monotonic and system time. |
cc @lifthrasiir |
Might it be worth contrasting this API with that offered by e.g. Jodatime, On 22 September 2015 at 00:05, Aaron Turon notifications@github.com wrote:
|
Hopefully not just The article is clearly really old, but might help this design: |
@wycats from the wording in the current draft it seems like the biggest problem isn't actually monotonicity of time but the creation of negative durations. If there are no other issues with time lacking monotonicity, one alternative I haven't seen discussed but would like to mention is to instead split up
Apart from naming conventions, I kinda like the consistency it has with numbers. I don't think it even changes all that much:
Pros: Cons: Let me know what downsides I'm not aware of or am currently missing. Thanks. P.S. After writing this up, if it helps, I did find: https://internals.rust-lang.org/t/unsigned-version-of-duration/893 |
I have nothing against a positive-first API, but one should not ignore negative durations either. That said, I can't really think of a case where you'd want signed durations and not want to branch off their direction. So my suggestion is that the |
I don't think libstd should contain a calendar - these contain lots of subjective edge cases. However, we probably want some form of absolute time for cross-system timestamps (we would need to choose between classic UNIX, UTS, or TAI). Leap-second-aware format conversions would be nice. |
Personally, I strongly favor TAI for cross-system times, for the following reasons:
|
Two minor suggestions:
pub struct StdHours(pub u64);
pub struct StdMinutes(pub u64);
// ...
impl From<StdHours> for Duration {
// ...
}
// ... Then conversions become as simple as: What do you think? Edit: One worry might be panicking |
Replying to @cristicbz (more later on other comments): I don't love MonotonicTime since I expect this to be the primary API developers use for things like benchmarks, and an obscure name might put them off. The point about being allowed to use BOOT_TIME is good, though. Can you think of a friendly name that communicates monotonicity? TimeThatObeysTheLawsOfPhysics :p I don't object to conversions at first glance and agree that they read nicely, provide nice ergonomics for mixing, and make it easy to avoid mistakes. I'll let others weigh in on the panicking issue before I form a strong opinion though :) @aturon? |
I think that C++'s
For As far as use cases go, what should each be used for?
Notably not covered:
|
That’s not a valid solution, is it? Regarding @vadimcn’s link, virtualised environments are a pretty interesting use case/test which might uncover a whole can of worms with every platform we support (and possibly various different processors that might or might not have “interesting” clocks, if any), since they might not allow virtualised kernel to access hardware directly and other small details. |
I was hoping it was like the architectural constraints/guarantees for |
@eternaleye: I don't recall seeing the 2-3s number anywhere. I would guess that non-monotonicity is on the order of as few ticks (as returned by |
It's a comment by the original reporter in response to a question about the magnitude.
|
@eternaleye: he probably meant 'ticks', not 'milliseconds'. |
ElapsedMilliseconds is consistently used - both in the original post's code example and the comments. |
Yeah - having a |
Seems that no one mentioned but according to wikipedia the desicion whether to keep or abolish leap seconds will be made at World Radiocommunication Conference which is scheduled for November 2–27 (i.e. it is happening now). Indeed, their page states:
Lets hope that they will do the right thing. |
`Duration` when used correctly. | ||
|
||
This design does not assume that negative `Duration`s are never useful, but | ||
rather than the most common uses of `Duration` do not have a meaningful |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: s/rather than/rather that/
Final clarifications
The libs team discussed this RFC during triage last week and after some final clarifications we've decided to merge. The last pieces were indicating that For now we've decided to take the conservative route of not trying to exhaustively specify all aspects of the two types here, but as usual there will be a period of Thanks again for all the discussion everyone! |
I've now got an implementation of this RFC, so if you'd like to take a look over it please feel free to leave a comment there! |
This commit is an implementation of [RFC 1288][rfc] which adds two new unstable types to the `std::time` module. The `Instant` type is used to represent measurements of a monotonically increasing clock suitable for measuring time withing a process for operations such as benchmarks or just the elapsed time to do something. An `Instant` favors panicking when bugs are found as the bugs are programmer errors rather than typical errors that can be encountered. [rfc]: rust-lang/rfcs#1288 The `SystemTime` type is used to represent a system timestamp and is not monotonic. Very few guarantees are provided about this measurement of the system clock, but a fixed point in time (`UNIX_EPOCH`) is provided to learn about the relative distance from this point for any particular time stamp. This PR takes the same implementation strategy as the `time` crate on crates.io, namely: | Platform | Instant | SystemTime | |------------|--------------------------|--------------------------| | Windows | QueryPerformanceCounter | GetSystemTimeAsFileTime | | OSX | mach_absolute_time | gettimeofday | | Unix | CLOCK_MONOTONIC | CLOCK_REALTIME | These implementations can perhaps be refined over time, but they currently satisfy the requirements of the `Instant` and `SystemTime` types while also being portable across implementations and revisions of each platform.
This commit is an implementation of [RFC 1288][rfc] which adds two new unstable types to the `std::time` module. The `Instant` type is used to represent measurements of a monotonically increasing clock suitable for measuring time withing a process for operations such as benchmarks or just the elapsed time to do something. An `Instant` favors panicking when bugs are found as the bugs are programmer errors rather than typical errors that can be encountered. [rfc]: rust-lang/rfcs#1288 The `SystemTime` type is used to represent a system timestamp and is not monotonic. Very few guarantees are provided about this measurement of the system clock, but a fixed point in time (`UNIX_EPOCH`) is provided to learn about the relative distance from this point for any particular time stamp. This PR takes the same implementation strategy as the `time` crate on crates.io, namely: | Platform | Instant | SystemTime | |------------|--------------------------|--------------------------| | Windows | QueryPerformanceCounter | GetSystemTimeAsFileTime | | OSX | mach_absolute_time | gettimeofday | | Unix | CLOCK_MONOTONIC | CLOCK_REALTIME | These implementations can perhaps be refined over time, but they currently satisfy the requirements of the `Instant` and `SystemTime` types while also being portable across implementations and revisions of each platform. cc #29866
Why not implement For example use std::time::Instant;
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord)]
struct ID {
random: u32,
instant: Instant,
} |
@tioover Duration now implements Hash: rust-lang/rust#30818. The lack of an implementation for Instant is just an oversight - feel free to make a PR adding the impl. Since this RFC has been merged, rust-lang/rust#29866 is the new place for discussion, by the way. |
Rendered