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
//! Canonical epoch type and the civil UTC representation.
use core::ops::{Add, Sub};
use crate::duration::Duration;
use crate::math;
/// A continuous TAI epoch.
///
/// The value is stored as whole TAI seconds plus nanoseconds since
/// `2000-01-01T00:00:00 TAI`. TAI has no leap seconds, so this
/// representation is monotonic and suitable for arithmetic.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Epoch {
sec: i64,
nanos: u32,
}
impl Epoch {
/// Builds an epoch from TAI seconds and nanoseconds since
/// `2000-01-01T00:00:00 TAI`, normalising the nanosecond field.
///
/// Panics when the result cannot be represented. Use
/// [`Epoch::checked_from_parts`] for untrusted input.
pub fn from_parts(sec: i64, nanos: i64) -> Self {
Self::checked_from_parts(sec, nanos).expect("epoch overflow")
}
/// Checked form of [`Epoch::from_parts`].
pub fn checked_from_parts(sec: i64, nanos: i64) -> Option<Self> {
let total = (sec as i128)
.checked_mul(1_000_000_000)?
.checked_add(nanos as i128)?;
Self::from_nanoseconds(total)
}
/// Builds an epoch from a floating-point TAI second count.
///
/// Panics on a non-finite or out-of-range value. Use
/// [`Epoch::checked_from_tai_seconds`] for untrusted input.
pub fn from_tai_seconds(seconds: f64) -> Self {
Self::checked_from_tai_seconds(seconds).expect("invalid or out-of-range epoch")
}
/// Checked form of [`Epoch::from_tai_seconds`].
pub fn checked_from_tai_seconds(seconds: f64) -> Option<Self> {
if !seconds.is_finite() {
return None;
}
let whole = math::floor(seconds);
if whole < i64::MIN as f64 || whole >= i64::MAX as f64 {
return None;
}
let frac = seconds - whole;
let nanos = math::round(frac * 1_000_000_000.0) as i64;
Self::checked_from_parts(whole as i64, nanos)
}
/// Returns the TAI second count as a floating-point value.
pub fn tai_seconds(&self) -> f64 {
self.sec as f64 + self.nanos as f64 * 1e-9
}
/// Returns the whole TAI seconds since the internal origin.
pub fn sec(&self) -> i64 {
self.sec
}
/// Returns the nanosecond remainder.
pub fn nanos(&self) -> u32 {
self.nanos
}
/// Checked addition of a duration.
pub fn checked_add(self, rhs: Duration) -> Option<Self> {
Self::from_nanoseconds(
self.total_nanoseconds()
.checked_add(rhs.total_nanoseconds())?,
)
}
/// Checked subtraction of a duration.
pub fn checked_sub(self, rhs: Duration) -> Option<Self> {
Self::from_nanoseconds(
self.total_nanoseconds()
.checked_sub(rhs.total_nanoseconds())?,
)
}
fn total_nanoseconds(&self) -> i128 {
self.sec as i128 * 1_000_000_000 + self.nanos as i128
}
fn from_nanoseconds(total: i128) -> Option<Self> {
let sec = total.div_euclid(1_000_000_000);
let nanos = total.rem_euclid(1_000_000_000);
if sec < i64::MIN as i128 || sec > i64::MAX as i128 {
return None;
}
Some(Self {
sec: sec as i64,
nanos: nanos as u32,
})
}
}
/// A civil UTC calendar representation.
///
/// `second` is normally in `0.0 .. 60.0`. On a positive-leap-second day
/// it may be in `60.0 .. 61.0`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct UtcDateTime {
/// Astronomical year numbering (1 BC is year 0).
pub year: i32,
/// Month in `1..=12`.
pub month: u8,
/// Day of month.
pub day: u8,
/// Hour in `0..=23`.
pub hour: u8,
/// Minute in `0..=59`.
pub minute: u8,
/// Seconds, including the fractional part.
pub second: f64,
}
impl UtcDateTime {
/// Creates a UTC representation. Range validation is performed by the
/// conversion routines, because the legal day of month depends on the
/// selected calendar and the leap-second table.
pub fn new(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: f64) -> Self {
Self {
year,
month,
day,
hour,
minute,
second,
}
}
}
impl Add<Duration> for Epoch {
type Output = Epoch;
fn add(self, rhs: Duration) -> Self::Output {
self.checked_add(rhs).expect("epoch overflow")
}
}
impl Sub<Duration> for Epoch {
type Output = Epoch;
fn sub(self, rhs: Duration) -> Self::Output {
self.checked_sub(rhs).expect("epoch overflow")
}
}
impl Sub<Epoch> for Epoch {
type Output = Duration;
fn sub(self, rhs: Epoch) -> Self::Output {
let total = self
.total_nanoseconds()
.checked_sub(rhs.total_nanoseconds())
.expect("duration overflow");
Duration::from_nanoseconds(total).expect("duration overflow")
}
}