Skip to content
Browse files

added total hours and total days along with unit formats

  • Loading branch information...
1 parent 38cf673 commit 8983f5c067bdcd2cd055715f234cacd9f991b534 @gdavison gdavison committed Apr 5, 2012
Showing with 70 additions and 26 deletions.
  1. +30 −20 lib/duration.rb
  2. +40 −6 test/test_duration.rb
View
50 lib/duration.rb
@@ -72,8 +72,8 @@ def %(other)
Duration.new(@total % other.to_i)
end
- def total_minutes
- @total / 60
+ %w(minutes hours days).each do |meth|
+ define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }
end
# Formats a duration in ISO8601.
@@ -105,21 +105,28 @@ def present?
# Format a duration into a human-readable string.
#
- # %w => weeks
- # %d => days
- # %h => hours
- # %m => minutes
- # %s => seconds
- # %tm => total minutes
- # %t => total seconds
- # %H => zero-padded hours
- # %M => zero-padded minutes
- # %S => zero-padded seconds
- # %~s => locale-dependent "seconds" terminology
- # %~m => locale-dependent "minutes" terminology
- # %~h => locale-dependent "hours" terminology
- # %~d => locale-dependent "days" terminology
- # %~w => locale-dependent "weeks" terminology
+ # %w => weeks
+ # %d => days
+ # %h => hours
+ # %m => minutes
+ # %s => seconds
+ # %td => total days
+ # %th => total hours
+ # %tm => total minutes
+ # %ts => total seconds
+ # %t => total seconds
+ # %H => zero-padded hours
+ # %M => zero-padded minutes
+ # %S => zero-padded seconds
+ # %~s => locale-dependent "seconds" terminology
+ # %~m => locale-dependent "minutes" terminology
+ # %~h => locale-dependent "hours" terminology
+ # %~d => locale-dependent "days" terminology
+ # %~w => locale-dependent "weeks" terminology
+ # %tdu => total days with locale-dependent unit
+ # %thu => total hours with locale-dependent unit
+ # %tmu => total minutes with locale-dependent unit
+ # %tsu => total seconds with locale-dependent unit
#
# You can also use the I18n support.
# The %~s, %~m, %~h, %~d and %~w can be translated with I18n.
@@ -149,7 +156,10 @@ def format(format_str)
'h' => @hours,
'm' => @minutes,
's' => @seconds,
+ 'td' => Proc.new { total_days },
+ 'th' => Proc.new { total_hours },
'tm' => Proc.new { total_minutes },
+ 'ts' => @total,
't' => @total,
'H' => @hours.to_s.rjust(2, '0'),
'M' => @minutes.to_s.rjust(2, '0'),
@@ -159,13 +169,13 @@ def format(format_str)
'~h' => i18n_for(:hour),
'~d' => i18n_for(:day),
'~w' => i18n_for(:week),
-# '~ts'=> Proc.new { i18n_for(:total) },
-# '~tm'=> Proc.new { i18n_for(:total_minute) }
+ 'tdu'=> Proc.new { "#{total_days} #{i18n_for(:total_day)}"},
+ 'thu'=> Proc.new { "#{total_hours} #{i18n_for(:total_hour)}"},
'tmu'=> Proc.new { "#{total_minutes} #{i18n_for(:total_minute)}"},
'tsu'=> Proc.new { "#{total} #{i18n_for(:total)}"}
}
- format_str.gsub(/%?%(w|d|h|m|s|(tmu)|(tsu)|(tm)|t|H|M|S|~(?:s|m|h|d|w))/) do |match|
+ format_str.gsub(/%?%(w|d|h|m|s|t([dhms]u?)?|H|M|S|~(?:s|m|h|d|w))/) do |match|
match['%%'] ? match : (identifiers[match[1..-1]].class == Proc ? identifiers[match[1..-1]].call : identifiers[match[1..-1]])
end.gsub('%%', '%')
end
View
46 test/test_duration.rb
@@ -11,17 +11,21 @@
assert_equal 30, d.seconds
assert_equal 90, d.total
assert_equal 1, d.total_minutes
+ assert_equal 0, d.total_hours
+ assert_equal 0, d.total_days
end
it "should initialize given duration in Hash" do
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
- assert_equal 1, d.weeks
- assert_equal 2, d.days
- assert_equal 3, d.hours
- assert_equal 4, d.minutes
- assert_equal 5, d.seconds
+ assert_equal 1, d.weeks
+ assert_equal 2, d.days
+ assert_equal 3, d.hours
+ assert_equal 4, d.minutes
+ assert_equal 5, d.seconds
assert_equal 788645, d.total
- assert_equal 13144, d.total_minutes
+ assert_equal 13144, d.total_minutes
+ assert_equal 219, d.total_hours
+ assert_equal 9, d.total_days
end
describe "mathematical operations" do
@@ -87,6 +91,36 @@
d = Duration.new(:hours => 1, :minutes => 1)
assert_equal "61 minutes", d.format("%tmu")
end
+
+ it "should display total hours as number" do
+ d = Duration.new(:days => 2, :hours => 1)
+ assert_equal "49", d.format("%th")
+ end
+
+ it 'should display total hours with unit' do
+ d = Duration.new(:days => 2, :hours => 2)
+ assert_equal "50 hours", d.format("%thu")
+ end
+
+ it "should display total hours in plural form when needed" do
+ d = Duration.new(:days => 1, :hours => 1)
+ assert_equal "25 hours", d.format("%thu")
+ end
+
+ it "should display total days as number" do
+ d = Duration.new(:weeks => 1, :days => 3)
+ assert_equal "10", d.format("%td")
+ end
+
+ it 'should display total days with unit' do
+ d = Duration.new(:weeks => 1, :days => 2)
+ assert_equal "9 days", d.format("%tdu")
+ end
+
+ it "should display total days in plural form when needed" do
+ d = Duration.new(:weeks => 1, :days => 1)
+ assert_equal "8 days", d.format("%tdu")
+ end
end
describe "#iso_6801" do

0 comments on commit 8983f5c

Please sign in to comment.
Something went wrong with that request. Please try again.