2022-11-12 10:16:55 +00:00
|
|
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
2022-04-01 20:51:33 +00:00
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
|
|
/// Get the current time since epoch
|
|
|
|
pub fn time() -> u64 {
|
2022-04-03 13:50:49 +00:00
|
|
|
SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.unwrap()
|
|
|
|
.as_secs()
|
|
|
|
}
|
2022-11-12 10:16:55 +00:00
|
|
|
|
|
|
|
/// Format unix timestamp to a human-readable string
|
|
|
|
pub fn fmt_time(timestamp: u64) -> String {
|
|
|
|
// Create a NaiveDateTime from the timestamp
|
|
|
|
let naive =
|
|
|
|
NaiveDateTime::from_timestamp_opt(timestamp as i64, 0).expect("Failed to parse timestamp!");
|
|
|
|
|
|
|
|
// Create a normal DateTime from the NaiveDateTime
|
|
|
|
let datetime: DateTime<Utc> = DateTime::from_utc(naive, Utc);
|
|
|
|
|
|
|
|
// Format the datetime how you want
|
|
|
|
datetime.format("%Y-%m-%d %H:%M:%S").to_string()
|
|
|
|
}
|