summaryrefslogtreecommitdiff
path: root/src/timestamp.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/timestamp.rs')
-rw-r--r--src/timestamp.rs9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/timestamp.rs b/src/timestamp.rs
index 6798792..ea8f2e1 100644
--- a/src/timestamp.rs
+++ b/src/timestamp.rs
@@ -7,6 +7,7 @@ pub struct UnixTimestamp {
}
impl UnixTimestamp {
+ #[must_use]
pub const fn new(seconds: i64, nanoseconds: u32) -> Self {
Self {
seconds,
@@ -14,14 +15,17 @@ impl UnixTimestamp {
}
}
+ #[must_use]
pub const fn seconds_since_unix_epoch(self) -> i64 {
self.seconds
}
+ #[must_use]
pub const fn nanosecond(self) -> u32 {
self.nanoseconds
}
+ #[must_use]
pub const fn add_seconds_overflowing(self, seconds: i64) -> (Self, bool) {
// TODO overflowing goes first
let (seconds, overflowing) = self.seconds.overflowing_add(seconds as i64);
@@ -30,6 +34,7 @@ impl UnixTimestamp {
(timestamp, overflowing)
}
+ #[must_use]
pub const fn add_nanoseconds_overflowing(self, nanoseconds: i64) -> (Self, bool) {
let total_nanos = (self.nanoseconds as i64 + nanoseconds) % 1_000_000_000;
let total_nanos = total_nanos + (1_000_000_000 * total_nanos.is_negative() as i64);
@@ -45,10 +50,10 @@ impl UnixTimestamp {
impl From<NaiveDateTime> for UnixTimestamp {
fn from(ndt: NaiveDateTime) -> Self {
- const UNIX_EPOCH_DAYS: isize = Date::UNIX_EPOCH.days_after_common_era();
+ const UNIX_EPOCH_DAYS: i64 = Date::UNIX_EPOCH.days_after_common_era();
// TODO don't require the .date()
let days = (ndt.date().days_after_common_era() - UNIX_EPOCH_DAYS) as i64;
- let seconds = days * 86_400 + ndt.time().seconds_from_midnight() as i64;
+ let seconds = days * 86_400 + i64::from(ndt.time().seconds_from_midnight());
let nanoseconds = ndt.nanosecond();
Self::new(seconds, nanoseconds)