From 2dd68d3fa5fa48cb19864aa0f35bf825cf28d96e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 4 Jul 2024 12:58:53 +0000 Subject: [PATCH] add time format string util Signed-off-by: Jason Volk --- Cargo.toml | 2 +- src/core/utils/time.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a52b4138..44f43849 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -208,7 +208,7 @@ features = ["serde"] # standard date and time tools [workspace.dependencies.chrono] version = "0.4.38" -features = ["alloc"] +features = ["alloc", "std"] default-features = false [workspace.dependencies.hyper] diff --git a/src/core/utils/time.rs b/src/core/utils/time.rs index f6208b31..7de00e9e 100644 --- a/src/core/utils/time.rs +++ b/src/core/utils/time.rs @@ -1,9 +1,9 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + #[inline] #[must_use] #[allow(clippy::as_conversions)] pub fn now_millis() -> u64 { - use std::time::UNIX_EPOCH; - UNIX_EPOCH .elapsed() .expect("positive duration after epoch") @@ -18,3 +18,11 @@ pub fn rfc2822_from_seconds(epoch: i64) -> String { .unwrap_or_default() .to_rfc2822() } + +#[must_use] +pub fn format(ts: SystemTime, str: &str) -> String { + use chrono::{DateTime, Utc}; + + let dt: DateTime = ts.into(); + dt.format(str).to_string() +}