Skip to content

Commit

Permalink
Merge pull request #13 from joevandyk/iso8601
Browse files Browse the repository at this point in the history
Add ability to parse iso8601 durations.
  • Loading branch information
peleteiro committed May 27, 2013
2 parents 40f923f + 9785afb commit f3f431d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
15 changes: 13 additions & 2 deletions lib/duration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*-
require 'i18n'
require 'active_support/core_ext'
require 'iso8601'

# Duration objects are simple mechanisms that allow you to operate on durations
# of time. They allow you to know how much time has passed since a certain
Expand Down Expand Up @@ -39,13 +40,23 @@ def initialize(args = 0)
unit = unit.to_sym
@seconds += args[unit].to_i * multiple if args.key?(unit)
end
elsif args.kind_of?(String) and args[0] == 'P'
@seconds = ISO8601::Duration.new(args).to_seconds
else
@seconds = args.to_i
end

calculate!
end

def self.load string
self.new(string)
end

def self.dump duration
duration.iso8601
end

# Compare this duration to another (or objects that respond to #to_i)
def <=>(other)
return false unless other.is_a?(Duration)
Expand Down Expand Up @@ -81,8 +92,8 @@ def %(other)
def iso8601
output = 'P'

output << "#{weeks}W" if weeks > 0
output << "#{days}D" if days > 0
number_of_days = weeks * 7 + days
output << "#{number_of_days}D" if number_of_days > 0
if seconds > 0 || minutes > 0 || hours > 0
output << 'T'
output << "#{hours}H" if hours > 0
Expand Down
1 change: 1 addition & 0 deletions ruby-duration.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |s|

s.add_dependency "activesupport", ">= 3.0.0"
s.add_dependency "i18n", ">= 0"
s.add_dependency "iso8601", ">= 0"

s.add_development_dependency "bundler", ">= 1.0.0"
s.add_development_dependency "minitest", ">= 0"
Expand Down
26 changes: 21 additions & 5 deletions test/test_duration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
assert_equal 9, d.total_days
end

it 'should load and dump' do
duration = Duration.new(:seconds => 3)
assert_equal 'PT3S', Duration.dump(duration)
assert_equal Duration.load('PT3S'), duration
end

describe "mathematical operations" do

it "should +" do
Expand Down Expand Up @@ -123,6 +129,16 @@
end
end

describe "creation from iso_8601" do
it "should work" do
assert_equal 60, Duration.new("PT1M").to_i
assert_equal 3630, Duration.new("PT1H30S").to_i

duration = Duration.new(:weeks => 1, :days => 1, :hours => 2, :minutes => 1, :seconds => 2)
assert_equal duration, Duration.new(duration.iso8601)
end
end

describe "#iso_6801" do
it "should format seconds" do
d = Duration.new(:seconds => 1)
Expand All @@ -144,20 +160,20 @@
assert_equal "P1D", d.iso8601
end

it "should format weeks" do
it "should format weeks as ndays" do
d = Duration.new(:weeks => 1)
assert_equal "P1W", d.iso8601
assert_equal "P7D", d.iso8601
end

it "should format only with given values" do
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
assert_equal "P1W2DT3H4M5S", d.iso8601
assert_equal "P9DT3H4M5S", d.iso8601

d = Duration.new(:weeks => 1, :hours => 2, :seconds => 3)
assert_equal "P1WT2H3S", d.iso8601
assert_equal "P7DT2H3S", d.iso8601

d = Duration.new(:weeks => 1, :days => 2)
assert_equal "P1W2D", d.iso8601
assert_equal "P9D", d.iso8601

d = Duration.new(:hours => 1, :seconds => 30)
assert_equal "PT1H30S", d.iso8601
Expand Down

0 comments on commit f3f431d

Please sign in to comment.