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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
// 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 core::marker::PhantomData;
use core::ops::{Add, AddAssign, Sub, SubAssign};
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> {
pub(crate) fn new(epoch: Epoch, duration: Duration) -> Self {
Self {
epoch,
offset: duration,
_phantom: PhantomData,
}
}
///
/// 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>`]
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub struct UnixEpoch;
pub trait FromTimestamp<T> {
fn from_timestamp(other: &Timestamp<T>) -> Self;
}
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<T> FromTimestamp<T> for $name {
fn from_timestamp(other: &Timestamp<T>) -> Self {
let epoch_offset = $epoch.0 - other.epoch.0;
let new_duration = other.offset - epoch_offset;
$name::from_offset(new_duration)
}
}
};
}
impl<T> Add<Duration> for Timestamp<T> {
type Output = Timestamp<T>;
fn add(self, rhs: Duration) -> Self::Output {
let offset = self.offset + rhs;
Self::new(self.epoch, offset)
}
}
impl<T> AddAssign<Duration> for Timestamp<T> {
fn add_assign(&mut self, rhs: Duration) {
self.offset += rhs;
}
}
impl<T> Sub<Duration> for Timestamp<T> {
type Output = Timestamp<T>;
fn sub(self, rhs: Duration) -> Self::Output {
let offset = self.offset - rhs;
Self::new(self.epoch, offset)
}
}
impl<T> SubAssign<Duration> for Timestamp<T> {
fn sub_assign(&mut self, rhs: Duration) {
self.offset -= rhs;
}
}
impl<T> Add<&Duration> for Timestamp<T> {
type Output = Timestamp<T>;
fn add(self, rhs: &Duration) -> Self::Output {
let offset = self.offset + *rhs;
Self::new(self.epoch, offset)
}
}
impl<T> AddAssign<&Duration> for Timestamp<T> {
fn add_assign(&mut self, rhs: &Duration) {
self.offset += *rhs;
}
}
impl<T> Sub<&Duration> for Timestamp<T> {
type Output = Timestamp<T>;
fn sub(self, rhs: &Duration) -> Self::Output {
let offset = self.offset - *rhs;
Self::new(self.epoch, offset)
}
}
impl<T> SubAssign<&Duration> for Timestamp<T> {
fn sub_assign(&mut self, rhs: &Duration) {
self.offset -= *rhs;
}
}
impl<T> Sub<Timestamp<T>> for Timestamp<T> {
type Output = Duration;
fn sub(self, rhs: Timestamp<T>) -> Self::Output {
self.offset - rhs.offset
}
}
impl<T> Sub<&Timestamp<T>> for Timestamp<T> {
type Output = Duration;
fn sub(self, rhs: &Timestamp<T>) -> Self::Output {
self.offset - rhs.offset
}
}
impl UnixTimestamp {
///
/// Returns the local system clock equivalent of the unix timestamp
#[must_use]
#[cfg(feature = "std")]
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 the local system clock duration since the timestamp. MAY BE NEGATIVE if the clock
/// has changed since the last call.
#[must_use]
#[cfg(feature = "std")]
pub fn elapsed(&self) -> Duration {
Self::now().offset - self.offset
}
///
/// 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>`]
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
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>`]
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
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>`]
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
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,
});
///
/// 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 PrimeTimestamp = Timestamp<PrimeEpoch>;
/// `PrimeEpoch` is a compile-time check for [`PrimeTimestamp`] = [`Timestamp<crate::epoch::PrimeEpoch>`]
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub struct PrimeEpoch;
derive_timestamp_impl!(PRIME_EPOCH, PrimeTimestamp);
///
/// The NTP epoch is the same as the [`PRIME_EPOCH`]
pub const NTP_EPOCH: Epoch = PRIME_EPOCH;