diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ef9b70..6dfca90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Procedure when bumping the version number: - `bot::botrulez::ping` - `bot::botrulez::short_help` - `bot::botrulez::uptime` +- `bot::botrulez::format_relative_time` ### Changed @@ -31,6 +32,8 @@ Procedure when bumping the version number: this causes a panic while using euphoxide, consider following the steps mentioned in the [tokio-tungstenite README]. If I'm reading the [rustls docs] correctly, it is on the users of the libraries to set the required features. +- `bot::botrulez::format_duration` now no longer mentions "since" or "ago", but + instead has a sign (`-`) if the duration is negative. [tokio-tungstenite README]: https://github.com/snapview/tokio-tungstenite?tab=readme-ov-file#features [rustls docs]: https://docs.rs/rustls/0.23.19/rustls/crypto/struct.CryptoProvider.html#using-the-per-process-default-cryptoprovider diff --git a/src/bot/botrulez.rs b/src/bot/botrulez.rs index 2120ce3..6dd5adb 100644 --- a/src/bot/botrulez.rs +++ b/src/bot/botrulez.rs @@ -7,4 +7,4 @@ pub mod uptime; pub use self::full_help::{FullHelp, HasDescriptions}; pub use self::ping::Ping; pub use self::short_help::ShortHelp; -pub use self::uptime::{format_duration, format_time, HasStartTime, Uptime}; +pub use self::uptime::{format_duration, format_relative_time, format_time, HasStartTime, Uptime}; diff --git a/src/bot/botrulez/uptime.rs b/src/bot/botrulez/uptime.rs index 7f79ba3..d8b1d0d 100644 --- a/src/bot/botrulez/uptime.rs +++ b/src/bot/botrulez/uptime.rs @@ -1,21 +1,29 @@ use async_trait::async_trait; use clap::Parser; -use jiff::{Span, Timestamp}; +use jiff::{Span, Timestamp, Unit}; use crate::api::Message; use crate::bot::command::{ClapCommand, Command, Context}; use crate::conn; pub fn format_time(t: Timestamp) -> String { - t.strftime("%Y-&m-%d %H:%M:%S UTC").to_string() + t.strftime("%Y-%m-%d %H:%M:%S UTC").to_string() +} + +pub fn format_relative_time(d: Span) -> String { + if d.is_positive() { + format!("in {}", format_duration(d.abs())) + } else { + format!("{} ago", format_duration(d.abs())) + } } pub fn format_duration(d: Span) -> String { - let d_abs = d.abs(); - let days = d_abs.get_days(); - let hours = d_abs.get_hours() % 24; - let mins = d_abs.get_minutes() % 60; - let secs = d_abs.get_seconds() % 60; + let total = d.abs().total(Unit::Second).unwrap() as i64; + let secs = total % 60; + let mins = (total / 60) % 60; + let hours = (total / 60 / 60) % 24; + let days = total / 60 / 60 / 24; let mut segments = vec![]; if days > 0 { @@ -36,9 +44,9 @@ pub fn format_duration(d: Span) -> String { let segments = segments.join(" "); if d.is_positive() { - format!("in {segments}") + segments } else { - format!("{segments} ago") + format!("-{segments}") } } @@ -56,7 +64,7 @@ impl Uptime { let mut reply = format!( "/me has been up since {} ({})", format_time(start), - format_duration(start - now), + format_relative_time(start - now), ); if connected { @@ -64,7 +72,7 @@ impl Uptime { reply.push_str(&format!( ", connected since {} ({})", format_time(since), - format_duration(since - now), + format_relative_time(since - now), )); }