1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use sys::{TimeSysCall, TimeClock};
14 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
15 pub struct Instant(Duration);
17 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
18 pub struct SystemTime(Duration);
20 pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
23 pub fn now() -> Instant {
24 Instant(TimeSysCall::perform(TimeClock::Monotonic))
27 pub fn sub_instant(&self, other: &Instant) -> Duration {
31 pub fn add_duration(&self, other: &Duration) -> Instant {
32 Instant(self.0 + *other)
35 pub fn sub_duration(&self, other: &Duration) -> Instant {
36 Instant(self.0 - *other)
41 pub fn now() -> SystemTime {
42 SystemTime(TimeSysCall::perform(TimeClock::System))
45 pub fn sub_time(&self, other: &SystemTime)
46 -> Result<Duration, Duration> {
47 self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
50 pub fn add_duration(&self, other: &Duration) -> SystemTime {
51 SystemTime(self.0 + *other)
54 pub fn sub_duration(&self, other: &Duration) -> SystemTime {
55 SystemTime(self.0 - *other)