time_difference 0.3.2 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f41189c3216e381c0b064cde1fd7de7bad4a7bf
4
+ data.tar.gz: 79c6dff109309e90f0b0303b927b47ba5521e286
5
+ SHA512:
6
+ metadata.gz: 423c17b810279a3b072d423542ea31ea985a97644aa91096ffc1932bf05b6841599a6d8c81ee44901bed63fdd85a469debd573370f0cde6359574052804ad92c
7
+ data.tar.gz: d8b9992a80bf9b35fbda3d8d04ecccbd104aefa23f71c7457926ec9dad47f7967a45059904c3cfd5ffab5fd8d331d2deaa17820944f7fb1581773e52c5417162
@@ -1,7 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - "1.9.3"
4
+ - 2.2
4
5
  - jruby-19mode # JRuby in 1.9 mode
5
6
  - rbx-19mode
6
7
  # uncomment this line if your project needs to run something other than `rake`:
7
- script: bundle exec rspec spec
8
+ script: bundle exec rspec spec
9
+ gemfile:
10
+ - Gemfile.activesupport32
11
+ - Gemfile
@@ -1,3 +1,6 @@
1
+ ## v0.4.2
2
+ * Support Time, DateTime, and Date class as input
3
+
1
4
  ## v0.3.1
2
5
  * Support in_each_component as a shortcut for all time components
3
6
 
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ gem 'activesupport', '~> 3.2'
data/README.md CHANGED
@@ -18,11 +18,22 @@ And then execute:
18
18
 
19
19
  ### Get the time difference in year
20
20
 
21
+ # Works for Time, DateTime, and Date
21
22
  start_time = Time.new(2013,1)
22
23
  end_time = Time.new(2014,1)
23
24
  TimeDifference.between(start_time, end_time).in_years
24
25
  => 1.0
25
26
 
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
31
+
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
36
+
26
37
  ### Get the time difference in months
27
38
 
28
39
  start_time = Time.new(2013,1)
@@ -3,46 +3,74 @@ require "active_support/all"
3
3
 
4
4
  class TimeDifference
5
5
 
6
+ private_class_method :new
7
+
8
+ TIME_COMPONENTS = [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
9
+
6
10
  def self.between(start_time, end_time)
7
- @time_diff = end_time - start_time
8
- self
9
- end
10
-
11
- class << self
12
-
13
- [:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
14
- self.class.instance_eval do
15
- define_method("in_#{time_component}") do
16
- if time_component == :months
17
- ((@time_diff/(1.days * 30.42)).round(2)).abs
18
- else
19
- ((@time_diff/1.send(time_component)).round(2)).abs
20
- end
21
- end
22
- end
23
- end
24
-
25
- end
26
-
27
- def self.in_each_component
28
- time_in_each_component = {}
29
- [:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
30
- if time_component == :months
31
- time_in_each_component[time_component] = ((@time_diff/(1.days * 30.42)).round(2)).abs
32
- else
33
- time_in_each_component[time_component] = ((@time_diff/1.send(time_component)).round(2)).abs
34
- end
35
- end
36
- time_in_each_component
37
- end
38
-
39
- def self.in_general
40
- result = {}
41
- [:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
42
- result[time_component] = (@time_diff/1.send(time_component)).floor
43
- @time_diff = (@time_diff - result[time_component].send(time_component))
44
- end
45
- result
11
+ new(start_time, end_time)
12
+ end
13
+
14
+ def in_years
15
+ in_component(:years)
16
+ end
17
+
18
+ def in_months
19
+ (@time_diff / (1.day * 30.42)).round(2)
20
+ end
21
+
22
+ def in_weeks
23
+ in_component(:weeks)
24
+ end
25
+
26
+ def in_days
27
+ in_component(:days)
28
+ end
29
+
30
+ def in_hours
31
+ in_component(:hours)
32
+ end
33
+
34
+ def in_minutes
35
+ in_component(:minutes)
36
+ end
37
+
38
+ def in_seconds
39
+ @time_diff
40
+ end
41
+
42
+ def in_each_component
43
+ Hash[TIME_COMPONENTS.map do |time_component|
44
+ [time_component, public_send("in_#{time_component}")]
45
+ end]
46
+ end
47
+
48
+ def in_general
49
+ remaining = @time_diff
50
+
51
+ Hash[TIME_COMPONENTS.map do |time_component|
52
+ rounded_time_component = (remaining / 1.send(time_component)).floor
53
+ remaining -= rounded_time_component.send(time_component)
54
+
55
+ [time_component, rounded_time_component]
56
+ end]
57
+ end
58
+
59
+ private
60
+
61
+ def initialize(start_time, end_time)
62
+ start_time = time_in_seconds(start_time)
63
+ end_time = time_in_seconds(end_time)
64
+
65
+ @time_diff = (end_time - start_time).abs
66
+ end
67
+
68
+ def time_in_seconds(time)
69
+ time.to_time.to_f
70
+ end
71
+
72
+ def in_component(component)
73
+ (@time_diff / 1.send(component)).round(2)
46
74
  end
47
75
 
48
76
  end
@@ -2,38 +2,170 @@ require 'spec_helper'
2
2
 
3
3
  describe TimeDifference do
4
4
 
5
- it "returns time difference in each component" do
6
- start_time = Time.new(2011,1)
7
- end_time = Time.new(2011,12)
8
- 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})
9
- end
10
-
11
- it "returns time difference in general that matches the total seconds" do
12
- start_time = Time.new(2009,9)
13
- end_time = Time.new(2010,11)
14
- 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})
15
- end
16
-
17
- it "returns time difference based on Wolfram Alpha" do
18
- start_time = Time.new(2011,1)
19
- end_time = Time.new(2011,12)
20
- expect(TimeDifference.between(start_time, end_time).in_years).to eql 0.91
21
- expect(TimeDifference.between(start_time, end_time).in_months).to eql 10.98
22
- expect(TimeDifference.between(start_time, end_time).in_weeks).to eql 47.71
23
- expect(TimeDifference.between(start_time, end_time).in_days).to eql 334.0
24
- expect(TimeDifference.between(start_time, end_time).in_hours).to eql 8016.0
25
- expect(TimeDifference.between(start_time, end_time).in_minutes).to eql 480960.0
26
- end
27
-
28
- it "returns time difference in absolute value regardless how it is minus-ed out" do
29
- start_time = Time.new(2011,1)
30
- end_time = Time.new(2011,12)
31
- expect(TimeDifference.between(end_time, start_time).in_years).to eql 0.91
32
- expect(TimeDifference.between(end_time, start_time).in_months).to eql 10.98
33
- expect(TimeDifference.between(end_time, start_time).in_weeks).to eql 47.71
34
- expect(TimeDifference.between(end_time, start_time).in_days).to eql 334.0
35
- expect(TimeDifference.between(end_time, start_time).in_hours).to eql 8016.0
36
- expect(TimeDifference.between(end_time, start_time).in_minutes).to eql 480960.0
37
- end
38
-
39
- end
5
+ def self.with_each_class(&block)
6
+ classes = [Time, Date, DateTime]
7
+
8
+ classes.each do |clazz|
9
+ context "with a #{clazz.name} class" do
10
+ instance_exec clazz, &block
11
+ end
12
+ end
13
+ end
14
+
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)
19
+
20
+ expect(TimeDifference.between(start_time, end_time)).to be_a(TimeDifference)
21
+ end
22
+ end
23
+
24
+ describe "#in_each_component" do
25
+ with_each_class do |clazz|
26
+ it "returns time difference in each component" do
27
+ start_time = clazz.new(2011, 1)
28
+ end_time = clazz.new(2011, 12)
29
+
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})
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#in_general" do
36
+ with_each_class do |clazz|
37
+ it "returns time difference in general that matches the total seconds" do
38
+ start_time = clazz.new(2009, 11)
39
+ end_time = clazz.new(2011, 1)
40
+
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})
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#in_years" do
47
+ with_each_class do |clazz|
48
+ it "returns time difference in years based on Wolfram Alpha" do
49
+ start_time = clazz.new(2011, 1)
50
+ end_time = clazz.new(2011, 12)
51
+
52
+ expect(TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
53
+ end
54
+
55
+ it "returns an absolute difference" do
56
+ start_time = clazz.new(2011, 12)
57
+ end_time = clazz.new(2011, 1)
58
+
59
+ expect(TimeDifference.between(start_time, end_time).in_years).to eql(0.91)
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#in_months" do
65
+ with_each_class do |clazz|
66
+ it "returns time difference in months based on Wolfram Alpha" do
67
+ start_time = clazz.new(2011, 1)
68
+ end_time = clazz.new(2011, 12)
69
+
70
+ expect(TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
71
+ end
72
+
73
+ it "returns an absolute difference" do
74
+ start_time = clazz.new(2011, 12)
75
+ end_time = clazz.new(2011, 1)
76
+
77
+ expect(TimeDifference.between(start_time, end_time).in_months).to eql(10.98)
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "#in_weeks" do
83
+ with_each_class do |clazz|
84
+ it "returns time difference in weeks based on Wolfram Alpha" do
85
+ start_time = clazz.new(2011, 1)
86
+ end_time = clazz.new(2011, 12)
87
+
88
+ expect(TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
89
+ end
90
+
91
+ it "returns an absolute difference" do
92
+ start_time = clazz.new(2011, 12)
93
+ end_time = clazz.new(2011, 1)
94
+
95
+ expect(TimeDifference.between(start_time, end_time).in_weeks).to eql(47.71)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#in_days" do
101
+ with_each_class do |clazz|
102
+ it "returns time difference in weeks based on Wolfram Alpha" do
103
+ start_time = clazz.new(2011, 1)
104
+ end_time = clazz.new(2011, 12)
105
+
106
+ expect(TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
107
+ end
108
+
109
+ it "returns an absolute difference" do
110
+ start_time = clazz.new(2011, 12)
111
+ end_time = clazz.new(2011, 1)
112
+
113
+ expect(TimeDifference.between(start_time, end_time).in_days).to eql(334.0)
114
+ end
115
+ end
116
+ end
117
+
118
+ describe "#in_hours" do
119
+ with_each_class do |clazz|
120
+ it "returns time difference in hours based on Wolfram Alpha" do
121
+ start_time = clazz.new(2011, 1)
122
+ end_time = clazz.new(2011, 12)
123
+
124
+ expect(TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
125
+ end
126
+
127
+ it "returns an absolute difference" do
128
+ start_time = clazz.new(2011, 12)
129
+ end_time = clazz.new(2011, 1)
130
+
131
+ expect(TimeDifference.between(start_time, end_time).in_hours).to eql(8016.0)
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "#in_minutes" do
137
+ with_each_class do |clazz|
138
+ it "returns time difference in minutes based on Wolfram Alpha" do
139
+ start_time = clazz.new(2011, 1)
140
+ end_time = clazz.new(2011, 12)
141
+
142
+ expect(TimeDifference.between(start_time, end_time).in_minutes).to eql(480960.0)
143
+ end
144
+
145
+ it "returns an absolute difference" do
146
+ start_time = clazz.new(2011, 12)
147
+ end_time = clazz.new(2011, 1)
148
+
149
+ expect(TimeDifference.between(start_time, end_time).in_minutes).to eql(480960.0)
150
+ end
151
+ end
152
+ end
153
+
154
+ describe "#in_seconds" do
155
+ with_each_class do |clazz|
156
+ it "returns time difference in seconds based on Wolfram Alpha" do
157
+ start_time = clazz.new(2011, 1)
158
+ end_time = clazz.new(2011, 12)
159
+
160
+ expect(TimeDifference.between(start_time, end_time).in_seconds).to eql(28857600.0)
161
+ end
162
+
163
+ it "returns an absolute difference" do
164
+ start_time = clazz.new(2011, 12)
165
+ end_time = clazz.new(2011, 1)
166
+
167
+ expect(TimeDifference.between(start_time, end_time).in_seconds).to eql(28857600.0)
168
+ end
169
+ end
170
+ end
171
+ 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.3.2"
14
+ gem.version = "0.4.2"
15
15
  gem.license = 'MIT'
16
16
 
17
17
  gem.add_runtime_dependency('activesupport')
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_difference
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
5
- prerelease:
4
+ version: 0.4.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - TM Lee
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-05 00:00:00.000000000 Z
11
+ date: 2015-03-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: 2.13.0
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: 2.13.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: TimeDifference is the missing Ruby method to calculate difference between
@@ -68,10 +61,11 @@ executables: []
68
61
  extensions: []
69
62
  extra_rdoc_files: []
70
63
  files:
71
- - .gitignore
72
- - .travis.yml
64
+ - ".gitignore"
65
+ - ".travis.yml"
73
66
  - CHANGELOG.md
74
67
  - Gemfile
68
+ - Gemfile.activesupport32
75
69
  - LICENSE
76
70
  - README.md
77
71
  - Rakefile
@@ -82,31 +76,29 @@ files:
82
76
  homepage: ''
83
77
  licenses:
84
78
  - MIT
79
+ metadata: {}
85
80
  post_install_message:
86
81
  rdoc_options: []
87
82
  require_paths:
88
83
  - lib
89
84
  required_ruby_version: !ruby/object:Gem::Requirement
90
- none: false
91
85
  requirements:
92
- - - ! '>='
86
+ - - ">="
93
87
  - !ruby/object:Gem::Version
94
88
  version: '0'
95
89
  required_rubygems_version: !ruby/object:Gem::Requirement
96
- none: false
97
90
  requirements:
98
- - - ! '>='
91
+ - - ">="
99
92
  - !ruby/object:Gem::Version
100
93
  version: '0'
101
94
  requirements: []
102
95
  rubyforge_project:
103
- rubygems_version: 1.8.23
96
+ rubygems_version: 2.4.5
104
97
  signing_key:
105
- specification_version: 3
98
+ specification_version: 4
106
99
  summary: TimeDifference is the missing Ruby method to calculate difference between
107
100
  two given time. You can do a Ruby time difference in year, month, week, day, hour,
108
101
  minute, and seconds.
109
102
  test_files:
110
103
  - spec/spec_helper.rb
111
104
  - spec/time_difference_spec.rb
112
- has_rdoc: