use chrono::DateTime; use std::time::{SystemTime, UNIX_EPOCH}; /// Get the current time since epoch pub fn time() -> u64 { SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() .as_secs() } /// Format unix timestamp to a human-readable string pub fn fmt_time(timestamp: u64) -> String { // Create a DateTime from the timestamp let datetime = DateTime::from_timestamp(timestamp as i64, 0).expect("Failed to parse timestamp!"); // Format the datetime how you want datetime.format("%Y-%m-%d %H:%M:%S").to_string() } #[cfg(test)] mod test { use crate::utils::time::{fmt_time, time}; #[test] fn test_time() { assert!(time() > 10); } #[test] fn test_fmt_time() { assert_eq!(fmt_time(1693475465), "2023-08-31 09:51:05"); } }