diaryrs/src/time.rs

26 lines
640 B
Rust
Raw Normal View History

2024-12-25 09:38:38 +00:00
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,
})
}
}