From dfde231ecaa480e9683d3a54f2182587dc747a56 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 30 Mar 2018 12:01:36 +0200 Subject: [PATCH 1/2] Make UNIX_EPOCH an associated constant of SystemTime It's not very discoverable as a separate const in the module. --- src/libstd/time.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index 7256ac43e27e4..aeaa304627441 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -259,6 +259,28 @@ 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. + /// + /// 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. + /// + /// # Examples + /// + /// ```no_run + /// use std::time::SystemTime; + /// + /// match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { + /// Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()), + /// Err(_) => panic!("SystemTime before UNIX EPOCH!"), + /// } + /// ``` + #[unstable(feature = "assoc_unix_epoch", issue = "49502")] + pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH; + /// Returns the system time corresponding to "now". /// /// # Examples From df2e238d1aae617f75424a2928d35cc39fae0fa0 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 30 Mar 2018 15:35:36 +0200 Subject: [PATCH 2/2] Fix doctest --- src/libstd/time.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libstd/time.rs b/src/libstd/time.rs index aeaa304627441..1215302757c31 100644 --- a/src/libstd/time.rs +++ b/src/libstd/time.rs @@ -271,6 +271,7 @@ impl SystemTime { /// # Examples /// /// ```no_run + /// #![feature(assoc_unix_epoch)] /// use std::time::SystemTime; /// /// match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {