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

Function format_timestamp_relative

atomic-cli/src/commands/mod.rs:485–523  ·  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

483/// // Output: "2 hours ago"
484/// ```
485pub fn format_timestamp_relative(timestamp: &DateTime<Utc>) -> String {
486 let now = Utc::now();
487 let duration = now.signed_duration_since(*timestamp);
488
489 if duration.num_seconds() < 60 {
490 "just now".to_string()
491 } else if duration.num_minutes() < 60 {
492 let mins = duration.num_minutes();
493 if mins == 1 {
494 "1 minute ago".to_string()
495 } else {
496 format!("{} minutes ago", mins)
497 }
498 } else if duration.num_hours() < 24 {
499 let hours = duration.num_hours();
500 if hours == 1 {
501 "1 hour ago".to_string()
502 } else {
503 format!("{} hours ago", hours)
504 }
505 } else if duration.num_days() < 7 {
506 let days = duration.num_days();
507 if days == 1 {
508 "yesterday".to_string()
509 } else {
510 format!("{} days ago", days)
511 }
512 } else if duration.num_weeks() < 4 {
513 let weeks = duration.num_weeks();
514 if weeks == 1 {
515 "1 week ago".to_string()
516 } else {
517 format!("{} weeks ago", weeks)
518 }
519 } else {
520 // For older dates, use absolute format
521 format_timestamp(timestamp)
522 }
523}
524
525/// Format a byte size in human-readable form.
526///

Calls 1

format_timestampFunction · 0.85