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

Function format_timestamp_relative

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

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

Calls 1

format_timestampFunction · 0.85