Fix time and duration formatting
This commit is contained in:
parent
58b55ef433
commit
fe68694932
3 changed files with 23 additions and 12 deletions
|
|
@ -22,6 +22,7 @@ Procedure when bumping the version number:
|
||||||
- `bot::botrulez::ping`
|
- `bot::botrulez::ping`
|
||||||
- `bot::botrulez::short_help`
|
- `bot::botrulez::short_help`
|
||||||
- `bot::botrulez::uptime`
|
- `bot::botrulez::uptime`
|
||||||
|
- `bot::botrulez::format_relative_time`
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|
@ -31,6 +32,8 @@ Procedure when bumping the version number:
|
||||||
this causes a panic while using euphoxide, consider following the steps
|
this causes a panic while using euphoxide, consider following the steps
|
||||||
mentioned in the [tokio-tungstenite README]. If I'm reading the [rustls docs]
|
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.
|
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
|
[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
|
[rustls docs]: https://docs.rs/rustls/0.23.19/rustls/crypto/struct.CryptoProvider.html#using-the-per-process-default-cryptoprovider
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,4 @@ pub mod uptime;
|
||||||
pub use self::full_help::{FullHelp, HasDescriptions};
|
pub use self::full_help::{FullHelp, HasDescriptions};
|
||||||
pub use self::ping::Ping;
|
pub use self::ping::Ping;
|
||||||
pub use self::short_help::ShortHelp;
|
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};
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,29 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use jiff::{Span, Timestamp};
|
use jiff::{Span, Timestamp, Unit};
|
||||||
|
|
||||||
use crate::api::Message;
|
use crate::api::Message;
|
||||||
use crate::bot::command::{ClapCommand, Command, Context};
|
use crate::bot::command::{ClapCommand, Command, Context};
|
||||||
use crate::conn;
|
use crate::conn;
|
||||||
|
|
||||||
pub fn format_time(t: Timestamp) -> String {
|
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 {
|
pub fn format_duration(d: Span) -> String {
|
||||||
let d_abs = d.abs();
|
let total = d.abs().total(Unit::Second).unwrap() as i64;
|
||||||
let days = d_abs.get_days();
|
let secs = total % 60;
|
||||||
let hours = d_abs.get_hours() % 24;
|
let mins = (total / 60) % 60;
|
||||||
let mins = d_abs.get_minutes() % 60;
|
let hours = (total / 60 / 60) % 24;
|
||||||
let secs = d_abs.get_seconds() % 60;
|
let days = total / 60 / 60 / 24;
|
||||||
|
|
||||||
let mut segments = vec![];
|
let mut segments = vec![];
|
||||||
if days > 0 {
|
if days > 0 {
|
||||||
|
|
@ -36,9 +44,9 @@ pub fn format_duration(d: Span) -> String {
|
||||||
|
|
||||||
let segments = segments.join(" ");
|
let segments = segments.join(" ");
|
||||||
if d.is_positive() {
|
if d.is_positive() {
|
||||||
format!("in {segments}")
|
segments
|
||||||
} else {
|
} else {
|
||||||
format!("{segments} ago")
|
format!("-{segments}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,7 +64,7 @@ impl Uptime {
|
||||||
let mut reply = format!(
|
let mut reply = format!(
|
||||||
"/me has been up since {} ({})",
|
"/me has been up since {} ({})",
|
||||||
format_time(start),
|
format_time(start),
|
||||||
format_duration(start - now),
|
format_relative_time(start - now),
|
||||||
);
|
);
|
||||||
|
|
||||||
if connected {
|
if connected {
|
||||||
|
|
@ -64,7 +72,7 @@ impl Uptime {
|
||||||
reply.push_str(&format!(
|
reply.push_str(&format!(
|
||||||
", connected since {} ({})",
|
", connected since {} ({})",
|
||||||
format_time(since),
|
format_time(since),
|
||||||
format_duration(since - now),
|
format_relative_time(since - now),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue