MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / format_timestamp_relative

Function format_timestamp_relative

atomic-cli/src/commands/mod.rs:602–640  ·  view source on GitHub ↗

Format a timestamp in a relative format (e.g., "2 hours ago"). This provides a more conversational representation of time that's easier to understand at a glance. # Arguments `timestamp` - The UTC timestamp to format # Returns A relative time string. # Example ```rust,ignore let ts = Utc::now() - chrono::Duration::hours(2); println!("{}", format_timestamp_relative(&ts)); // Output: "2 hours

(timestamp: &DateTime<Utc>)

Source from the content-addressed store, hash-verified

600/// // Output: "2 hours ago"
601/// ```
602pub fn format_timestamp_relative(timestamp: &DateTime<Utc>) -> String {
603 let now = Utc::now();
604 let duration = now.signed_duration_since(*timestamp);
605
606 if duration.num_seconds() < 60 {
607 "just now".to_string()
608 } else if duration.num_minutes() < 60 {
609 let mins = duration.num_minutes();
610 if mins == 1 {
611 "1 minute ago".to_string()
612 } else {
613 format!("{} minutes ago", mins)
614 }
615 } else if duration.num_hours() < 24 {
616 let hours = duration.num_hours();
617 if hours == 1 {
618 "1 hour ago".to_string()
619 } else {
620 format!("{} hours ago", hours)
621 }
622 } else if duration.num_days() < 7 {
623 let days = duration.num_days();
624 if days == 1 {
625 "yesterday".to_string()
626 } else {
627 format!("{} days ago", days)
628 }
629 } else if duration.num_weeks() < 4 {
630 let weeks = duration.num_weeks();
631 if weeks == 1 {
632 "1 week ago".to_string()
633 } else {
634 format!("{} weeks ago", weeks)
635 }
636 } else {
637 // For older dates, use absolute format
638 format_timestamp(timestamp)
639 }
640}
641
642/// Format a byte size in human-readable form.
643///

Calls 1

format_timestampFunction · 0.85