-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Expand the std::time module #29866
Comments
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
One question raised during review is what representation these types should have on each platform. Currently they all have the native representation (e.g. As a consequence, however, issues like #29970 come up where the types have the property that basic algebra does not work on them. For example I think that having a |
Typical algebra currently doesn't work on the types in std::time currently (see [this comment][comment]), so tweak the tests to account for this property. [comment]: rust-lang#29866 (comment) Closes rust-lang#29970
Typical algebra currently doesn't work on the types in std::time currently (see [this comment][comment]), so tweak the tests to account for this property. [comment]: #29866 (comment) Closes #29970
Is there intended to be a public constructor for any of these, or make |
It’s a bit convoluted, but you can construct a |
Does |
Yeah as @SimonSapin mentioned construction is intended to not be possible for the opaque |
They're stored as microseconds since 2000, so there would potentially be 2037 problems when converting. |
Ah ok, in that case (especially if there's no extra time zone data or anything like that) I'd just create something like: let microseconds_since_2000 = db_value;
let t2000 = UNIX_EPOCH + thirty_years;
let duration_since_2000 = Duration::new(...);
let date = t2000 + duration_since_2000; |
I added support for this to Diesel in diesel-rs/diesel@dbcc1d2. A note on the API from that -- The lack of negative durations makes constructing arbitrary times really painful, though I understand the reasoning that went into that. However, using |
Very interesting! It's true that the APIs weren't necessarily designed initially with ergonomics as the primary feature, but rather correctness in terms of not omitting various oddities here and there. It's an interesting point though that |
|
cc @wycats |
My original design did indeed have a Signed Duration, but @aturon wanted to @aturon how do you feel now? On Mon, Dec 7, 2015, 6:37 AM Aaron Turon notifications@github.com wrote:
|
In short, the justification is: if the way to convert |
I'm not opposed to a sign duration type, and agree with @SimonSapin that we'll still have achieved the basic goal by making the "default" duration nonnegative. Perhaps we can usefully prototype this out of tree. |
I don’t really like the name Although it would probably have to duplicate much of |
I experimented with converting r2d2 over to using One note I do have is that for whatever reason, I found |
@sfackler Would |
Might be, yeah. |
Greater than or equal to. There is no uniqueness guarantee. |
I'd like to re-raise the issue of renaming Another possibility is to rename |
@danburkert note that the name is |
Sorry about that. But yes, I have the same issues with |
I'm also somewhat sympathetic to naming, specifically if we end up having a couple of clocks we'd probably want them all consistently named. Right now the names |
@alexcrichton @danburkert I'm pretty uneasy with Instead of trying to find a way to make the two types appear to have parity, I'd rather find a name for |
I agree with @wycats here. The name |
This can go both ways. Ultimately you can't save all parties from their own ignorance by anointing one clock or another. It's better to put the two clock types on equal footing, and leave the education up to the documentation.
The two types do have parity. They are both clocks which provide measurements. Neither is perfect, neither is strictly superior, and in any given situation it's likely that only one of them can appropriately be used.
It also implies other properties: steadiness, not jumping forwards, that measurements can be compared across hosts, etc. None of the properties are guaranteed or provided by |
One more thought. A monotonic clock is not sufficient for benchmarking, the clock should also be steady. As a trivial example, a logical clock is monotonic but would be inappropriate for benchmarking. Perhaps the type should be |
although |
Just re-export it from |
I've been using time2 in one of my projects, and I've been getting panics from "overflow when subtracting Durations". Unfortunately I haven't been able to repro this myself, but one of the users of my code has been getting it sporadically. I haven't been able to tell which subtraction could be causing them (they all look fine), and I've yet to get a backtrace back. I think its a bit of an ergonomic issue for subtraction to panic; if we want to disallow negative Durations, then it seems to me that subtraction should return a |
The libs team discussed this during triage yesterday and we reached a few conclusions:
|
Ah, and the other conclusion was to also stabilize everything, which is probably worth mentioning! |
This commit is the result of the FCPs ending for the 1.8 release cycle for both the libs and the lang suteams. The full list of changes are: Stabilized * `braced_empty_structs` * `augmented_assignments` * `str::encode_utf16` - renamed from `utf16_units` * `str::EncodeUtf16` - renamed from `Utf16Units` * `Ref::map` * `RefMut::map` * `ptr::drop_in_place` * `time::Instant` * `time::SystemTime` * `{Instant,SystemTime}::now` * `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier` * `{Instant,SystemTime}::elapsed` * Various `Add`/`Sub` impls for `Time` and `SystemTime` * `SystemTimeError` * `SystemTimeError::duration` * Various impls for `SystemTimeError` * `UNIX_EPOCH` * `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign` Deprecated * Scoped TLS (the `scoped_thread_local!` macro) * `Ref::filter_map` * `RefMut::filter_map` * `RwLockReadGuard::map` * `RwLockWriteGuard::map` * `Condvar::wait_timeout_with` Closes rust-lang#27714 Closes rust-lang#27715 Closes rust-lang#27746 Closes rust-lang#27748 Closes rust-lang#27908 Closes rust-lang#29866
This commit is the result of the FCPs ending for the 1.8 release cycle for both the libs and the lang suteams. The full list of changes are: Stabilized * `braced_empty_structs` * `augmented_assignments` * `str::encode_utf16` - renamed from `utf16_units` * `str::EncodeUtf16` - renamed from `Utf16Units` * `Ref::map` * `RefMut::map` * `ptr::drop_in_place` * `time::Instant` * `time::SystemTime` * `{Instant,SystemTime}::now` * `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier` * `{Instant,SystemTime}::elapsed` * Various `Add`/`Sub` impls for `Time` and `SystemTime` * `SystemTimeError` * `SystemTimeError::duration` * Various impls for `SystemTimeError` * `UNIX_EPOCH` * `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign` Deprecated * Scoped TLS (the `scoped_thread_local!` macro) * `Ref::filter_map` * `RefMut::filter_map` * `RwLockReadGuard::map` * `RwLockWriteGuard::map` * `Condvar::wait_timeout_with` Closes #27714 Closes #27715 Closes #27746 Closes #27748 Closes #27908 Closes #29866 Closes #28235 Closes #29720
This commit is the result of the FCPs ending for the 1.8 release cycle for both the libs and the lang suteams. The full list of changes are: Stabilized * `braced_empty_structs` * `augmented_assignments` * `str::encode_utf16` - renamed from `utf16_units` * `str::EncodeUtf16` - renamed from `Utf16Units` * `Ref::map` * `RefMut::map` * `ptr::drop_in_place` * `time::Instant` * `time::SystemTime` * `{Instant,SystemTime}::now` * `{Instant,SystemTime}::duration_since` - renamed from `duration_from_earlier` * `{Instant,SystemTime}::elapsed` * Various `Add`/`Sub` impls for `Time` and `SystemTime` * `SystemTimeError` * `SystemTimeError::duration` * Various impls for `SystemTimeError` * `UNIX_EPOCH` * `ops::{Add,Sub,Mul,Div,Rem,BitAnd,BitOr,BitXor,Shl,Shr}Assign` Deprecated * Scoped TLS (the `scoped_thread_local!` macro) * `Ref::filter_map` * `RefMut::filter_map` * `RwLockReadGuard::map` * `RwLockWriteGuard::map` * `Condvar::wait_timeout_with` Closes rust-lang#27714 Closes rust-lang#27715 Closes rust-lang#27746 Closes rust-lang#27748 Closes rust-lang#27908 Closes rust-lang#29866
Tracking issue for rust-lang/rfcs#1288
The text was updated successfully, but these errors were encountered: