diff options
| author | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-09-01 13:01:25 +0200 |
|---|---|---|
| committer | Daniel Jankowski <daniel.jankowski@rub.de> | 2023-09-01 13:01:25 +0200 |
| commit | 6a3aedcec03a2962d2f99085d6c794778886d17c (patch) | |
| tree | 2b107c301e195e88a8130b9f16a3dac893fb93c6 /src/widgets | |
| parent | fix: make bar not selectable (diff) | |
| download | sl0pstatus-6a3aedcec03a2962d2f99085d6c794778886d17c.tar.gz sl0pstatus-6a3aedcec03a2962d2f99085d6c794778886d17c.tar.xz sl0pstatus-6a3aedcec03a2962d2f99085d6c794778886d17c.zip | |
fix: make timezone configurable
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/datetime.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/widgets/datetime.rs b/src/widgets/datetime.rs index d06e20d..4332caa 100644 --- a/src/widgets/datetime.rs +++ b/src/widgets/datetime.rs @@ -1,6 +1,7 @@ -use std::collections::BTreeMap; +use std::{collections::BTreeMap, str::FromStr}; use chrono::Local; +use chrono_tz::Tz; use crate::{config::FormattedPart, render}; @@ -9,6 +10,7 @@ use super::widget::Widget; pub struct DateTimeWidget { format: String, color_format: FormattedPart, + time_zone: Option<Tz>, } impl DateTimeWidget { @@ -18,6 +20,16 @@ impl DateTimeWidget { format = form; } + let mut time_zone_string = "Etc/UTC"; + if let Some(tz_string) = config.get("datetime_timezone") { + time_zone_string = tz_string; + } + + let time_zone = match Tz::from_str(time_zone_string) { + Ok(tz) => Some(tz), + Err(_) => None, + }; + let mut color_format = ""; if let Some(form) = config.get("datetime") { color_format = form; @@ -26,6 +38,7 @@ impl DateTimeWidget { Self { format: format.to_string(), color_format: FormattedPart::from_format_string(color_format.to_string()), + time_zone, } } } @@ -36,9 +49,14 @@ impl Widget for DateTimeWidget { if output.contains("{format}") { let date = Local::now(); + let mut tz = Tz::UTC; + if let Some(t) = self.time_zone.clone() { + tz = t; + } + output = output.replace( "{format}", - format!("{}", date.format(self.format.as_str())).as_str(), + format!("{}", date.with_timezone(&tz).format(self.format.as_str())).as_str(), ); } |
