Module: ClaudeMemory::Core::RelativeTime

Defined in:
lib/claude_memory/core/relative_time.rb

Overview

Formats timestamps as human-readable relative time strings. Progressive granularity: just now → Xm ago → Xh ago → Xd ago → date

Constant Summary collapse

MINUTE =
60
HOUR =
3600
DAY =
86400

Class Method Summary collapse

Class Method Details

.format(timestamp, now: Time.now) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/claude_memory/core/relative_time.rb', line 12

def self.format(timestamp, now: Time.now)
  return nil if timestamp.nil?

  time = parse_time(timestamp)
  return nil unless time

  diff = now - time
  return format_absolute(time) if diff.negative?

  case diff
  when 0...MINUTE then "just now"
  when MINUTE...HOUR then "#{(diff / MINUTE).to_i}m ago"
  when HOUR...DAY then "#{(diff / HOUR).to_i}h ago"
  when DAY...(7 * DAY) then "#{(diff / DAY).to_i}d ago"
  else format_absolute(time)
  end
end

.format_absolute(time) ⇒ Object



49
50
51
# File 'lib/claude_memory/core/relative_time.rb', line 49

def self.format_absolute(time)
  time.strftime("%Y-%m-%d")
end

.parse_time(value) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/claude_memory/core/relative_time.rb', line 30

def self.parse_time(value)
  case value
  when Time then value
  when String then Time.parse(value)
  when Integer, Float then Time.at(value)
  end
rescue ArgumentError
  nil
end

.to_epoch(value) ⇒ Object

Parse a timestamp value into a Unix epoch integer; returns 0 when the value is unparseable. Used by sort comparators that need a stable numeric key without an exception path.



43
44
45
46
47
# File 'lib/claude_memory/core/relative_time.rb', line 43

def self.to_epoch(value)
  Time.parse(value.to_s).to_i
rescue ArgumentError, TypeError
  0
end