mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
20 lines
664 B
Rust
20 lines
664 B
Rust
use std::path::Path;
|
|
use time::{Date, OffsetDateTime, Time, macros::format_description};
|
|
|
|
pub struct TimeUtils;
|
|
|
|
impl TimeUtils {
|
|
pub fn datetime_from_path(path: &Path) -> Option<OffsetDateTime> {
|
|
let (dt_part, hour_part) =
|
|
(path.parent()?.file_name()?.to_str()?, path.file_name()?.to_str()?);
|
|
Some(OffsetDateTime::new_utc(
|
|
Date::parse(dt_part, &format_description!("[year][month][day]")).ok()?,
|
|
Time::from_hms(hour_part.parse().ok()?, 0, 0).ok()?,
|
|
))
|
|
}
|
|
|
|
pub fn date_from_datetime(dt: OffsetDateTime) -> String {
|
|
dt.format(&format_description!("[year][month][day]")).unwrap()
|
|
}
|
|
}
|