generated from Astrial.org/template
26 lines
640 B
Rust
26 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,
|
||
|
})
|
||
|
}
|
||
|
}
|