Sha256: f84d2e8341edace5354a8926f0fd7c5f3bc62d912ee4f2ee30ccfc8c82ae396f

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

module Jets::Util
  module FormatTime
    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

    # Simple implementation of time_ago_in_words so we dont have to include ActionView::Helpers::DateHelper
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jets-6.0.5 lib/jets/util/format_time.rb
jets-6.0.4 lib/jets/util/format_time.rb
jets-6.0.3 lib/jets/util/format_time.rb
jets-6.0.2 lib/jets/util/format_time.rb