Skip to content

Commit

Permalink
Add limit_to_hours output option
Browse files Browse the repository at this point in the history
  • Loading branch information
henrypoydar committed Sep 8, 2014
1 parent 71b8151 commit 55f992d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -32,6 +32,8 @@ The reverse can also be accomplished with the output method. So pass in seconds
=> 2 wks 1 day 1 hr
>> ChronicDuration.output(1299600, :weeks => true, :units => 2)
=> 2 wks 1 day
>> ChronicDuration.output(45*24*60*60 + 15*60, :limit_to_hours => true)
=> 1080 hrs 15 mins
>> ChronicDuration.output(1299600, :weeks => true, :units => 2, :joiner => ', ')
=> 2 wks, 1 day
>> ChronicDuration.output(1296000)
Expand Down
36 changes: 19 additions & 17 deletions lib/chronic_duration.rb
Expand Up @@ -75,22 +75,24 @@ def output(seconds, opts = {})
if minutes >= 60
hours = (minutes / 60).to_i
minutes = (minutes % 60).to_i
if hours >= ChronicDuration.hours_per_day
days = (hours / ChronicDuration.hours_per_day).to_i
hours = (hours % ChronicDuration.hours_per_day).to_i
if opts[:weeks]
if days >= ChronicDuration.days_per_week
weeks = (days / ChronicDuration.days_per_week).to_i
days = (days % ChronicDuration.days_per_week).to_i
if weeks >= 4
months = (weeks / 4).to_i
weeks = (weeks % 4).to_i
if !opts[:limit_to_hours]
if hours >= ChronicDuration.hours_per_day
days = (hours / ChronicDuration.hours_per_day).to_i
hours = (hours % ChronicDuration.hours_per_day).to_i
if opts[:weeks]
if days >= ChronicDuration.days_per_week
weeks = (days / ChronicDuration.days_per_week).to_i
days = (days % ChronicDuration.days_per_week).to_i
if weeks >= 4
months = (weeks / 4).to_i
weeks = (weeks % 4).to_i
end
end
else
if days >= 30
months = (days / 30).to_i
days = (days % 30).to_i
end
end
else
if days >= 30
months = (days / 30).to_i
days = (days % 30).to_i
end
end
end
Expand Down Expand Up @@ -238,8 +240,8 @@ def filter_through_white_list(string)
raise DurationParseError, "An invalid word #{word.inspect} was used in the string to be parsed."
end
end
# add '1' at front if string starts with something recognizable but not with a number, like 'day' or 'minute 30sec'
res.unshift(1) if res.length > 0 && mappings[res[0]]
# add '1' at front if string starts with something recognizable but not with a number, like 'day' or 'minute 30sec'
res.unshift(1) if res.length > 0 && mappings[res[0]]
res.join(' ')
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chronic_duration/version.rb
@@ -1,3 +1,3 @@
module ChronicDuration
VERSION = '0.10.5'
VERSION = '0.10.6'
end
4 changes: 4 additions & 0 deletions spec/lib/chronic_duration_spec.rb
Expand Up @@ -201,6 +201,10 @@
ChronicDuration.output(45*24*60*60, :weeks => true).should =~ /.*wk.*/
end

it "returns hours and minutes only when :hours_only option specified" do
ChronicDuration.output(395*24*60*60 + 15*60, :limit_to_hours => true).should == '9480 hrs 15 mins'
end

it "returns the specified number of units if provided" do
ChronicDuration.output(4 * 3600 + 60 + 1, units: 2).should == '4 hrs 1 min'
ChronicDuration.output(6 * 30 * 24 * 3600 + 24 * 3600 + 3600 + 60 + 1, units: 3, format: :long).should == '6 months 1 day 1 hour'
Expand Down

0 comments on commit 55f992d

Please sign in to comment.