From d4774180011119a00fce909e1102f45ef3a27c2c Mon Sep 17 00:00:00 2001 From: Botahamec Date: Mon, 21 Mar 2022 10:18:46 -0400 Subject: Convert DateTime to TAI timestamp --- src/datetime.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/datetime.rs') 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 DateTime { } pub fn offset(&self) -> UtcOffset { - let utc = DateTime::::from_utc(self.utc_datetime, Utc); + let utc = self.as_utc(); self.timezone.utc_offset(utc) } @@ -41,6 +42,29 @@ impl DateTime { 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 { + DateTime::::from_utc(self.utc_datetime, Utc) + } + + pub fn as_tai(&self) -> DateTime { + DateTime::::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 { -- cgit v1.2.3