datetime/
instant.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Exact points on a timeline.

use std::fmt;
use std::ops::{Add, Sub};

use system::sys_time;
use duration::Duration;


/// An **instant** is an exact point on the timeline, irrespective of time
/// zone or calendar format, with millisecond precision.
///
/// Internally, this is represented by a 64-bit integer of seconds, and a
/// 16-bit integer of milliseconds. This means that it will overflow (and thus
/// be unsuitable for) instants past GMT 15:30:08, Sunday 4th December,
/// 292,277,026,596 (yes, that’s a year)
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct Instant {
    seconds: i64,
    milliseconds: i16,
}

impl Instant {

    /// Creates a new Instant set to the number of seconds since the Unix
    /// epoch, and zero milliseconds.
    pub fn at(seconds: i64) -> Self {
        Self::at_ms(seconds, 0)
    }

    /// Creates a new Instant set to the number of seconds since the
    /// Unix epoch, along with the number of milliseconds so far this
    /// second.
    pub fn at_ms(seconds: i64, milliseconds: i16) -> Self {
        Self { seconds, milliseconds }
    }

    /// Creates a new Instant set to the computer’s current time.
    #[cfg_attr(target_os = "redox", allow(unused_unsafe))]
    pub fn now() -> Self {
        let (seconds, milliseconds) = unsafe { sys_time() };
        Self { seconds, milliseconds }
    }

    /// Creates a new Instant set to the Unix epoch.
    pub fn at_epoch() -> Self {
        Self::at(0)
    }

    /// Returns the number of seconds at this instant
    pub fn seconds(&self) -> i64 {
        self.seconds
    }

    /// Returns the number of milliseconds at this instant
    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,
        }
    }
}