Skip to content

Document that SystemTime does not count leap seconds #109660

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

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/std/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ pub struct Instant(time::Instant);
/// The size of a `SystemTime` struct may vary depending on the target operating
/// system.
///
/// A `SystemTime` does not count leap seconds.
/// `SystemTime::now()`'s behaviour around a leap second
/// is the same as the operating system's wall clock.
/// The precise behaviour near a leap second
/// (e.g. whether the clock appears to run slow or fast, or stop, or jump)
/// depends on platform and configuration,
/// so should not be relied on.
///
/// Example:
///
/// ```no_run
Expand Down Expand Up @@ -461,13 +469,22 @@ impl fmt::Debug for Instant {
impl SystemTime {
/// An anchor in time which can be used to create new `SystemTime` instances or
/// learn about where in time a `SystemTime` lies.
//
// NOTE! this documentation is duplicated, here and in std::time::UNIX_EPOCH.
// The two copies are not quite identical, because of the difference in naming.
///
/// This constant is defined to be "1970-01-01 00:00:00 UTC" on all systems with
/// respect to the system clock. Using `duration_since` on an existing
/// `SystemTime` instance can tell how far away from this point in time a
/// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
/// `SystemTime` instance to represent another fixed point in time.
///
/// `duration_since(UNIX_EPOCH).unwrap().as_secs()`
/// returns a POSIX `time_t` (as a `u64`):
/// the number of non-leap seconds since the start of 1970 UTC.
/// This is the same time representation as used in many Internet protocols,
/// for example: JWT, CBOR, TOTP, certificate transparency and DNS TSIG/DNSSEC.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think enumerating these probably is over the line for too much documentation, since it might lead people to file PRs adding other protocols. I also would prefer saying "This is $description, same as $POSIX_equivalent" rather than the other way around.

So my suggesion would be for this (and the other) comment to be more like:

duration_since(UNIX_EPOCH).unwrap().as_secs() returns the number of non-leap seconds since the start of 1970 UTC, as a u64. This is the same time representation as used in POSIX time_t, as well as a wide variety of internet protocols.

(Note: I'm not tied to precise wording except for the bits I mentioned above). Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with these suggestions and have adopted them. Thanks.

///
/// # Examples
///
/// ```no_run
Expand Down Expand Up @@ -617,13 +634,22 @@ impl fmt::Debug for SystemTime {

/// An anchor in time which can be used to create new `SystemTime` instances or
/// learn about where in time a `SystemTime` lies.
//
// NOTE! this documentation is duplicated, here and in SystemTime::UNIX_EPOCH.
// The two copies are not quite identical, because of the difference in naming.
///
/// This constant is defined to be "1970-01-01 00:00:00 UTC" on all systems with
/// respect to the system clock. Using `duration_since` on an existing
/// [`SystemTime`] instance can tell how far away from this point in time a
/// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
/// [`SystemTime`] instance to represent another fixed point in time.
///
/// `duration_since(UNIX_EPOCH).unwrap().as_secs()`
/// returns a POSIX `time_t` (as a `u64`):
/// the number of non-leap seconds since the start of 1970 UTC.
/// This is the same time representation as used in many Internet protocols,
/// for example: JWT, CBOR, TOTP, certificate transparency and DNS TSIG/DNSSEC.
///
/// # Examples
///
/// ```no_run
Expand Down