From 8e968f13e674ae7454de4d667cc73ea36cce5066 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 12 Dec 2023 21:41:44 +0000 Subject: [PATCH] Add Internet Time for some reason --- src/config.rs | 11 +++++++++++ src/tiles/internet_time.rs | 40 ++++++++++++++++++++++++++++++++++++++ src/tiles/mod.rs | 2 ++ 3 files changed, 53 insertions(+) create mode 100644 src/tiles/internet_time.rs diff --git a/src/config.rs b/src/config.rs index 305de12..fae88d8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -62,6 +62,7 @@ pub enum TileConfigType { UtcTime(UtcTimeConfig), SystemTime(SystemTimeConfig), LocalTime(LocalTimeConfig), + InternetTime(InternetTimeConfig), Iwd(IwdConfig), } @@ -112,6 +113,13 @@ pub struct LocalTimeConfig { pub ip_addr_api: Box, } +#[derive(SmartDefault, Deserialize, Clone, Debug)] +#[serde(default)] +pub struct InternetTimeConfig { + #[default(2)] + pub precision: u8, +} + #[derive(Debug, StructOpt)] #[structopt( name = "rustybar", @@ -175,6 +183,9 @@ pub fn process_tile( ), TileConfigType::SystemTime(c) => wrap(tiles::system_time_stream(c.clone()), tile.update), TileConfigType::LocalTime(c) => wrap(tiles::local_time_stream(c.clone()), tile.update), + TileConfigType::InternetTime(c) => { + wrap(tiles::internet_time_stream(c.clone()), tile.update) + } } } diff --git a/src/tiles/internet_time.rs b/src/tiles/internet_time.rs new file mode 100644 index 0000000..d4a3d3d --- /dev/null +++ b/src/tiles/internet_time.rs @@ -0,0 +1,40 @@ +use super::TileResult; +use crate::config::InternetTimeConfig; +use crate::tile::Block; +use chrono::prelude::*; +use futures_async_stream::stream; +use tokio::time::sleep; + +#[stream(item = TileResult)] +pub async fn internet_time_stream(config: InternetTimeConfig) { + // "BMT" is actually just UTC+1 + let offset = FixedOffset::east_opt(60 * 60).unwrap(); + let factor = 10f64.powi(config.precision.into()); + loop { + let now = Utc::now().with_timezone(&offset); + // If we have a leap second this could be 83_640.999_999_999, giving us a time of 1000.01 + // I don't care. + let seconds = + now.num_seconds_from_midnight() as f64 + (now.nanosecond() as f64) / 1_000_000_000f64; + let internet_time = seconds / 86.4; + yield Ok(Block { + full_text: format!( + "@{time:03.prec$}", + time = internet_time, + prec = config.precision as usize + ) + .into_boxed_str(), + short_text: Some(format!("@{:03.0}", internet_time).into_boxed_str()), + name: "internet_time".into(), + ..Default::default() + }); + + // In normal (non-metric) seconds + let difference = (1f64 - (internet_time * factor).fract()) / factor * 86.4; + + sleep(std::time::Duration::from_nanos( + (difference * 1_000_000_000f64) as u64, + )) + .await; + } +} diff --git a/src/tiles/mod.rs b/src/tiles/mod.rs index a94c8ae..c44b5f8 100644 --- a/src/tiles/mod.rs +++ b/src/tiles/mod.rs @@ -1,5 +1,6 @@ pub mod battery; pub mod hostname; +pub mod internet_time; pub mod iwd; pub mod load; pub mod local_time; @@ -8,6 +9,7 @@ pub mod system_time; pub mod utc_time; pub use battery::battery_stream; pub use hostname::hostname_stream; +pub use internet_time::internet_time_stream; pub use iwd::iwd_stream; pub use load::load_stream; pub use local_time::local_time_stream;