fix(tui): correct relative time wording
This commit is contained in:
1 parent
ea7478eb27
commit
3bee6d8338
4 files changed
+37
-29
No files matched your search
+26
@@ -0,0 +1,26 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
// Rough human-friendly time delta. "2 months ago" is close enough when months
|
||||
// are about 30 days.
|
||||
pub(crate) fn format_relative_time(timestamp: Option<DateTime<Utc>>) -> String {
|
||||
let Some(timestamp) = timestamp else {
|
||||
return "Never played".to_string();
|
||||
};
|
||||
let seconds = Utc::now()
|
||||
.signed_duration_since(timestamp)
|
||||
.num_seconds()
|
||||
.max(0) as u64;
|
||||
match seconds {
|
||||
0..=59 => "Just now".to_string(),
|
||||
60..=3599 => ago(seconds / 60, "minute"),
|
||||
3600..=86399 => ago(seconds / 3600, "hour"),
|
||||
86400..=2591999 => ago(seconds / 86400, "day"),
|
||||
2592000..=31535999 => ago(seconds / 2592000, "month"),
|
||||
_ => "Over a year ago".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ago(value: u64, unit: &str) -> String {
|
||||
let plural = if value == 1 { "" } else { "s" };
|
||||
format!("{value} {unit}{plural} ago")
|
||||
}
|
||||
Reference in new issue
Block a user