Module: Ace::Git::Atoms::TimeFormatter

Defined in:
lib/ace/git/atoms/time_formatter.rb

Overview

Pure functions for formatting timestamps as relative time strings Examples: “2h ago”, “1d ago”, “3w ago”

Constant Summary collapse

SECONDS_PER_MINUTE =

Time constants for format_duration calculations

60
MINUTES_PER_HOUR =
60
HOURS_PER_DAY =
24
DAYS_PER_WEEK =
7
DAYS_PER_MONTH =

Simplified (actual avg ~30.44)

30
DAYS_PER_YEAR =
365
MONTHS_PER_YEAR =
12

Class Method Summary collapse

Class Method Details

.relative_time(timestamp, reference_time: Time.now) ⇒ String

Convert an ISO8601 timestamp to relative time

Parameters:

  • timestamp (String, Time)

    ISO8601 timestamp or Time object

  • reference_time (Time) (defaults to: Time.now)

    Time to compare against (default: Time.now)

Returns:

  • (String)

    Relative time string like “2h ago”, “1d ago”, or empty for future times



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ace/git/atoms/time_formatter.rb', line 25

def relative_time(timestamp, reference_time: Time.now)
  return "" if timestamp.nil? || (timestamp.is_a?(String) && timestamp.empty?)

  time = parse_timestamp(timestamp)
  return "" if time.nil?

  seconds_ago = (reference_time - time).to_i
  # Don't format future times (negative duration)
  return "" if seconds_ago < 0

  format_duration(seconds_ago)
end