diaryrs/src/time.rs
2024-12-25 17:38:38 +08:00

25 lines
640 B
Rust

use chrono::prelude::*;
pub struct Time {
pub datetime: DateTime<Local>,
pub timestamp: String,
pub timezone: String,
}
impl Time {
pub fn build() -> Result<Self, Box<dyn std::error::Error>> {
let local: DateTime<Local> = Local::now();
// rfc3339 equiv. to stricter ISO 8601
let timestamp = local.to_rfc3339();
let tz_offset = local.format("%z").to_string();
let tz_name = iana_time_zone::get_timezone()?;
let timezone = format!("{} ({})", tz_name, tz_offset);
Ok(Time {
datetime: local,
timestamp,
timezone,
})
}
}