use chrono::offset::Utc;
use std::{thread, time};
use std::sync::atomic::{AtomicUsize, Ordering};
lazy_static! {
static ref NOW: AtomicUsize = AtomicUsize::new(Utc::now().timestamp() as usize);
}
pub fn now() -> i64 {
NOW.load(Ordering::Relaxed) as i64
}
pub fn update_time() {
let dur = time::Duration::from_millis(500);
loop {
thread::sleep(dur);
let now = Utc::now().timestamp() as usize;
let order = Ordering::Relaxed;
NOW.store(now, order);
}
}
#[inline]
pub fn delay(attempts: u32) {
let delay = match attempts {
0 => return,
1 => 1,
2 => 4,
3 => 8,
4 => 16,
5 => 32,
6 => 64,
7 => 128,
8 => 256,
_ => 512,
};
let sleep_time = time::Duration::from_millis(delay as u64);
thread::sleep(sleep_time);
}