21 lines
567 B
Rust
21 lines
567 B
Rust
use rand::distr::{Alphanumeric, SampleString};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
/// Generate a random string of a given size
|
|
pub fn rand_str(len: usize) -> String {
|
|
Alphanumeric.sample_string(&mut rand::rng(), len)
|
|
}
|
|
|
|
/// Get current time
|
|
pub fn curr_time() -> anyhow::Result<u64> {
|
|
Ok(SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.map(|t| t.as_secs())?)
|
|
}
|
|
|
|
/// Format time
|
|
pub fn format_time(time: u64) -> Option<String> {
|
|
let time = chrono::DateTime::from_timestamp(time as i64, 0)?;
|
|
Some(time.naive_local().to_string())
|
|
}
|