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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3f41189c3216e381c0b064cde1fd7de7bad4a7bf
4
- data.tar.gz: 79c6dff109309e90f0b0303b927b47ba5521e286
3
+ metadata.gz: 521798b61869e9f427fbca86a655a6c58fb2d1dd
4
+ data.tar.gz: 057f1bca6e4d5635b8acf32cd9712f94c597f037
5
5
  SHA512:
6
- metadata.gz: 423c17b810279a3b072d423542ea31ea985a97644aa91096ffc1932bf05b6841599a6d8c81ee44901bed63fdd85a469debd573370f0cde6359574052804ad92c
7
- data.tar.gz: d8b9992a80bf9b35fbda3d8d04ecccbd104aefa23f71c7457926ec9dad47f7967a45059904c3cfd5ffab5fd8d331d2deaa17820944f7fb1581773e52c5417162
6
+ metadata.gz: af876c743e4defbd0595924b6f579e59d1d43bd0b421d27de0749f782bd9e78e5e2ad8fee73f88ccd9921755417093d18eec62c8814d1d2e573b8e2c945def79
7
+ data.tar.gz: a366fd8bf3be6e4e46c0c50e49ad93800b52c0703079b4ed273839458311747bcb0304f215e6c2307648c744618c0b979a3de2212a1e9b960855c6b4aaf45b61
@@ -7,5 +7,8 @@ rvm:
7
7
  # uncomment this line if your project needs to run something other than `rake`:
8
8
  script: bundle exec rspec spec
9
9
  gemfile:
10
- - Gemfile.activesupport32
11
- - Gemfile
10
+ - Gemfile.activesupport32
11
+ - Gemfile
12
+
13
+ before_install:
14
+ - gem install bundler
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
- gem 'time_difference'
11
+ ```ruby
12
+ gem 'time_difference'
13
+ ```
12
14
 
13
15
  And then execute:
14
16
 
15
- $ bundle
17
+ ```bash
18
+ $ bundle install
19
+ ```
16
20
 
17
21
  ## Usage
18
22
 
19
- ### Get the time difference in year
23
+ ### Works for Time, DateTime, and Date
20
24
 
21
- # Works for Time, DateTime, and Date
22
- start_time = Time.new(2013,1)
23
- end_time = Time.new(2014,1)
24
- TimeDifference.between(start_time, end_time).in_years
25
- => 1.0
25
+ ```ruby
26
+ # Time
27
+ start_time = Time.new(2013,1)
28
+ end_time = Time.new(2014,1)
26
29
 
27
- start_time = DateTime.new(2013,1)
28
- end_time = DateTime.new(2014,1)
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
- start_time = Date.new(2013,1)
33
- end_time = Date.new(2014,1)
34
- TimeDifference.between(start_time, end_time).in_years
35
- => 1.0
33
+ # DateTime
34
+ start_time = DateTime.new(2013,1)
35
+ end_time = DateTime.new(2014,1)
36
36
 
37
- ### Get the time difference in months
37
+ TimeDifference.between(start_time, end_time).in_years
38
+ => 1.0
38
39
 
39
- start_time = Time.new(2013,1)
40
- end_time = Time.new(2014,1)
41
- TimeDifference.between(start_time, end_time).in_months
42
- => 12.0
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
- start_time = Time.new(2013,1)
47
- end_time = Time.new(2014,1)
48
- TimeDifference.between(start_time, end_time).in_each_component
49
- => {:years=>1.0, :months=>12.0, :weeks=>52.14, :days=>365.0, :hours=>8760.0, :minutes=>525600.0, :seconds=>31536000.0}
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
- ### If you would like an overall estimated time component, use in_general (not that accurate)
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
- start_time = Time.new(2013,1)
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
- ### Supported time difference includes
98
+ ```ruby
99
+ start_time = Time.new(2013,1)
100
+ end_time = Time.new(2014,1)
59
101
 
60
- in_years
61
- in_months
62
- in_weeks
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
 
@@ -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)
@@ -4,4 +4,7 @@ require 'time_difference'
4
4
  RSpec.configure do |config|
5
5
  config.color_enabled = true
6
6
  config.formatter = 'documentation'
7
+
8
+ # Configure Timezone for proper tests
9
+ ENV['TZ'] = 'UTC'
7
10
  end
@@ -13,11 +13,13 @@ describe TimeDifference do
13
13
  end
14
14
 
15
15
  describe ".between" do
16
- it "returns a new TimeDifference instance" do
17
- start_time = Time.new(2011, 1)
18
- end_time = Time.new(2011, 12)
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
- expect(TimeDifference.between(start_time, end_time)).to be_a(TimeDifference)
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({:years => 0.91, :months => 10.98, :weeks => 47.71, :days => 334.0, :hours => 8016.0, :minutes => 480960.0, :seconds => 28857600.0})
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({:years => 1, :months => 2, :weeks => 0, :days => 0, :hours => 18, :minutes => 0, :seconds => 0})
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
@@ -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.4.2"
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.2
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: 2015-03-24 00:00:00.000000000 Z
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.4.5
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