fixed timestamp and implemented display

This commit is contained in:
cbax 2024-09-25 15:40:48 +00:00
parent f030cec3d0
commit 926a2a502d

View file

@ -15,6 +15,12 @@ pub struct Icechip<T> {
epoch: Option<u64> epoch: Option<u64>
} }
impl fmt::Display for Icechip<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.into::<T>());
}
}
impl Into<u64> for Icechip<u64> { impl Into<u64> for Icechip<u64> {
fn into(self) -> u64 { fn into(self) -> u64 {
let mut ret: u64 = self.timestamp.checked_shl(23).unwrap(); let mut ret: u64 = self.timestamp.checked_shl(23).unwrap();
@ -63,8 +69,8 @@ impl<T> Icechip<T> {
} }
// the offset will get subtracted from the unix epoch before being used in the 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) { pub fn set_epoch(mut self, epoch: u64) {
self.epoch = Some(UNIX_EPOCH - offset); self.epoch = Some(epoch);
} }
pub fn tick(&self) -> Icechip<T> { pub fn tick(&self) -> Icechip<T> {
@ -72,7 +78,7 @@ impl<T> Icechip<T> {
- if current timestamp in relation to self does not equal self, return an updated value, else increment the sequence - 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 - 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) => { Ok(o) => {
match self.version { match self.version {
Version::ENHANCED => { Version::ENHANCED => {
@ -104,7 +110,11 @@ impl<T> Icechip<T> {
} }
}, },
Version::EXTENDED => { 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 { if (self.sequence_id + 1) > u32::MAX {
std::thread::sleep(std::time:Duration::from_millis(1)); std::thread::sleep(std::time:Duration::from_millis(1));
return self.tick(); return self.tick();