Skip to content

Commit

Permalink
add time format string util
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Volk <[email protected]>
  • Loading branch information
jevolk committed Jul 4, 2024
1 parent dc18f89 commit 2dd68d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 10 additions & 2 deletions src/core/utils/time.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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()
}

0 comments on commit 2dd68d3

Please sign in to comment.