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

Function format_timestamp_relative

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

498/// // Output: "2 hours ago"
499/// ```
500pub fn format_timestamp_relative(timestamp: &DateTime<Utc>) -> String {
501 let now = Utc::now();
502 let duration = now.signed_duration_since(*timestamp);
503
504 if duration.num_seconds() < 60 {
505 "just now".to_string()
506 } else if duration.num_minutes() < 60 {
507 let mins = duration.num_minutes();
508 if mins == 1 {
509 "1 minute ago".to_string()
510 } else {
511 format!("{} minutes ago", mins)
512 }
513 } else if duration.num_hours() < 24 {
514 let hours = duration.num_hours();
515 if hours == 1 {
516 "1 hour ago".to_string()
517 } else {
518 format!("{} hours ago", hours)
519 }
520 } else if duration.num_days() < 7 {
521 let days = duration.num_days();
522 if days == 1 {
523 "yesterday".to_string()
524 } else {
525 format!("{} days ago", days)
526 }
527 } else if duration.num_weeks() < 4 {
528 let weeks = duration.num_weeks();
529 if weeks == 1 {
530 "1 week ago".to_string()
531 } else {
532 format!("{} weeks ago", weeks)
533 }
534 } else {
535 // For older dates, use absolute format
536 format_timestamp(timestamp)
537 }
538}
539
540/// Format a byte size in human-readable form.
541///

Calls 1

format_timestampFunction · 0.85