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
[workspace.dependencies.chrono]
version = "0.4.38"
features = ["alloc"]
features = ["alloc", "std"]
default-features = false
[workspace.dependencies.hyper]

View file

@ -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<Utc> = ts.into();
dt.format(str).to_string()
}