diff --git a/Cargo.toml b/Cargo.toml index 5b96f34..f4f278b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,6 @@ serde_json = "1.0.113" curl = "0.4.46" regex = "1.11.1" chrono = "0.4.41" +chrono-tz = "0.10.3" +iana-time-zone = "0.1.63" clap = { version = "4.5.1", features = ["derive"] } diff --git a/src/bar_modules/bar_module_time.rs b/src/bar_modules/bar_module_time.rs index 12d5c90..2e5a47a 100644 --- a/src/bar_modules/bar_module_time.rs +++ b/src/bar_modules/bar_module_time.rs @@ -1,11 +1,13 @@ -use chrono::{DateTime, Local}; +use chrono::{DateTime, TimeZone, Local, Utc}; use crate::common; use crate::bar_modules; +use chrono_tz::{OffsetName, Tz}; #[derive(Clone)] pub struct UnibarModuleTime { opts: common::AppOptions, date_time: DateTime, + time_zone: String, } impl UnibarModuleTime { @@ -14,6 +16,7 @@ impl UnibarModuleTime { UnibarModuleTime { opts: o, date_time: Local::now(), + time_zone: "".to_string(), } } } @@ -23,10 +26,23 @@ impl bar_modules::BarModuleActions for UnibarModuleTime { // -------------------- fn clear(&mut self) { self.date_time = Local::now(); + self.time_zone = "".to_string(); } // -------------------- fn generate_data(&mut self) { + match iana_time_zone::get_timezone() { + Ok(tz_str) => { + let tz: Tz = tz_str.parse().expect("Trouble parsing timezone string"); + let offset = tz.offset_from_utc_date(&Utc::now().date_naive()); + self.time_zone = offset.abbreviation() + .expect("Trouble abbreviating timezone") + .to_string(); + } + Err(e) => { + eprintln!("Trouble getting timezone: {e}"); + } + } if self.opts.debug { println!("-----> Time dump {:#?}", self.date_time); } @@ -34,7 +50,7 @@ impl bar_modules::BarModuleActions for UnibarModuleTime { // -------------------- fn get_content(&self) -> String { - return format!("{}", self.date_time.format("%I:%M%p %Z")); + return format!("{} {}", self.date_time.format("%I:%M%p"), self.time_zone); } // --------------------