Render a duration as a short human phrase (`in 30d`, `3d ago`).
(remaining: Option<Duration>)
| 228 | |
| 229 | /// Render a duration as a short human phrase (`in 30d`, `3d ago`). |
| 230 | pub fn humanize_remaining(remaining: Option<Duration>) -> String { |
| 231 | let Some(remaining) = remaining else { |
| 232 | return "never".to_string(); |
| 233 | }; |
| 234 | let days = remaining.num_days(); |
| 235 | let hours = remaining.num_hours(); |
| 236 | |
| 237 | if remaining.num_seconds() < 0 { |
| 238 | let ago = -remaining; |
| 239 | return if ago.num_days() > 0 { |
| 240 | format!("{}d ago", ago.num_days()) |
| 241 | } else { |
| 242 | format!("{}h ago", ago.num_hours().max(1)) |
| 243 | }; |
| 244 | } |
| 245 | if days > 0 { |
| 246 | format!("in {days}d") |
| 247 | } else if hours > 0 { |
| 248 | format!("in {hours}h") |
| 249 | } else { |
| 250 | "in <1h".to_string() |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | #[cfg(test)] |
| 255 | mod tests { |