From 988ce8bbc2a33f6a90adecb7547a88cfbf7acaf2 Mon Sep 17 00:00:00 2001 From: Alejandro Soto Date: Mon, 3 Jan 2022 08:16:28 -0600 Subject: Implement Timestamp::new() --- src/lib.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 75ca189..e42747d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ pub struct Ttl { #[derive(Copy, Clone, Default, Eq, PartialEq)] pub struct Timestamp { - seconds: u64, + seconds: i64, nanoseconds: u32, } @@ -98,12 +98,33 @@ impl Ttl { } } +impl Timestamp { + pub fn new(seconds: i64, nanoseconds: u32) -> Self { + Timestamp { + seconds, + nanoseconds, + } + } +} + impl From for Timestamp { fn from(time: SystemTime) -> Self { - let duration = time.duration_since(UNIX_EPOCH).unwrap(); + let (seconds, nanoseconds) = match time.duration_since(UNIX_EPOCH) { + Ok(duration) => { + let secs = duration.as_secs().try_into().unwrap(); + (secs, duration.subsec_nanos()) + } + + Err(before_epoch) => { + let duration = before_epoch.duration(); + let secs = -i64::try_from(duration.as_secs()).unwrap(); + (secs, duration.subsec_nanos()) + } + }; + Timestamp { - seconds: duration.as_secs(), - nanoseconds: duration.subsec_nanos(), + seconds, + nanoseconds, } } } -- cgit v1.2.3