Skip to content
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ end_time = Time.new(2014,1)

TimeDifference.between(start_time, end_time).humanize
=> "12 Months and 5 Days"

The :only and :except options can be used to limit the components included

TimeDifference.between(start_time, end_time).humanize(only: [:months])
=> "12 Months"

TimeDifference.between(start_time, end_time).humanize(except: [:months])
=> "5 Days"
```

## Contributing
Expand Down
19 changes: 16 additions & 3 deletions lib/time_difference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,22 @@ def in_general
end]
end

def humanize
def humanize(options={})
diff_parts = []
in_general.each do |part,quantity|
general_hash = in_general
attribute_names = general_hash.keys

if only = options[:only]
attribute_names &= Array(only).map(&:to_sym)
end

if except = options[:except]
attribute_names -= Array(except).map(&:to_sym)
end

result = general_hash.select {|part, quantity| part.in?(attribute_names) && quantity > 0 }

result.each do |part,quantity|
next if quantity <= 0
part = part.to_s.humanize

Expand All @@ -78,7 +91,7 @@ def humanize
end

private

def initialize(start_time, end_time)
start_time = time_in_seconds(start_time)
end_time = time_in_seconds(end_time)
Expand Down
26 changes: 24 additions & 2 deletions spec/time_difference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.with_each_class(&block)
start_time = clazz.new(2009, 11)
end_time = clazz.new(2011, 1)

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})
expect(TimeDifference.between(start_time, end_time).in_general).to eql({years: 1, months: 1, weeks: 4, days: 2, hours: 7, minutes: 41, seconds: 42})
end
end
end
Expand All @@ -51,8 +51,30 @@ def self.with_each_class(&block)
start_time = clazz.new(2009, 11)
end_time = clazz.new(2011, 1)

expect(TimeDifference.between(start_time, end_time).humanize).to eql("1 Year, 2 Months and 18 Hours")
expect(TimeDifference.between(start_time, end_time).humanize).to eql("1 Year, 1 Month, 4 Weeks, 2 Days, 7 Hours, 41 Minutes and 42 Seconds")
end

it "returns a string representing the time difference for selected components using :only option" do
start_time = clazz.new(2009, 11)
end_time = clazz.new(2011, 1)

expect(TimeDifference.between(start_time, end_time).humanize(only: [:years, :months, :minutes])).to eql("1 Year, 1 Month and 41 Minutes")
end

it "returns a string representing the time difference for selected components using :except option" do
start_time = clazz.new(2009, 11)
end_time = clazz.new(2011, 1)

expect(TimeDifference.between(start_time, end_time).humanize(except: [:months, :weeks, :days])).to eql("1 Year, 7 Hours, 41 Minutes and 42 Seconds")
end

it "returns nil when same components are specified in :only and :except option" do
start_time = clazz.new(2009, 11)
end_time = clazz.new(2011, 1)

expect(TimeDifference.between(start_time, end_time).humanize(only: [:months, :weeks], except: [:months, :weeks])).to eql(nil)
end

end
end

Expand Down