Describe how long ago `then` was, relative to `now`, in coarse units.
(now: &str, then: &str)
| 606 | |
| 607 | /// Describe how long ago `then` was, relative to `now`, in coarse units. |
| 608 | fn relative_age(now: &str, then: &str) -> String { |
| 609 | let (Ok(now), Ok(then)) = ( |
| 610 | chrono::DateTime::parse_from_rfc3339(now), |
| 611 | chrono::DateTime::parse_from_rfc3339(then), |
| 612 | ) else { |
| 613 | return "recently".to_string(); |
| 614 | }; |
| 615 | let secs = (now - then).num_seconds().max(0); |
| 616 | match secs { |
| 617 | 0..=59 => format!("{secs}s ago"), |
| 618 | 60..=3599 => format!("{}m ago", secs / 60), |
| 619 | 3600..=86_399 => format!("{}h ago", secs / 3600), |
| 620 | _ => format!("{}d ago", secs / 86_400), |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | #[cfg(test)] |
| 625 | mod tests { |