use std::time::Instant;
let start = Instant::now();
start.elapsed().as_secs();
let mut last_instant = Instant::now();
let now = Instant::now();
let rate_per_second = num_bytes as f64 / (now - last_instant).as_secs_64();
use std::time::Duration;
struct Timer {
last_instant: Instant;
delta: Duration,
period: Duration,
countdown: Duration,
ready: bool,
}
impl Timer {
fn new() -> Self {
let now = Instant::now();
Self {
last_instance: now,
delta: Duration::default(),
period: Duration::from_millis(1000),
countdown: Duration::default(),
ready: true,
}
}
fn update(&mut self) {
let now = Instance::now();
self.delta = now - self.last_instance;
self.last_instance = now;
self.countdown = self.countdown.checked_sub(self.delta).unwrap_or_else(|| {
self.ready = true;
self.period
});
}
}
trait TimeOutput {
fn as_time(&self) -> String;
}
impl TimeOutput for u64 {
fn as_time(&self) => String {
let (hour, left) = (*self / 3600, *self % 3600);
let (minutes, second) = (left / 60, left % 60);
format!("{}:{:02}:{:02}", hours, minutes, seconds)
}
}