diff options
| author | Botahamec <botahamec@outlook.com> | 2022-03-21 10:18:46 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2022-03-21 10:18:46 -0400 |
| commit | d4774180011119a00fce909e1102f45ef3a27c2c (patch) | |
| tree | 41da7c4afc689546201e3cda0331119e9e8118a3 /src | |
| parent | db9f20681f90f206d393d2b06a6a8401515ff562 (diff) | |
Convert DateTime to TAI timestamp
Diffstat (limited to 'src')
| -rw-r--r-- | src/datetime.rs | 26 | ||||
| -rw-r--r-- | src/timezone.rs | 12 |
2 files changed, 31 insertions, 7 deletions
diff --git a/src/datetime.rs b/src/datetime.rs index 931d023..a46d0b7 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -1,5 +1,6 @@ use crate::{ date::{DayGreaterThanMaximumForMonthError, LeapDayNotInLeapYearError}, + tai::Tai, timezone::{Utc, UtcOffset}, Date, Month, Time, TimeZone, UnixTimestamp, Year, }; @@ -30,7 +31,7 @@ impl<Tz: TimeZone> DateTime<Tz> { } pub fn offset(&self) -> UtcOffset { - let utc = DateTime::<Utc>::from_utc(self.utc_datetime, Utc); + let utc = self.as_utc(); self.timezone.utc_offset(utc) } @@ -41,6 +42,29 @@ impl<Tz: TimeZone> DateTime<Tz> { pub fn naive_utc(&self) -> NaiveDateTime { self.utc_datetime } + + pub fn to_naive_overflowing(&self) -> (NaiveDateTime, bool) { + self.utc_datetime + .add_seconds_overflowing(self.offset().seconds_ahead().into()) + } + + pub fn as_utc(&self) -> DateTime<Utc> { + DateTime::<Utc>::from_utc(self.utc_datetime, Utc) + } + + pub fn as_tai(&self) -> DateTime<Tai> { + DateTime::<Tai>::from_utc(self.utc_datetime, Tai) + } + + pub fn unix_timestamp(&self) -> UnixTimestamp { + self.utc_datetime.timestamp() + } + + // TODO rethink the name of UnixTimestamp + // TODO should this overflow? + pub fn tai_timestamp(&self) -> UnixTimestamp { + self.as_tai().to_naive_overflowing().0.timestamp() + } } impl NaiveDateTime { diff --git a/src/timezone.rs b/src/timezone.rs index 6f356ca..f0096c6 100644 --- a/src/timezone.rs +++ b/src/timezone.rs @@ -52,8 +52,8 @@ impl UtcOffset { pub const UTC: Self = Self { offset_seconds: 0 }; /// Makes a new `UtcOffset` timezone with the given timezone difference. - /// A positive number is the Eastern hemisphere. A negative number is the - /// Western hemisphere. + /// A positive number is the Eastern hemisphere. A negative number behind + /// UTC, such as UTC-5. #[must_use] pub const fn from_seconds(seconds: i32) -> Self { Self { @@ -62,22 +62,22 @@ impl UtcOffset { } /// Makes a new `UtcOffset` timezone with the given timezone difference. - /// A positive number is the Eastern hemisphere. A negative number is the - /// Western hemisphere. + /// A positive number is the Eastern hemisphere. A negative number is + /// behind UTC, such as UTC-5. #[must_use] pub const fn from_hours(hours: i32) -> Self { Self::from_seconds(hours * 3600) } /// The number of hours this timezone is ahead of UTC. This number is - /// negative if the timezone is in the Western hemisphere + /// negative if the timezone is behind UTC, such as UTC-5. #[must_use] pub fn hours_ahead(self) -> f32 { self.offset_seconds as f32 / 3600.0 } /// The number of seconds this timezone is ahead of UTC. This number is - /// negative if the timezone is in the Western hemisphere + /// negative if the timezone is behind UTC, such as UTC-5. #[must_use] pub const fn seconds_ahead(self) -> i32 { self.offset_seconds |
