summaryrefslogtreecommitdiff
path: root/src/datetime.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/datetime.rs')
-rw-r--r--src/datetime.rs56
1 files changed, 40 insertions, 16 deletions
diff --git a/src/datetime.rs b/src/datetime.rs
index df4e637..9a43a4b 100644
--- a/src/datetime.rs
+++ b/src/datetime.rs
@@ -1,6 +1,6 @@
use crate::{
timezone::{Utc, UtcOffset},
- Date, Month, Time, TimeZone, Year,
+ Date, Month, Time, TimeZone, UnixTimestamp, Year,
};
use core::{cmp::Ordering, fmt::Display, hash::Hash};
@@ -45,72 +45,82 @@ impl<Tz: TimeZone> DateTime<Tz> {
impl NaiveDateTime {
// TODO docs
+ #[must_use]
pub const fn new(date: Date, time: Time) -> Self {
Self { date, time }
}
+ #[must_use]
pub const fn date(self) -> Date {
self.date
}
+ #[must_use]
pub const fn time(self) -> Time {
self.time
}
+ #[must_use]
pub const fn year(self) -> Year {
self.date.year()
}
+ #[must_use]
pub const fn month(self) -> Month {
self.date.month()
}
+ #[must_use]
pub const fn day(self) -> u8 {
self.date.day()
}
+ #[must_use]
pub const fn hour(self) -> u8 {
self.time.hour()
}
+ #[must_use]
pub const fn minute(self) -> u8 {
self.time.minute()
}
+ #[must_use]
pub const fn second(self) -> u8 {
self.time.second()
}
+ #[must_use]
pub const fn millisecond(self) -> u16 {
self.time.millisecond()
}
+ #[must_use]
pub const fn microsecond(self) -> u32 {
self.time.microsecond()
}
+ #[must_use]
pub const fn nanosecond(self) -> u32 {
self.time.nanosecond()
}
#[must_use]
- pub fn add_seconds(self, seconds: isize) -> Self {
- let time = self.time.add_seconds(seconds);
+ pub fn add_seconds_overflowing(self, seconds: i64) -> (Self, bool) {
+ let timestamp: UnixTimestamp = self.into();
+ let (timestamp, overflow) = timestamp.add_seconds_overflowing(seconds);
+ let datetime: NaiveDateTime = timestamp.into();
- Self {
- date: self.date,
- time,
- }
+ (datetime, overflow)
}
#[must_use]
- pub fn add_nanoseconds(self, nanoseconds: isize) -> Self {
- let time = self.time.add_nanoseconds(nanoseconds);
+ pub fn add_nanoseconds_overflowing(self, nanoseconds: i64) -> (Self, bool) {
+ let timestamp: UnixTimestamp = self.into();
+ let (timestamp, overflow) = timestamp.add_nanoseconds_overflowing(nanoseconds);
+ let datetime: NaiveDateTime = timestamp.into();
- Self {
- date: self.date,
- time,
- }
+ (datetime, overflow)
}
}
@@ -153,7 +163,7 @@ impl<Tz: TimeZone, Other: TimeZone> PartialEq<DateTime<Other>> for DateTime<Tz>
impl<Tz: TimeZone> Hash for DateTime<Tz> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
- self.utc_datetime.hash(state)
+ self.utc_datetime.hash(state);
}
}
@@ -169,8 +179,6 @@ impl<Tz: TimeZone> Ord for DateTime<Tz> {
}
}
-// TODO addition
-
impl Display for NaiveDateTime {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{} {}", self.date, self.time)
@@ -182,3 +190,19 @@ impl<Tz: TimeZone> Display for DateTime<Tz> {
write!(f, "{} {}", self.utc_datetime, self.timezone)
}
}
+
+impl From<UnixTimestamp> for NaiveDateTime {
+ fn from(timestamp: UnixTimestamp) -> 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_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 nanoseconds = timestamp.nanosecond();
+ let time = Time::MIDNIGHT
+ .add_seconds(seconds_after_midnight as isize)
+ .add_nanoseconds(nanoseconds as isize);
+
+ Self::new(date, time)
+ }
+}