Module: Jets::Util::FormatTime

Included in:
CLI::Ci::Status, CLI::Release::History
Defined in:
lib/jets/util/format_time.rb

Instance Method Summary collapse

Instance Method Details

#pretty_time(time) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jets/util/format_time.rb', line 3

def pretty_time(time)
  datetime = case time
  when Time
    time.to_datetime
  when String
    DateTime.parse(time)
  else
    time
  end

  if datetime > 1.day.ago.utc
    time_ago_in_words(datetime) + " ago"
  else
    tz_override = ENV["JETS_TZ"] # IE: America/Los_Angeles
    local = if tz_override
      tz = TZInfo::Timezone.get(tz_override)
      tz.time_to_local(datetime)
    else
      datetime.new_offset(DateTime.now.offset) # local time
    end

    if tz_override
      local.strftime("%b %-d, %Y %-l:%M:%S%P")
    else
      local.strftime("%b %-d, %Y %H:%M:%S")
    end
  end
end

#time_ago_in_words(from_time, to_time = Time.now) ⇒ Object

Simple implementation of time_ago_in_words so we dont have to include ActionView::Helpers::DateHelper



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jets/util/format_time.rb', line 33

def time_ago_in_words(from_time, to_time = Time.now)
  distance_in_seconds = (to_time - from_time).to_i
  case distance_in_seconds
  when 0..59
    "#{distance_in_seconds} #{"second".pluralize(distance_in_seconds)}"
  when 60..3599
    minutes = distance_in_seconds / 60
    "#{minutes} #{"minute".pluralize(minutes)}"
  when 3600..86_399
    hours = distance_in_seconds / 3600
    "#{hours} #{"hour".pluralize(hours)}"
  when 86_400..604_799
    days = distance_in_seconds / 86_400
    "#{days} #{"day".pluralize(days)}"
  else
    from_time.strftime("%B %d, %Y")
  end
end