From 4d06a1df842c38ec6f70c790cc5eb61e9c81340c Mon Sep 17 00:00:00 2001 From: Xiretza Date: Tue, 18 Jul 2023 20:19:03 +0000 Subject: [PATCH] Print opening times in local time --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 2 +- src/bot.rs | 18 ++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 21e33c3..6a011f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1516,6 +1516,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "object" version = "0.29.0" @@ -2391,8 +2400,12 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376" dependencies = [ + "itoa", + "libc", + "num_threads", "serde", "time-core", + "time-macros", ] [[package]] @@ -2401,6 +2414,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" +[[package]] +name = "time-macros" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d967f99f534ca7e495c575c62638eebc2898a8c84c119b89e250477bc4ba16b2" +dependencies = [ + "time-core", +] + [[package]] name = "tinyvec" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 712b6f1..6266031 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ reqwest = { version = "0.11.12", features = ["json"] } serde = { version = "1.0.147", features = ["derive"] } spaceapi = "0.8.1" thiserror = "1.0.37" -time = "0.3.17" +time = { version = "0.3.17", features = ["local-offset", "formatting", "macros"] } tokio = { version = "1.21.2", features = ["full"] } toml = "0.5.9" url = { version = "2.3.1", features = ["serde"] } diff --git a/src/bot.rs b/src/bot.rs index d092787..b6fd24f 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -25,7 +25,7 @@ use matrix_sdk::{ use never_say_never::Never; use reqwest::Url; use serde::Deserialize; -use time::OffsetDateTime; +use time::{ext::NumericalDuration, macros::format_description, OffsetDateTime, UtcOffset}; use tokio::time::sleep; use tracing::{event, instrument, span, Level}; @@ -228,7 +228,21 @@ impl Bot { "isitopen" => { match self.update_open_state().await? { OpenState::Open { since } => { - reply(&format!("positive! space has been open since {since}")).await? + if let Ok(offset) = UtcOffset::current_local_offset() { + let since = since.to_offset(offset); + + let s = if OffsetDateTime::now_utc() - since > 20.hours() { + since.format(format_description!( + "[year]-[month]-[day] [hour]:[minute]:[second]" + ))? + } else { + since.format(format_description!("[hour]:[minute]:[second]"))? + }; + + reply(&format!("positive! space has been open since {s}")).await? + } else { + reply(&format!("positive! space has been open since {since}")).await? + } } OpenState::Closed => reply("negative!").await?, };