torrust_index/utils/
clock.rs1use chrono::{DateTime, TimeDelta, Utc};
2
3pub const DATETIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S";
4
5#[must_use]
12pub fn now() -> u64 {
13 u64::try_from(chrono::prelude::Utc::now().timestamp()).expect("timestamp should be positive")
14}
15
16#[must_use]
23pub fn seconds_ago_utc(seconds: i64) -> DateTime<chrono::Utc> {
24 Utc::now()
25 - TimeDelta::try_seconds(seconds).expect("seconds should be more than i64::MAX / 1_000 or less than -i64::MAX / 1_000")
26}
27
28#[must_use]
32pub fn datetime_now() -> String {
33 Utc::now().format(DATETIME_FORMAT).to_string()
34}