Skip to content

Commit e38eca6

Browse files
committed
Auto merge of #49501 - sfackler:unix-epoch-assoc-const, r=alexcrichton
Make UNIX_EPOCH an associated constant of SystemTime It's not very discoverable as a separate const in the module. r? @alexcrichton
2 parents 70248b1 + df2e238 commit e38eca6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libstd/time.rs

+23
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,29 @@ impl fmt::Debug for Instant {
259259
}
260260

261261
impl SystemTime {
262+
/// An anchor in time which can be used to create new `SystemTime` instances or
263+
/// learn about where in time a `SystemTime` lies.
264+
///
265+
/// This constant is defined to be "1970-01-01 00:00:00 UTC" on all systems with
266+
/// respect to the system clock. Using `duration_since` on an existing
267+
/// `SystemTime` instance can tell how far away from this point in time a
268+
/// measurement lies, and using `UNIX_EPOCH + duration` can be used to create a
269+
/// `SystemTime` instance to represent another fixed point in time.
270+
///
271+
/// # Examples
272+
///
273+
/// ```no_run
274+
/// #![feature(assoc_unix_epoch)]
275+
/// use std::time::SystemTime;
276+
///
277+
/// match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
278+
/// Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
279+
/// Err(_) => panic!("SystemTime before UNIX EPOCH!"),
280+
/// }
281+
/// ```
282+
#[unstable(feature = "assoc_unix_epoch", issue = "49502")]
283+
pub const UNIX_EPOCH: SystemTime = UNIX_EPOCH;
284+
262285
/// Returns the system time corresponding to "now".
263286
///
264287
/// # Examples

0 commit comments

Comments
 (0)