Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an in_calendar_months feature #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ TimeDifference.between(start_time, end_time).in_years
TimeDifference.between(start_time, end_time).in_months
=> 12.0

TimeDifference.between(start_time, end_time).in_calendar_months
=> 12.0

TimeDifference.between(start_time, end_time).in_weeks
=> 52.14

Expand Down
26 changes: 23 additions & 3 deletions lib/time_difference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def in_years
def in_months
(@time_diff / (1.day * 30.42)).round(2)
end

def in_calendar_months
months_between(@start_time, @end_time)
end

def in_weeks
in_component(:weeks)
Expand Down Expand Up @@ -82,10 +86,13 @@ def humanize
private

def initialize(start_time, end_time)
start_time = time_in_seconds(start_time)
end_time = time_in_seconds(end_time)
@start_time = start_time.to_time
@end_time = end_time.to_time

start_time_s = time_in_seconds(start_time)
end_time_s = time_in_seconds(end_time)

@time_diff = (end_time - start_time).abs
@time_diff = (end_time_s - start_time_s).abs
end

def time_in_seconds(time)
Expand All @@ -95,5 +102,18 @@ def time_in_seconds(time)
def in_component(component)
(@time_diff / 1.send(component)).round(2)
end

def months_between(t1, t2)
months_excl_days = (t2.year*12 + t2.month) - (t1.year*12 + t1.month)
if t2.day > t1.day
months_excl_days
elsif t2.day < t1.day
months_excl_days - 1
elsif t2.seconds_since_midnight >= t1.seconds_since_midnight
months_excl_days
else
months_excl_days - 1
end
end

end
54 changes: 54 additions & 0 deletions spec/time_difference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,58 @@ def self.with_each_class(&block)
end
end
end

describe '#in_calendar_months' do
with_each_class do |clazz|
it 'considers Sep-4 to Dec-4 to be 3 months' do
start_time = clazz.new(2017, 9, 4)
end_time = clazz.new(2017, 12, 4)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(3)
end
it 'considers Aug-4 to Nov-4 to be 3 months' do
start_time = clazz.new(2017, 8, 4)
end_time = clazz.new(2017, 11, 4)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(3)
end
it 'considers Sep-4 to Dec-3 to be 2 months' do
start_time = clazz.new(2017, 9, 4)
end_time = clazz.new(2017, 12, 3)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(2)
end
it 'considers Sep-4 to Dec-31 to be 3 months' do
start_time = clazz.new(2017, 9, 4)
end_time = clazz.new(2017, 12, 31)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(3)
end
it 'considers 2016-Sep-4 to 2017-Sep-4 to be 12 months' do
start_time = clazz.new(2016, 9, 4)
end_time = clazz.new(2017, 9, 4)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(12)
end
it 'considers 2016-Sep-4 to 2017-Dec-31 to be 15 months' do
start_time = clazz.new(2016, 9, 4)
end_time = clazz.new(2017, 12, 31)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(15)
end
end
it 'counts months on the day when time2 > time1' do
start_time = Time.new(2017, 9, 4, 12, 15)
end_time = Time.new(2017, 12, 4, 12, 22)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(3)
end
it 'does not count months on the day when time2 < time1' do
start_time = Time.new(2017, 9, 4, 12, 15)
end_time = Time.new(2017, 12, 4, 12, 10)

expect(TimeDifference.between(start_time, end_time).in_calendar_months).to eq(2)
end
end

end