time_difference 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -2
- data/README.md +76 -39
- data/lib/time_difference.rb +21 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/time_difference_spec.rb +19 -6
- data/time_difference.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 521798b61869e9f427fbca86a655a6c58fb2d1dd
|
4
|
+
data.tar.gz: 057f1bca6e4d5635b8acf32cd9712f94c597f037
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af876c743e4defbd0595924b6f579e59d1d43bd0b421d27de0749f782bd9e78e5e2ad8fee73f88ccd9921755417093d18eec62c8814d1d2e573b8e2c945def79
|
7
|
+
data.tar.gz: a366fd8bf3be6e4e46c0c50e49ad93800b52c0703079b4ed273839458311747bcb0304f215e6c2307648c744618c0b979a3de2212a1e9b960855c6b4aaf45b61
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -8,63 +8,100 @@ TimeDifference is the missing Ruby method to calculate difference between two gi
|
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
|
-
|
11
|
+
```ruby
|
12
|
+
gem 'time_difference'
|
13
|
+
```
|
12
14
|
|
13
15
|
And then execute:
|
14
16
|
|
15
|
-
|
17
|
+
```bash
|
18
|
+
$ bundle install
|
19
|
+
```
|
16
20
|
|
17
21
|
## Usage
|
18
22
|
|
19
|
-
###
|
23
|
+
### Works for Time, DateTime, and Date
|
20
24
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
=> 1.0
|
25
|
+
```ruby
|
26
|
+
# Time
|
27
|
+
start_time = Time.new(2013,1)
|
28
|
+
end_time = Time.new(2014,1)
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
TimeDifference.between(start_time, end_time).in_years
|
30
|
-
=> 1.0
|
30
|
+
TimeDifference.between(start_time, end_time).in_years
|
31
|
+
=> 1.0
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
=> 1.0
|
33
|
+
# DateTime
|
34
|
+
start_time = DateTime.new(2013,1)
|
35
|
+
end_time = DateTime.new(2014,1)
|
36
36
|
|
37
|
-
|
37
|
+
TimeDifference.between(start_time, end_time).in_years
|
38
|
+
=> 1.0
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
# Date
|
41
|
+
start_time = Date.new(2013,1)
|
42
|
+
end_time = Date.new(2014,1)
|
43
|
+
|
44
|
+
TimeDifference.between(start_time, end_time).in_years
|
45
|
+
=> 1.0
|
46
|
+
```
|
47
|
+
|
48
|
+
### Get the time difference in various units
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
start_time = Time.new(2013,1)
|
52
|
+
end_time = Time.new(2014,1)
|
53
|
+
|
54
|
+
TimeDifference.between(start_time, end_time).in_years
|
55
|
+
=> 1.0
|
56
|
+
|
57
|
+
TimeDifference.between(start_time, end_time).in_months
|
58
|
+
=> 12.0
|
59
|
+
|
60
|
+
TimeDifference.between(start_time, end_time).in_weeks
|
61
|
+
=> 52.14
|
62
|
+
|
63
|
+
TimeDifference.between(start_time, end_time).in_days
|
64
|
+
=> 365.0
|
65
|
+
|
66
|
+
TimeDifference.between(start_time, end_time).in_hours
|
67
|
+
=> 8760.0
|
68
|
+
|
69
|
+
TimeDifference.between(start_time, end_time).in_minutes
|
70
|
+
=> 525600.0
|
71
|
+
|
72
|
+
TimeDifference.between(start_time, end_time).in_seconds
|
73
|
+
=> 31536000.0
|
74
|
+
```
|
43
75
|
|
44
76
|
### Get the time difference in each component
|
45
77
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
78
|
+
```ruby
|
79
|
+
start_time = Time.new(2013,1)
|
80
|
+
end_time = Time.new(2014,1)
|
81
|
+
|
82
|
+
TimeDifference.between(start_time, end_time).in_each_component
|
83
|
+
=> {:years=>1.0, :months=>12.0, :weeks=>52.14, :days=>365.0, :hours=>8760.0, :minutes=>525600.0, :seconds=>31536000.0}
|
84
|
+
```
|
85
|
+
|
86
|
+
### If you would like an overall estimated time component, use `in_general` _(not that accurate)_
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
start_time = Time.new(2013,1)
|
90
|
+
end_time = Time.new(2014,1)
|
50
91
|
|
51
|
-
|
92
|
+
TimeDifference.between(start_time, end_time).in_general
|
93
|
+
=> {:years=>0, :months=>12, :weeks=>0, :days=>5, :hours=>0, :minutes=>0, :seconds=>0}
|
94
|
+
```
|
52
95
|
|
53
|
-
|
54
|
-
end_time = Time.new(2014,1)
|
55
|
-
TimeDifference.between(start_time, end_time).in_general
|
56
|
-
=> {:years=>0, :months=>12, :weeks=>0, :days=>5, :hours=>0, :minutes=>0, :seconds=>0}
|
96
|
+
### You can also get `in_general` as a human readable string, using `humanize`
|
57
97
|
|
58
|
-
|
98
|
+
```ruby
|
99
|
+
start_time = Time.new(2013,1)
|
100
|
+
end_time = Time.new(2014,1)
|
59
101
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
in_days
|
64
|
-
in_hours
|
65
|
-
in_minutes
|
66
|
-
in_seconds
|
67
|
-
|
102
|
+
TimeDifference.between(start_time, end_time).humanize
|
103
|
+
=> "12 Months and 5 Days"
|
104
|
+
```
|
68
105
|
|
69
106
|
## Contributing
|
70
107
|
|
data/lib/time_difference.rb
CHANGED
@@ -56,6 +56,27 @@ class TimeDifference
|
|
56
56
|
end]
|
57
57
|
end
|
58
58
|
|
59
|
+
def humanize
|
60
|
+
diff_parts = []
|
61
|
+
in_general.each do |part,quantity|
|
62
|
+
next if quantity <= 0
|
63
|
+
part = part.to_s.humanize
|
64
|
+
|
65
|
+
if quantity <= 1
|
66
|
+
part = part.singularize
|
67
|
+
end
|
68
|
+
|
69
|
+
diff_parts << "#{quantity} #{part}"
|
70
|
+
end
|
71
|
+
|
72
|
+
last_part = diff_parts.pop
|
73
|
+
if diff_parts.empty?
|
74
|
+
return last_part
|
75
|
+
else
|
76
|
+
return [diff_parts.join(', '), last_part].join(' and ')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
59
80
|
private
|
60
81
|
|
61
82
|
def initialize(start_time, end_time)
|
data/spec/spec_helper.rb
CHANGED
@@ -13,11 +13,13 @@ describe TimeDifference do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
describe ".between" do
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
with_each_class do |clazz|
|
17
|
+
it "returns a new TimeDifference instance in each component" do
|
18
|
+
start_time = clazz.new(2011, 1)
|
19
|
+
end_time = clazz.new(2011, 12)
|
19
20
|
|
20
|
-
|
21
|
+
expect(TimeDifference.between(start_time, end_time)).to be_a(TimeDifference)
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
@@ -27,7 +29,7 @@ describe TimeDifference do
|
|
27
29
|
start_time = clazz.new(2011, 1)
|
28
30
|
end_time = clazz.new(2011, 12)
|
29
31
|
|
30
|
-
expect(TimeDifference.between(start_time, end_time).in_each_component).to eql({:
|
32
|
+
expect(TimeDifference.between(start_time, end_time).in_each_component).to eql({years: 0.91, months: 10.98, weeks: 47.71, days: 334.0, hours: 8016.0, minutes: 480960.0, seconds: 28857600.0})
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
@@ -38,7 +40,18 @@ describe TimeDifference do
|
|
38
40
|
start_time = clazz.new(2009, 11)
|
39
41
|
end_time = clazz.new(2011, 1)
|
40
42
|
|
41
|
-
expect(TimeDifference.between(start_time, end_time).in_general).to eql({:
|
43
|
+
expect(TimeDifference.between(start_time, end_time).in_general).to eql({years: 1, months: 2, weeks: 0, days: 0, hours: 18, minutes: 0, seconds: 0})
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#humanize" do
|
49
|
+
with_each_class do |clazz|
|
50
|
+
it "returns a string representing the time difference from in_general" do
|
51
|
+
start_time = clazz.new(2009, 11)
|
52
|
+
end_time = clazz.new(2011, 1)
|
53
|
+
|
54
|
+
expect(TimeDifference.between(start_time, end_time).humanize).to eql("1 Year, 2 Months and 18 Hours")
|
42
55
|
end
|
43
56
|
end
|
44
57
|
end
|
data/time_difference.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
12
|
gem.name = "time_difference"
|
13
13
|
gem.require_paths = ["lib"]
|
14
|
-
gem.version = "0.
|
14
|
+
gem.version = "0.5.0"
|
15
15
|
gem.license = 'MIT'
|
16
16
|
|
17
17
|
gem.add_runtime_dependency('activesupport')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_difference
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TM Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.5.0
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: TimeDifference is the missing Ruby method to calculate difference between
|