iso8601 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/iso8601/duration.rb +16 -7
- data/lib/iso8601/errors.rb +13 -3
- data/lib/iso8601/version.rb +1 -1
- data/spec/iso8601/duration_spec.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6495a0f183eb9f89a063e0404b9001fd024b90d
|
4
|
+
data.tar.gz: c9bedd70b4ecc51ad6c1094a4f714b5d2a17fdcc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dbe7ba46843e37298b2a4766752e74d240546ec6232e9dd1aa0e2449589351d18440ed0bf1b982c39353141d5a91b84b8f031b29697d5f09616bb6eadbfd33d
|
7
|
+
data.tar.gz: ced9dcc8204013b265f69e6c8a2407247386202026c83e19296acd99ec44b06a4811b7785e9becc1cae7a49c8a4005c64c4e4a519f7f06decb8bdfd727991b0f
|
data/CHANGELOG.md
CHANGED
data/lib/iso8601/duration.rb
CHANGED
@@ -18,16 +18,16 @@ module ISO8601
|
|
18
18
|
@duration = /^(\+|-)? # Sign
|
19
19
|
P(
|
20
20
|
(
|
21
|
-
(\d+Y)? # Years
|
22
|
-
(\d+M)? # Months
|
23
|
-
(\d+D)? # Days
|
21
|
+
(\d+(?:[,.]\d+)?Y)? # Years
|
22
|
+
(\d+(?:[.,]\d+)?M)? # Months
|
23
|
+
(\d+(?:[.,]\d+)?D)? # Days
|
24
24
|
(T
|
25
|
-
(\d+H)? # Hours
|
26
|
-
(\d+M)? # Minutes
|
27
|
-
(\d+(
|
25
|
+
(\d+(?:[.,]\d+)?H)? # Hours
|
26
|
+
(\d+(?:[.,]\d+)?M)? # Minutes
|
27
|
+
(\d+(?:[.,]\d+)?S)? # Seconds
|
28
28
|
)? # Time
|
29
29
|
)
|
30
|
-
|(\d+W) # Weeks
|
30
|
+
|(\d+(?:[.,]\d+)?W) # Weeks
|
31
31
|
) # Duration
|
32
32
|
$/x.match(pattern) or raise ISO8601::Errors::UnknownPattern.new(pattern)
|
33
33
|
|
@@ -43,6 +43,7 @@ module ISO8601
|
|
43
43
|
:minutes => @duration[9].nil? ? 0 : @duration[9].chop.to_f * sign,
|
44
44
|
:seconds => @duration[10].nil? ? 0 : @duration[10].chop.to_f * sign
|
45
45
|
}
|
46
|
+
valid_fractions?
|
46
47
|
end
|
47
48
|
##
|
48
49
|
# Assigns a new base datetime
|
@@ -198,5 +199,13 @@ module ISO8601
|
|
198
199
|
raise ISO8601::Errors::UnknownPattern.new(@duration)
|
199
200
|
end
|
200
201
|
end
|
202
|
+
|
203
|
+
def valid_fractions?
|
204
|
+
values = @atoms.values.reject(&:zero?)
|
205
|
+
fractions = values.select { |a| (a % 1) != 0 }
|
206
|
+
if fractions.size > 1 || (fractions.size == 1 && fractions.last != values.last)
|
207
|
+
raise ISO8601::Errors::InvalidFractions.new(@duration)
|
208
|
+
end
|
209
|
+
end
|
201
210
|
end
|
202
211
|
end
|
data/lib/iso8601/errors.rb
CHANGED
@@ -4,23 +4,33 @@ module ISO8601
|
|
4
4
|
##
|
5
5
|
# Contains all ISO8601-specific errors.
|
6
6
|
module Errors
|
7
|
+
|
8
|
+
class StandardError < ::StandardError
|
9
|
+
end
|
7
10
|
##
|
8
11
|
# Raised when the given pattern doesn't fit as ISO 8601 parser.
|
9
|
-
class UnknownPattern <
|
12
|
+
class UnknownPattern < StandardError
|
10
13
|
def initialize(pattern)
|
11
14
|
super("The pattern “#{pattern}” is not allowed in this implementation of ISO8601.")
|
12
15
|
end
|
13
16
|
end
|
14
17
|
##
|
18
|
+
# Raised when the given pattern contains an invalid fraction.
|
19
|
+
class InvalidFractions < StandardError
|
20
|
+
def initialize(pattern)
|
21
|
+
super("Fractions are only allowed in the last component")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
##
|
15
25
|
# Raised when the given date is valid but out of range.
|
16
|
-
class RangeError <
|
26
|
+
class RangeError < StandardError
|
17
27
|
def initialize(pattern)
|
18
28
|
super("“#{pattern}” is out of range")
|
19
29
|
end
|
20
30
|
end
|
21
31
|
##
|
22
32
|
# Raise when the base is not suitable.
|
23
|
-
class DurationBaseError <
|
33
|
+
class DurationBaseError < StandardError
|
24
34
|
def initialize(duration)
|
25
35
|
super("Wrong base for #{duration} duration.")
|
26
36
|
end
|
data/lib/iso8601/version.rb
CHANGED
@@ -13,14 +13,30 @@ describe ISO8601::Duration do
|
|
13
13
|
expect { ISO8601::Duration.new('~P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
14
14
|
expect { ISO8601::Duration.new('.P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
15
15
|
end
|
16
|
+
it "should raise a ISO8601::Errors::InvalidFraction for any invalid patterns" do
|
17
|
+
expect { ISO8601::Duration.new('P1.5Y0.5M') }.to raise_error(ISO8601::Errors::InvalidFractions)
|
18
|
+
expect { ISO8601::Duration.new('P1.5Y1M') }.to raise_error(ISO8601::Errors::InvalidFractions)
|
19
|
+
expect { ISO8601::Duration.new('P1.5MT10.5S') }.to raise_error(ISO8601::Errors::InvalidFractions)
|
20
|
+
end
|
16
21
|
it "should parse any allowed pattern" do
|
17
22
|
expect { ISO8601::Duration.new('P1Y') }.to_not raise_error
|
23
|
+
expect { ISO8601::Duration.new('P0.5Y') }.to_not raise_error
|
24
|
+
expect { ISO8601::Duration.new('P0,5Y') }.to_not raise_error
|
18
25
|
expect { ISO8601::Duration.new('P1Y1M') }.to_not raise_error
|
26
|
+
expect { ISO8601::Duration.new('P1Y0.5M') }.to_not raise_error
|
27
|
+
expect { ISO8601::Duration.new('P1Y0,5M') }.to_not raise_error
|
19
28
|
expect { ISO8601::Duration.new('P1Y1M1D') }.to_not raise_error
|
29
|
+
expect { ISO8601::Duration.new('P1Y1M0.5D') }.to_not raise_error
|
30
|
+
expect { ISO8601::Duration.new('P1Y1M0,5D') }.to_not raise_error
|
20
31
|
expect { ISO8601::Duration.new('P1Y1M1DT1H') }.to_not raise_error
|
32
|
+
expect { ISO8601::Duration.new('P1Y1M1DT0.5H') }.to_not raise_error
|
33
|
+
expect { ISO8601::Duration.new('P1Y1M1DT0,5H') }.to_not raise_error
|
21
34
|
expect { ISO8601::Duration.new('P1Y1M1DT1H1M') }.to_not raise_error
|
35
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H0.5M') }.to_not raise_error
|
36
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H0,5M') }.to_not raise_error
|
22
37
|
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S') }.to_not raise_error
|
23
38
|
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1.0S') }.to_not raise_error
|
39
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1,0S') }.to_not raise_error
|
24
40
|
expect { ISO8601::Duration.new('P1W') }.to_not raise_error
|
25
41
|
expect { ISO8601::Duration.new('+P1Y') }.to_not raise_error
|
26
42
|
expect { ISO8601::Duration.new('-P1Y') }.to_not raise_error
|