since_when 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +18 -2
- data/lib/since_when/version.rb +1 -1
- data/lib/since_when.rb +38 -44
- metadata +1 -1
data/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# SinceWhen
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Returns a string containing the amount of time since the date given with
|
|
4
|
+
varying scopes of specificy.
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -18,7 +19,22 @@ Or install it yourself as:
|
|
|
18
19
|
|
|
19
20
|
## Usage
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
SinceWhen::time_since( date, :scope )
|
|
23
|
+
|
|
24
|
+
Example:
|
|
25
|
+
|
|
26
|
+
example_date = Date.new(2001, 2, 3)
|
|
27
|
+
|
|
28
|
+
SinceWhen::time_since( example_date, :month )
|
|
29
|
+
|
|
30
|
+
>> "12 years 8 months"
|
|
31
|
+
|
|
32
|
+
# or
|
|
33
|
+
|
|
34
|
+
SinceWhen::time_since( example_date, :day)
|
|
35
|
+
|
|
36
|
+
>> "12 years 8 months 12 days"
|
|
37
|
+
|
|
22
38
|
|
|
23
39
|
## Contributing
|
|
24
40
|
|
data/lib/since_when/version.rb
CHANGED
data/lib/since_when.rb
CHANGED
|
@@ -2,75 +2,69 @@ require "since_when/version"
|
|
|
2
2
|
|
|
3
3
|
module SinceWhen
|
|
4
4
|
def self.time_since date, scope = :month
|
|
5
|
-
|
|
6
5
|
days_in_month = [31, date.year%4 == 0 ? 29 : 28 , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
|
6
|
+
scope_arr = %w(year month day hour minute second)
|
|
7
|
+
|
|
7
8
|
time_string = ""
|
|
8
9
|
|
|
9
10
|
if date.class != Date
|
|
10
11
|
date = date.to_datetime
|
|
12
|
+
hour_dif = DateTime.now.hour - date.hour
|
|
13
|
+
minute_dif = DateTime.now.minute - date.minute
|
|
14
|
+
second_dif = DateTime.now.second - date.second
|
|
11
15
|
end
|
|
12
16
|
|
|
13
17
|
day_dif = Date.today.day - date.day
|
|
14
18
|
month_dif = Date.today.month - date.month
|
|
15
19
|
year_dif = Date.today.year - date.year
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
end
|
|
29
|
-
unless scope == :minute
|
|
30
|
-
second_dif = DateTime.now.second - date.second
|
|
31
|
-
if second_dif < 0
|
|
32
|
-
minute_dif -= 1
|
|
33
|
-
second_dif += 60
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
21
|
+
if second_dif && second_dif < 0
|
|
22
|
+
minute_dif -= 1
|
|
23
|
+
second_dif += 60
|
|
24
|
+
end
|
|
25
|
+
if minute_dif && minute_dif < 0
|
|
26
|
+
hour_dif -= 1
|
|
27
|
+
minute_dif += 60
|
|
28
|
+
end
|
|
29
|
+
if hour_dif && hour_dif < 0
|
|
30
|
+
day_dif -= 1
|
|
31
|
+
hour_dif += 24
|
|
37
32
|
end
|
|
38
|
-
|
|
39
33
|
if day_dif < 0
|
|
40
34
|
month_dif -= 1
|
|
41
|
-
|
|
42
|
-
day_dif += days_in_month[date.month + 1]
|
|
43
|
-
end
|
|
35
|
+
day_dif += days_in_month[date.month + 1]
|
|
44
36
|
end
|
|
45
|
-
|
|
46
37
|
if month_dif < 0
|
|
47
38
|
year_dif -= 1
|
|
48
39
|
month_dif += 12
|
|
49
40
|
end
|
|
50
41
|
|
|
51
|
-
if year_dif > 0
|
|
52
|
-
time_string << "#{year_dif} #{year_dif > 1 ? 'years' : 'year' }"
|
|
53
|
-
end
|
|
54
42
|
|
|
55
|
-
if
|
|
56
|
-
|
|
57
|
-
|
|
43
|
+
if scope_arr.include?(scope.to_s)
|
|
44
|
+
if year_dif > 0
|
|
45
|
+
time_string << "#{year_dif} #{year_dif > 1 ? 'years' : 'year' }"
|
|
46
|
+
end
|
|
58
47
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
time_string << " #{day_dif} #{day_dif > 1 ? 'days' : 'day'}"
|
|
48
|
+
if month_dif > 0
|
|
49
|
+
time_string << " #{month_dif} #{month_dif > 1 ? 'months' : 'month'}"
|
|
62
50
|
end
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
51
|
+
|
|
52
|
+
unless scope == :month
|
|
53
|
+
if day_dif > 0
|
|
54
|
+
time_string << " #{day_dif} #{day_dif > 1 ? 'days' : 'day'}"
|
|
66
55
|
end
|
|
67
|
-
unless scope == :
|
|
68
|
-
if
|
|
69
|
-
time_string << " #{
|
|
56
|
+
unless scope == :day
|
|
57
|
+
if hour_dif > 0
|
|
58
|
+
time_string << " #{hour_dif} #{hour_dif > 1 ? 'hours' : 'hour'}"
|
|
70
59
|
end
|
|
71
|
-
unless scope == :
|
|
72
|
-
if
|
|
73
|
-
time_string << " #{
|
|
60
|
+
unless scope == :hour
|
|
61
|
+
if minute_dif > 0
|
|
62
|
+
time_string << " #{minute_dif} #{minute_dif > 1 ? 'minutes' : 'minute'}"
|
|
63
|
+
end
|
|
64
|
+
unless scope == :minute
|
|
65
|
+
if second_dif > 0
|
|
66
|
+
time_string << " #{second_dif} #{second_dif > 1 ? 'seconds' : 'second'}"
|
|
67
|
+
end
|
|
74
68
|
end
|
|
75
69
|
end
|
|
76
70
|
end
|