summaryrefslogtreecommitdiff
path: root/src/datetime.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-03-21 10:18:46 -0400
committerBotahamec <botahamec@outlook.com>2022-03-21 10:18:46 -0400
commitd4774180011119a00fce909e1102f45ef3a27c2c (patch)
tree41da7c4afc689546201e3cda0331119e9e8118a3 /src/datetime.rs
parentdb9f20681f90f206d393d2b06a6a8401515ff562 (diff)
Convert DateTime to TAI timestamp
Diffstat (limited to 'src/datetime.rs')
-rw-r--r--src/datetime.rs26
1 files changed, 25 insertions, 1 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 {