add time format string util

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-04 12:58:53 +00:00
parent dc18f89c0b
commit 2dd68d3fa5
2 changed files with 11 additions and 3 deletions

View file

@ -208,7 +208,7 @@ features = ["serde"]
# standard date and time tools # standard date and time tools
[workspace.dependencies.chrono] [workspace.dependencies.chrono]
version = "0.4.38" version = "0.4.38"
features = ["alloc"] features = ["alloc", "std"]
default-features = false default-features = false
[workspace.dependencies.hyper] [workspace.dependencies.hyper]

View file

@ -1,9 +1,9 @@
use std::time::{SystemTime, UNIX_EPOCH};
#[inline] #[inline]
#[must_use] #[must_use]
#[allow(clippy::as_conversions)] #[allow(clippy::as_conversions)]
pub fn now_millis() -> u64 { pub fn now_millis() -> u64 {
use std::time::UNIX_EPOCH;
UNIX_EPOCH UNIX_EPOCH
.elapsed() .elapsed()
.expect("positive duration after epoch") .expect("positive duration after epoch")
@ -18,3 +18,11 @@ pub fn rfc2822_from_seconds(epoch: i64) -> String {
.unwrap_or_default() .unwrap_or_default()
.to_rfc2822() .to_rfc2822()
} }
#[must_use]
pub fn format(ts: SystemTime, str: &str) -> String {
use chrono::{DateTime, Utc};
let dt: DateTime<Utc> = ts.into();
dt.format(str).to_string()
}