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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use chrono::prelude::*;
use chrono_tz::Tz;

use crate::time::chrono::{Offset, Time};
use crate::NANOS_IN_SECOND;

impl Time {
    /// Returns the hour of the day `0..=23` for _time_.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let now = Time::now();
    /// let hour_of_day = now.hour();
    /// ```
    #[inline]
    #[must_use]
    pub fn hour(self) -> u32 {
        let Self {
            timestamp,
            sub_second_nanos,
            offset,
        } = self;
        let naive = NaiveDateTime::from_timestamp(timestamp, sub_second_nanos);
        match offset {
            Offset::Utc => {
                let aware = DateTime::<Utc>::from_utc(naive, Utc);
                aware.hour()
            }
            Offset::Local => {
                let offset = Local.offset_from_utc_datetime(&naive);
                let aware = DateTime::<Local>::from_utc(naive, offset);
                aware.hour()
            }
            Offset::Tz(timezone) => {
                let offset = timezone.offset_from_utc_datetime(&naive);
                let aware = DateTime::<Tz>::from_utc(naive, offset);
                aware.hour()
            }
            Offset::Fixed(offset) => {
                let aware = DateTime::<FixedOffset>::from_utc(naive, offset);
                aware.hour()
            }
        }
    }

    /// Returns the minute of the hour `0..=59` for _time_.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let now = Time::now();
    /// let minute_of_hour = now.minute();
    /// ```
    #[inline]
    #[must_use]
    pub fn minute(self) -> u32 {
        let Self {
            timestamp,
            sub_second_nanos,
            offset,
        } = self;
        let naive = NaiveDateTime::from_timestamp(timestamp, sub_second_nanos);
        match offset {
            Offset::Utc => {
                let aware = DateTime::<Utc>::from_utc(naive, Utc);
                aware.minute()
            }
            Offset::Local => {
                let offset = Local.offset_from_utc_datetime(&naive);
                let aware = DateTime::<Local>::from_utc(naive, offset);
                aware.minute()
            }
            Offset::Tz(timezone) => {
                let offset = timezone.offset_from_utc_datetime(&naive);
                let aware = DateTime::<Tz>::from_utc(naive, offset);
                aware.minute()
            }
            Offset::Fixed(offset) => {
                let aware = DateTime::<FixedOffset>::from_utc(naive, offset);
                aware.minute()
            }
        }
    }

    /// Returns the second of the minute `0..=60` for _time_.
    ///
    /// Seconds range from zero to 60 to allow the system to inject [leap
    /// seconds].
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let now = Time::now();
    /// let second_of_minute = now.second();
    /// if second_of_minute >= 60 {
    ///     // `now` is during a leap second
    /// }
    /// ```
    ///
    /// [leap seconds]: https://en.wikipedia.org/wiki/Leap_second
    #[inline]
    #[must_use]
    pub fn second(self) -> u32 {
        let Self {
            timestamp,
            sub_second_nanos,
            offset,
        } = self;
        let naive = NaiveDateTime::from_timestamp(timestamp, sub_second_nanos);
        let second = match offset {
            Offset::Utc => {
                let aware = DateTime::<Utc>::from_utc(naive, Utc);
                aware.second()
            }
            Offset::Local => {
                let offset = Local.offset_from_utc_datetime(&naive);
                let aware = DateTime::<Local>::from_utc(naive, offset);
                aware.second()
            }
            Offset::Tz(timezone) => {
                let offset = timezone.offset_from_utc_datetime(&naive);
                let aware = DateTime::<Tz>::from_utc(naive, offset);
                aware.second()
            }
            Offset::Fixed(offset) => {
                let aware = DateTime::<FixedOffset>::from_utc(naive, offset);
                aware.second()
            }
        };
        // `chrono` stores leap seconds in the `sub_second_nanos` field.
        // Normalize so nanos is `0..1_000_000_000`.
        if sub_second_nanos >= NANOS_IN_SECOND {
            second + 1
        } else {
            second
        }
    }

    /// Returns the number of microseconds for _time_.
    ///
    /// This method returns microseconds since the last second (including [leap
    /// seconds]. The range of this value is always from 0 to 999,999.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let now = Time::now();
    /// let usec_since_last_second = now.second();
    /// if usec_since_last_second >= 1_000_000 {
    ///     // `now` is during a leap second
    /// }
    /// ```
    ///
    /// [leap seconds]: https://en.wikipedia.org/wiki/Leap_second
    #[inline]
    #[must_use]
    pub const fn microsecond(self) -> u32 {
        self.sub_second_nanos / 1_000
    }

    /// Returns the number of nanoseconds for _time_.
    ///
    /// This method returns nanoseconds since the last second (including [leap
    /// seconds]. The range of this value is always from 0 to 999,999,999.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let now = Time::now();
    /// let nsec_since_last_second = now.nanosecond();
    /// ```
    ///
    /// # Implementation notes
    ///
    /// The IEEE 754 double is not accurate enough to represent the exact number
    /// of nanoseconds since the Unix Epoch. [`nanosecond`](Self::nanosecond) is
    /// more accurate than [`to_float`](Self::to_float).
    ///
    /// [leap seconds]: https://en.wikipedia.org/wiki/Leap_second
    #[inline]
    #[must_use]
    pub fn nanosecond(self) -> u32 {
        let Self { sub_second_nanos, .. } = self;
        // `chrono` stores leap seconds in the `sub_second_nanos` field.
        // Normalize so nanos is `0..1_000_000_000`.
        sub_second_nanos
            .checked_sub(NANOS_IN_SECOND)
            .unwrap_or(sub_second_nanos)
    }

    /// Returns the fraction for time.
    ///
    /// The return value can be a rational number. This result is more accurate
    /// than [`to_float`](Self::to_float) because IEEE 754 double precision is
    /// not sufficient to losslessly encode nanosecond fractions of seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use spinoso_time::Time;
    /// let time = Time::now();
    /// let (sub_second_units, units_per_second) = time.subsec();
    /// ```
    ///
    /// # Implementation notes
    ///
    /// Time has a maximum granularity of nanoseconds, so in practice this
    /// method always returns nanoseconds, but the returned tuple accomodates
    /// returning any fractional part, such as millis or micros.
    #[inline]
    #[must_use]
    pub const fn subsec(self) -> (u32, u32) {
        (self.sub_second_nanos, NANOS_IN_SECOND)
    }
}