Add Internet Time for some reason

This commit is contained in:
Artemis Tosini 2023-12-12 21:41:44 +00:00
parent ecf78a4264
commit 8e968f13e6
Signed by: artemist
GPG key ID: EE5227935FE3FF18
3 changed files with 53 additions and 0 deletions

View file

@ -62,6 +62,7 @@ pub enum TileConfigType {
UtcTime(UtcTimeConfig), UtcTime(UtcTimeConfig),
SystemTime(SystemTimeConfig), SystemTime(SystemTimeConfig),
LocalTime(LocalTimeConfig), LocalTime(LocalTimeConfig),
InternetTime(InternetTimeConfig),
Iwd(IwdConfig), Iwd(IwdConfig),
} }
@ -112,6 +113,13 @@ pub struct LocalTimeConfig {
pub ip_addr_api: Box<str>, pub ip_addr_api: Box<str>,
} }
#[derive(SmartDefault, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct InternetTimeConfig {
#[default(2)]
pub precision: u8,
}
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
#[structopt( #[structopt(
name = "rustybar", name = "rustybar",
@ -175,6 +183,9 @@ pub fn process_tile(
), ),
TileConfigType::SystemTime(c) => wrap(tiles::system_time_stream(c.clone()), tile.update), 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::LocalTime(c) => wrap(tiles::local_time_stream(c.clone()), tile.update),
TileConfigType::InternetTime(c) => {
wrap(tiles::internet_time_stream(c.clone()), tile.update)
}
} }
} }

View file

@ -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;
}
}

View file

@ -1,5 +1,6 @@
pub mod battery; pub mod battery;
pub mod hostname; pub mod hostname;
pub mod internet_time;
pub mod iwd; pub mod iwd;
pub mod load; pub mod load;
pub mod local_time; pub mod local_time;
@ -8,6 +9,7 @@ pub mod system_time;
pub mod utc_time; pub mod utc_time;
pub use battery::battery_stream; pub use battery::battery_stream;
pub use hostname::hostname_stream; pub use hostname::hostname_stream;
pub use internet_time::internet_time_stream;
pub use iwd::iwd_stream; pub use iwd::iwd_stream;
pub use load::load_stream; pub use load::load_stream;
pub use local_time::local_time_stream; pub use local_time::local_time_stream;