use chrono::{DateTime, Local, Utc, offset};
use std::time::{Duration, Instant};
pub fn now() -> String {
Local::now().to_string()
}
pub fn timestamp() -> i64 {
Utc::now().timestamp()
}
pub fn from_timestamp(secs: i64, nsecs: u32) -> String {
if let Some(value) = DateTime::from_timestamp(secs, nsecs) {
value.with_timezone(&offset::Local).to_string()
} else {
"".to_string()
}
}
#[derive(Debug, Clone, Copy)]
pub struct Stopwatch {
start: Instant,
}
impl Stopwatch {
pub fn new() -> Self {
Self {
start: Instant::now(),
}
}
pub fn elapsed(&self) -> Duration {
self.start.elapsed()
}
pub fn elapsed_ms(&self) -> u32 {
self.elapsed().as_millis().max(1) as u32
}
pub fn elapsed_human(&self) -> String {
humantime::format_duration(self.elapsed()).to_string()
}
}
impl Default for Stopwatch {
fn default() -> Self {
Self::new()
}
}