Method: ICFS.time_relative

Defined in:
lib/icfs.rb

.time_relative(tme) ⇒ String

Get relative display time from epoch

Parameters:

  • tme (Integer)

    the epoch time

Returns:

  • (String)

    Relative time string



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/icfs.rb', line 264

def self.time_relative(tme)
  rel = Time.now.to_i - tme
  abs = rel.abs

  # time periods
  if abs >= (2*365*24*60*60)
    num = abs / (365*24*60*60)
    txt = 'years'
  elsif abs >= (3*7*24*60*60)
    num = abs / (7*24*60*60)
    txt = 'weeks'
  elsif abs >= (3*24*60*60)
    num = abs / (24*60*60)
    txt = 'days'
  elsif abs >= (3*60*60)
    num = abs / (60*60)
    txt = 'hours'
  elsif abs >= 2*60
    num = abs / 60
    txt = 'minutes'
  else
    num = 0
    txt = 'just now'
  end

  # description
  desc = '%d %s' % [num, txt]

  # before/after
  if num == 0
    final = 'just now'
  elsif rel < 0
    final = 'in ' + desc
  else
    final = desc + ' ago'
  end

  return final
end