Class: TimeAgo

Inherits:
Object
  • Object
show all
Defined in:
lib/timeago.rb

Class Method Summary collapse

Class Method Details

.distance_of_time_in_words(minutes) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/timeago.rb', line 34

def self.distance_of_time_in_words(minutes)
	case
 	when minutes < 0.4
 		"a few seconds"
 	when minutes < 0.6
   	"half a minute"
	when minutes < 1
		"less than a minute"
	when minutes < 2
		"one minute"
	when minutes < 45
		"#{minutes} minutes"
	when minutes < 59
 		"less than one hour"
	when minutes < 120
		"one hour"
	when minutes < 1080
		"#{(minutes / 60).round} hours"
	when minutes < 1440
		"one day"
	when minutes < 2880
		"about one day"
	when minutes < 8640
		"#{(minutes / 1440).round} days"
	when minutes < 10080
		"one week"
 	when minutes < 40320
		"#{(minutes / 8640).round} weeks"
	when minutes < 43200
		"one month"
  when minutes < 525580
		"#{(minutes / 43200).round} months"
	when minutes < 525600
		"one year"
  when minutes < 2628000
		"#{(minutes / 525600).round} years"
	else
		"#{minutes} minutes"
	end
end

.readable(time) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/timeago.rb', line 10

def self.readable(time)

  rightnow = Time.new
  backthen = time

  delta_setting = (rightnow.to_i - backthen.to_i).floor / 60

distance = distance_of_time_in_words(delta_setting)
return "#{distance} ago"

if rightnow.to_i == backthen.to_i
  "just now"
  elsif delta_minutes.abs <= (8724*60)       
  		distance = distance_of_time_in_words(delta_minutes)       
  		if distance <= (8724*60)
  			return "#{distance} from now"
  		else
  			return "#{distance} ago"
  		end
  else
		return "on #{DateTime.now.to_formatted_s(date_format)}"
	end
end

.right_nowObject



5
6
7
8
# File 'lib/timeago.rb', line 5

def self.right_now
  time = Time.new
time.strftime("%I:%M:%S %p")
end