use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};
pub struct IdWorkerUtils;
impl IdWorkerUtils {
pub fn calc_timestamp(epoch: u64, precision: i8) -> Result<u64, SystemTimeError>
where
Self: Sized,
{
let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() as u64
* (precision * 10) as u64;
Ok(now - epoch * precision as u64)
}
pub fn calc_id(
timestamp: u64,
timestamp_shift: u8,
worker: u16,
sequence: u32,
sequence_bits: u8,
) -> u64
where
Self: Sized,
{
timestamp << timestamp_shift | (worker as u64) << sequence_bits | sequence as u64
}
}