From 926a2a502db7207916846ccb0a6674e2babefefb Mon Sep 17 00:00:00 2001 From: cbax Date: Wed, 25 Sep 2024 15:40:48 +0000 Subject: [PATCH] fixed timestamp and implemented display --- src/lib.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 015d716..32a888c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,12 @@ pub struct Icechip { epoch: Option } +impl fmt::Display for Icechip { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.into::()); + } +} + impl Into for Icechip { fn into(self) -> u64 { let mut ret: u64 = self.timestamp.checked_shl(23).unwrap(); @@ -63,8 +69,8 @@ impl Icechip { } // the offset will get subtracted from the unix epoch before being used in the Icechip. - pub fn set_epoch_offset(mut self, offset: u64) { - self.epoch = Some(UNIX_EPOCH - offset); + pub fn set_epoch(mut self, epoch: u64) { + self.epoch = Some(epoch); } pub fn tick(&self) -> Icechip { @@ -72,7 +78,7 @@ impl Icechip { - if current timestamp in relation to self does not equal self, return an updated value, else increment the sequence - if sequence is going to overflow, sleep 1 ms */ - match SystemTime::now().duration_since(UNIX_EPOCH) { + match SystemTime::now().duration_since(UNIX_EPOCH).as_millis() { Ok(o) => { match self.version { Version::ENHANCED => { @@ -104,7 +110,11 @@ impl Icechip { } }, Version::EXTENDED => { - if self.timestamp == o { + let curr_ts = match self.epoch { + Some(s) => (o - s), + None => o, + } + if self.timestamp == curr_ts { if (self.sequence_id + 1) > u32::MAX { std::thread::sleep(std::time:Duration::from_millis(1)); return self.tick();