-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
std: Add Instant and SystemTime to std::time #29894
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
573eb3d
to
6651a6f
Compare
☔ The latest upstream changes (presumably #29083) made this pull request unmergeable. Please resolve the merge conflicts. |
6651a6f
to
efd9a8b
Compare
r? @brson |
lgtm after a quick pass |
The The implementations contain plattform-specific types, none of which look like the |
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.
efd9a8b
to
c6eb852
Compare
I personally prefer to retain the platform-specific representation for as long as possible so I opted to avoid the cross-platform representation via the secs/nanos repr, but this does have the downside of different |
I'm also not too worried about cross-platform |
I have some concern that math on @bors r+ |
📌 Commit c6eb852 has been approved by |
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
@brson I was concerned about overflow behavior as well when making the libtime version of SteadyTime. From looking at boost/C++11's implementations, it seems like everyone assumes that overflow doesn't occur. Windows guarantees that overflow won't happen for at least a century or two of uptime, and I bet the same is roughly true in practice for OSX and Linux. Leaving time information in the native formats for as long as possible should help as well with overflow behavior. |
This commit is an implementation of RFC 1288 which adds two new unstable
types to the
std::time
module. TheInstant
type is used to representmeasurements 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 areprogrammer errors rather than typical errors that can be encountered.
The
SystemTime
type is used to represent a system timestamp and is notmonotonic. 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 therelative distance from this point for any particular time stamp.
This PR takes the same implementation strategy as the
time
crate on crates.io,namely:
These implementations can perhaps be refined over time, but they currently
satisfy the requirements of the
Instant
andSystemTime
types while alsobeing portable across implementations and revisions of each platform.
cc #29866