use std::fmt;
use std::ops::{Add, Sub};
use system::sys_time;
use duration::Duration;
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct Instant {
seconds: i64,
milliseconds: i16,
}
impl Instant {
pub fn at(seconds: i64) -> Self {
Self::at_ms(seconds, 0)
}
pub fn at_ms(seconds: i64, milliseconds: i16) -> Self {
Self { seconds, milliseconds }
}
#[cfg_attr(target_os = "redox", allow(unused_unsafe))]
pub fn now() -> Self {
let (seconds, milliseconds) = unsafe { sys_time() };
Self { seconds, milliseconds }
}
pub fn at_epoch() -> Self {
Self::at(0)
}
pub fn seconds(&self) -> i64 {
self.seconds
}
pub fn milliseconds(&self) -> i16 {
self.milliseconds
}
}
impl fmt::Debug for Instant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Instant({}s/{}ms)", self.seconds, self.milliseconds)
}
}
impl Add<Duration> for Instant {
type Output = Self;
fn add(self, duration: Duration) -> Self {
let (seconds, milliseconds) = duration.lengths();
Self {
seconds: self.seconds + seconds,
milliseconds: self.milliseconds + milliseconds,
}
}
}
impl Sub<Duration> for Instant {
type Output = Self;
fn sub(self, duration: Duration) -> Self {
let (seconds, milliseconds) = duration.lengths();
Self {
seconds: self.seconds - seconds,
milliseconds: self.milliseconds - milliseconds,
}
}
}