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
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors
//!
//! Contains the concept of an [`Epoch`] - a specific Proleptic Gregorian [`Date`] from which a
//! [`Timestamp`] is measured against.
//!
//! A [`Timestamp`] is a [`Duration`], a physical amount of time measured against an [`Epoch`]
//!
use std::marker::PhantomData;
use irox_units::units::duration::Duration;
use crate::gregorian::Date;
///
/// An `Epoch` serves as a reference point from which time is measured.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Epoch(pub Date);
impl Epoch {
///
/// The Gregorian Date of this particular Epoch.
pub fn get_gregorian_date(&self) -> Date {
self.0
}
}
///
/// Represents a [`Duration`] offset from a particular [`Epoch`]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Timestamp<T> {
epoch: Epoch,
offset: Duration,
_phantom: PhantomData<T>,
}
impl<T> Timestamp<T> {
///
/// Returns the base epoch for this timestamp
#[must_use]
pub fn get_epoch(&self) -> Epoch {
self.epoch
}
///
/// Returns the relative offset of this timestamp from the specified epoch.
#[must_use]
pub fn get_offset(&self) -> Duration {
self.offset
}
}
///
/// The Unix Epoch, 1970-01-01, 00:00:00
pub const UNIX_EPOCH: Epoch = Epoch(Date {
year: 1970,
day_of_year: 0,
});
///
/// Represents a duration offset from the [`UNIX_EPOCH`].
pub type UnixTimestamp = Timestamp<UnixEpoch>;
/// `UnixEpoch` is a compile-time check for [`UnixTimestamp`] = [`Timestamp<UnixEpoch>`]
pub struct UnixEpoch;
macro_rules! derive_timestamp_impl {
($epoch:ident,$name:ident) => {
impl $name {
///
/// Creates a new timestamp given the specified offset
pub const fn from_offset(offset: Duration) -> $name {
$name {
epoch: $epoch,
offset,
_phantom: PhantomData {},
}
}
///
/// Creates a new timestamp given the specified number of seconds
pub const fn from_seconds(seconds: u32) -> $name {
$name::from_seconds_f64(seconds as f64)
}
///
/// Creates a new timestamp given the specified number of fractional seconds
pub const fn from_seconds_f64(seconds: f64) -> $name {
$name::from_offset(Duration::new_seconds(seconds))
}
}
impl Default for $name {
fn default() -> Self {
$name {
epoch: $epoch,
offset: Duration::default(),
_phantom: Default::default(),
}
}
}
impl From<Duration> for $name {
fn from(value: Duration) -> Self {
$name::from_offset(value)
}
}
};
}
impl UnixTimestamp {
///
/// Returns the local system clock equivalent of the unix timestamp
#[must_use]
pub fn now() -> UnixTimestamp {
match std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
Ok(t) => UnixTimestamp::from_offset(t.into()),
Err(t) => {
UnixTimestamp::from_offset(Duration::new_seconds(-1.0 * t.duration().as_secs_f64()))
}
}
}
///
/// Returns this timestamp as a Date
#[must_use]
pub fn as_date(&self) -> Date {
self.into()
}
}
derive_timestamp_impl!(UNIX_EPOCH, UnixTimestamp);
///
/// The GPS Epoch, 1980-01-06, 00:00:00
pub const GPS_EPOCH: Epoch = Epoch(Date {
year: 1980,
day_of_year: 5,
});
///
/// Represents a duration offset from the [`GPS_EPOCH`]
pub type GPSTimestamp = Timestamp<GPSEpoch>;
/// `GPSEpoch` is a compile-time check for [`GPSTimestamp`] = [`Timestamp<GPSEpoch>`]
pub struct GPSEpoch;
derive_timestamp_impl!(GPS_EPOCH, GPSTimestamp);
///
/// The Gregorian Epoch, 15-OCT-1582
pub const GREGORIAN_EPOCH: Epoch = Epoch(Date {
year: 1582,
day_of_year: 287,
});
///
/// Represents a duration offset from the [`GREGORIAN_EPOCH`]
pub type GregorianTimestamp = Timestamp<GregorianEpoch>;
/// `GregorianEpoch` is a compile-time check for [`GregorianTimestamp`] = [`Timestamp<GregorianEpoch>`]
pub struct GregorianEpoch;
derive_timestamp_impl!(GREGORIAN_EPOCH, GregorianTimestamp);
///
/// The Windows NT Epoch, 01-JAN-1601.
///
/// Why this date? The Gregorian calendar operates on a 400-year cycle, and
/// 1601 is the first year of the cycle that was active at the time Windows NT
/// was being designed. In other words, it was chosen to make the math come out
/// nicely.
pub const WINDOWS_NT_EPOCH: Epoch = Epoch(Date {
year: 1601,
day_of_year: 0,
});
///
/// Represents a duration offset from the [`WINDOWS_NT_EPOCH`]
///
/// Note: when a duration is actually retrieved from the windows FILETIME
/// routines, it comes back in 100-nanosecond increments from this epoch.
pub type WindowsNTTimestamp = Timestamp<WindowsEpoch>;
/// `WindowsEpoch` is a compile-time check for [`WindowsNTTimestamp`] = [`Timestamp<WindowsEpoch>`]
pub struct WindowsEpoch;
derive_timestamp_impl!(WINDOWS_NT_EPOCH, WindowsNTTimestamp);
///
/// The Common Era Epoch, 01-JAN-0001 AD
pub const COMMON_ERA_EPOCH: Epoch = Epoch(Date {
year: 1,
day_of_year: 0,
});
///
/// The Prime Epoch, 01-JAN-1900
pub const PRIME_EPOCH: Epoch = Epoch(Date {
year: 1900,
day_of_year: 0,
});
///
/// The NTP epoch is the same as the [`PRIME_EPOCH`]
pub const NTP_EPOCH: Epoch = PRIME_EPOCH;