diff options
| author | Alejandro Soto <alejandro@34project.org> | 2022-01-03 08:16:28 -0600 |
|---|---|---|
| committer | Alejandro Soto <alejandro@34project.org> | 2022-01-03 08:16:28 -0600 |
| commit | 988ce8bbc2a33f6a90adecb7547a88cfbf7acaf2 (patch) | |
| tree | 7477c5b1671c37b60b30abae86e6b4c659ba8b50 /src/lib.rs | |
| parent | b64ce0506671f72a5142c0e82673331dc687b9e2 (diff) | |
Implement Timestamp::new()
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 29 |
1 files changed, 25 insertions, 4 deletions
@@ -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<SystemTime> 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, } } } |
