diff options
| -rw-r--r-- | src/datetime.rs | 33 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/timestamp.rs | 12 |
3 files changed, 23 insertions, 24 deletions
diff --git a/src/datetime.rs b/src/datetime.rs index 49506e3..a374d3d 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -2,7 +2,7 @@ use crate::{ date::{DayGreaterThanMaximumForMonthError, LeapDayNotInLeapYearError}, tai::Tai, timezone::{Utc, UtcOffset}, - Date, Month, Time, TimeZone, UnixTimestamp, Year, + Date, Month, Time, TimeZone, Timestamp, Year, }; use core::{cmp::Ordering, fmt::Display, hash::Hash}; @@ -70,13 +70,12 @@ impl<Tz: TimeZone> DateTime<Tz> { self.into_timezone(Tai) } - pub fn unix_timestamp(&self) -> UnixTimestamp { + pub fn unix_timestamp(&self) -> Timestamp { self.utc_datetime.timestamp() } - // TODO rethink the name of UnixTimestamp // TODO should this overflow? - pub fn tai_timestamp(&self) -> UnixTimestamp { + pub fn tai_timestamp(&self) -> Timestamp { self.as_tai().to_naive_overflowing().0.timestamp() } @@ -109,12 +108,12 @@ impl NaiveDateTime { Self { date, time } } - pub const fn from_timestamp(timestamp: UnixTimestamp) -> Self { + pub const fn from_timestamp(timestamp: Timestamp) -> Self { const UNIX_EPOCH_DAYS_AFTER_CE: i64 = Date::UNIX_EPOCH.days_after_common_era(); - let days_after_unix_epoch = timestamp.seconds_since_unix_epoch() / 86_400; + let days_after_unix_epoch = timestamp.total_seconds() / 86_400; let days_after_ce = days_after_unix_epoch + UNIX_EPOCH_DAYS_AFTER_CE as i64; let date = Date::from_days_after_common_era(days_after_ce); - let seconds_after_midnight = timestamp.seconds_since_unix_epoch() % 86_400; + let seconds_after_midnight = timestamp.total_seconds() % 86_400; let nanoseconds = timestamp.nanosecond(); let time = Time::MIDNIGHT .add_seconds_overflowing(seconds_after_midnight as isize) @@ -181,14 +180,14 @@ impl NaiveDateTime { } #[must_use] - pub const fn timestamp(self) -> UnixTimestamp { + pub const fn timestamp(self) -> Timestamp { const UNIX_EPOCH_DAYS: i64 = Date::UNIX_EPOCH.days_after_common_era(); // TODO don't require the .date() let days = (self.date.days_after_common_era() - UNIX_EPOCH_DAYS) as i64; let seconds = days * 86_400 + self.time().seconds_from_midnight() as i64; let nanoseconds = self.nanosecond(); - UnixTimestamp::new(seconds, nanoseconds) + Timestamp::new(seconds, nanoseconds) } pub const fn add_years_overflowing( @@ -242,7 +241,7 @@ impl NaiveDateTime { #[must_use] pub const fn add_hours_overflowing(self, hours: i64) -> (Self, bool) { - let timestamp: UnixTimestamp = self.timestamp(); + let timestamp: Timestamp = self.timestamp(); let (timestamp, overflow) = timestamp.add_hours_overflowing(hours); let datetime: NaiveDateTime = Self::from_timestamp(timestamp); @@ -251,7 +250,7 @@ impl NaiveDateTime { #[must_use] pub const fn add_minutes_overflowing(self, minutes: i64) -> (Self, bool) { - let timestamp: UnixTimestamp = self.timestamp(); + let timestamp: Timestamp = self.timestamp(); let (timestamp, overflow) = timestamp.add_minutes_overflowing(minutes); let datetime: NaiveDateTime = Self::from_timestamp(timestamp); @@ -260,7 +259,7 @@ impl NaiveDateTime { #[must_use] pub const fn add_seconds_overflowing(self, seconds: i64) -> (Self, bool) { - let timestamp: UnixTimestamp = self.timestamp(); + let timestamp: Timestamp = self.timestamp(); let (timestamp, overflow) = timestamp.add_seconds_overflowing(seconds); let datetime: NaiveDateTime = Self::from_timestamp(timestamp); @@ -269,7 +268,7 @@ impl NaiveDateTime { #[must_use] pub const fn add_nanoseconds_overflowing(self, nanoseconds: i64) -> (Self, bool) { - let timestamp: UnixTimestamp = self.timestamp(); + let timestamp: Timestamp = self.timestamp(); let (timestamp, overflow) = timestamp.add_nanoseconds_overflowing(nanoseconds); let datetime: NaiveDateTime = Self::from_timestamp(timestamp); @@ -345,13 +344,13 @@ impl<Tz: TimeZone> Display for DateTime<Tz> { } // TODO there's a lossy cast somewhere here or in the into(). Where is it? -impl From<UnixTimestamp> for NaiveDateTime { - fn from(timestamp: UnixTimestamp) -> Self { +impl From<Timestamp> for NaiveDateTime { + fn from(timestamp: Timestamp) -> Self { const UNIX_EPOCH_DAYS_AFTER_CE: i64 = Date::UNIX_EPOCH.days_after_common_era(); - let days_after_unix_epoch = timestamp.seconds_since_unix_epoch() / 86_400; + let days_after_unix_epoch = timestamp.total_seconds() / 86_400; let days_after_ce = days_after_unix_epoch + UNIX_EPOCH_DAYS_AFTER_CE as i64; let date = Date::from_days_after_common_era(days_after_ce); - let seconds_after_midnight = timestamp.seconds_since_unix_epoch() % 86_400; + let seconds_after_midnight = timestamp.total_seconds() % 86_400; let nanoseconds = timestamp.nanosecond(); let time = Time::MIDNIGHT .add_seconds(seconds_after_midnight as isize) @@ -17,7 +17,7 @@ pub use datetime::DateTime; pub use datetime::NaiveDateTime; pub use month::Month; pub use time::Time; -pub use timestamp::UnixTimestamp; +pub use timestamp::Timestamp; pub use timezone::TimeZone; pub use weekday::Weekday; pub use year::Year; diff --git a/src/timestamp.rs b/src/timestamp.rs index c223ccb..08944aa 100644 --- a/src/timestamp.rs +++ b/src/timestamp.rs @@ -1,12 +1,12 @@ use crate::{Date, NaiveDateTime}; #[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)] -pub struct UnixTimestamp { +pub struct Timestamp { seconds: i64, nanoseconds: u32, } -impl UnixTimestamp { +impl Timestamp { #[must_use] pub const fn new(seconds: i64, nanoseconds: u32) -> Self { Self { @@ -16,7 +16,7 @@ impl UnixTimestamp { } #[must_use] - pub const fn seconds_since_unix_epoch(self) -> i64 { + pub const fn total_seconds(self) -> i64 { self.seconds } @@ -72,7 +72,7 @@ impl UnixTimestamp { } } -impl From<NaiveDateTime> for UnixTimestamp { +impl From<NaiveDateTime> for Timestamp { fn from(ndt: NaiveDateTime) -> Self { const UNIX_EPOCH_DAYS: i64 = Date::UNIX_EPOCH.days_after_common_era(); // TODO don't require the .date() @@ -84,7 +84,7 @@ impl From<NaiveDateTime> for UnixTimestamp { } } -impl PartialOrd for UnixTimestamp { +impl PartialOrd for Timestamp { fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { match self.seconds.partial_cmp(&other.seconds) { Some(core::cmp::Ordering::Equal) => self.nanoseconds.partial_cmp(&other.nanoseconds), @@ -93,7 +93,7 @@ impl PartialOrd for UnixTimestamp { } } -impl Ord for UnixTimestamp { +impl Ord for Timestamp { fn cmp(&self, other: &Self) -> core::cmp::Ordering { match self.seconds.cmp(&other.seconds) { core::cmp::Ordering::Equal => self.nanoseconds.cmp(&other.nanoseconds), |
