Skip to content

Commit 20e8a91

Browse files
committed
Fix bug when we're accumulating on yearS
1 parent 0c927f8 commit 20e8a91

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

lib/dotiw/time_hash.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ def to_hash
4343
def build_time_hash
4444
if accumulate_on = options[:accumulate_on]
4545
accumulate_on = accumulate_on.to_sym
46-
return build_time_hash if accumulate_on == :years
47-
4846
TIME_FRACTIONS.index(accumulate_on).downto(0) { |i| send("build_#{TIME_FRACTIONS[i]}") }
4947
else
5048
while distance > 0
@@ -91,11 +89,15 @@ def build_weeks
9189
def build_months
9290
build_years_months_weeks_days
9391

94-
if (years = output.delete(:years)) > 0
92+
if options[:accumulate_on]&.to_sym == :months && (years = output.delete(:years)) > 0
9593
output[:months] += (years * 12)
9694
end
9795
end
9896

97+
def build_years
98+
build_years_months_weeks_days
99+
end
100+
99101
def build_years_months_weeks_days
100102
months = (largest.year - smallest.year) * 12 + (largest.month - smallest.month)
101103
years, months = months.divmod(12)

spec/lib/dotiw_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@
219219
START_TIME + 2.day + 10_000.hour + 10.second,
220220
:months,
221221
'13 months, 3 weeks, 1 day, 16 hours, and 10 seconds'],
222-
['2015-1-15'.to_time, '2016-3-15'.to_time, :months, '14 months']
222+
['2015-1-15'.to_time, '2016-3-15'.to_time, :months, '14 months'],
223+
['2015-1-15'.to_time, '2016-3-15'.to_time, :years, '1 year and 2 months']
223224
].each do |start, finish, accumulator, output|
224225
it "should be #{output}" do
225226
expect(distance_of_time_in_words(start, finish, true, accumulate_on: accumulator)).to eq(output)

0 commit comments

Comments
 (0)