time-interval 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/time_interval/duration.rb +21 -9
- data/lib/time_interval/version.rb +1 -1
- data/spec/lib/time_interval/duration_spec.rb +28 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f07fe3b685cf4deb569c85716903b92d1b5011d
|
4
|
+
data.tar.gz: 5057c93856e759678b3e09de12cdc6bf74740206
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d175b9c1e8a6a150d7d523d9656297fcf8895bbad59e848fce5f18ac4ca6b1ba5684f086ea572298b06fd200614c39c64882ba4ef321cd2427110eb869a9364d
|
7
|
+
data.tar.gz: 8277a184873b26605553041ca0c3a8b2d839f6b44b659bcb23aec0b28135e3c6c7f7791ab59f21324be99655cf69ec8a9646f2f506aeca084aac42033c2676a3
|
@@ -1,20 +1,32 @@
|
|
1
1
|
module TimeInterval
|
2
2
|
class Duration
|
3
3
|
def self.parse(duration_str)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
if duration_str['PT']
|
5
|
+
date_str = nil
|
6
|
+
time_str = duration_str
|
7
|
+
else
|
8
|
+
date_str, time_str = duration_str.split('T')
|
9
|
+
end
|
10
|
+
|
11
|
+
if date_str
|
12
|
+
y = date_str['Y'] ? date_str.match(/(\d+)Y/)[1].to_i : 0
|
13
|
+
m = date_str['M'] ? date_str.match(/(\d+)M/)[1].to_i : 0
|
14
|
+
w = date_str['W'] ? date_str.match(/(\d+)W/)[1].to_i : 0
|
15
|
+
d = date_str['D'] ? date_str.match(/(\d+)D/)[1].to_i : 0
|
16
|
+
else
|
17
|
+
y = m = w = d = 0
|
18
|
+
end
|
19
|
+
|
20
|
+
if time_str
|
21
|
+
h = time_str['H'] ? time_str.match(/(\d+)H/)[1].to_i : 0
|
22
|
+
mi = time_str['M'] ? time_str.match(/(\d+)M/)[1].to_i : 0
|
23
|
+
s = time_str['S'] ? time_str.match(/(\d+)S/)[1].to_i : 0
|
12
24
|
else
|
13
25
|
h = mi = s = 0
|
14
26
|
end
|
15
27
|
|
16
28
|
new(years: y, months: m, weeks: w, days: d, hours: h, minutes: mi,
|
17
|
-
|
29
|
+
seconds: s)
|
18
30
|
end
|
19
31
|
|
20
32
|
attr_reader :years, :months, :weeks, :days, :hours, :minutes, :seconds
|
@@ -0,0 +1,28 @@
|
|
1
|
+
RSpec.describe Duration do
|
2
|
+
describe '.parse' do
|
3
|
+
it 'should parse time-only durations correctly' do
|
4
|
+
duration = Duration.parse 'PT2H25M'
|
5
|
+
|
6
|
+
expect(duration.months).to eq 0
|
7
|
+
expect(duration.hours).to eq 2
|
8
|
+
expect(duration.minutes).to eq 25
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should parse date-only durations correctly' do
|
12
|
+
duration = Duration.parse 'P1Y2M'
|
13
|
+
|
14
|
+
expect(duration.years).to eq 1
|
15
|
+
expect(duration.months).to eq 2
|
16
|
+
expect(duration.minutes).to eq 0
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should parse mixed durations correctly' do
|
20
|
+
duration = Duration.parse 'P1Y2MT2H25M'
|
21
|
+
|
22
|
+
expect(duration.years).to eq 1
|
23
|
+
expect(duration.months).to eq 2
|
24
|
+
expect(duration.hours).to eq 2
|
25
|
+
expect(duration.minutes).to eq 25
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time-interval
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastian Edwards
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -101,6 +101,7 @@ files:
|
|
101
101
|
- lib/time_interval/time_pair.rb
|
102
102
|
- lib/time_interval/time_with_duration.rb
|
103
103
|
- lib/time_interval/version.rb
|
104
|
+
- spec/lib/time_interval/duration_spec.rb
|
104
105
|
- spec/lib/time_interval/repeating_interval_spec.rb
|
105
106
|
- spec/lib/time_interval/time_pair_spec.rb
|
106
107
|
- spec/lib/time_interval/time_with_duration_spec.rb
|
@@ -132,6 +133,7 @@ signing_key:
|
|
132
133
|
specification_version: 4
|
133
134
|
summary: Simple library to handle ISO8601 time intervals.
|
134
135
|
test_files:
|
136
|
+
- spec/lib/time_interval/duration_spec.rb
|
135
137
|
- spec/lib/time_interval/repeating_interval_spec.rb
|
136
138
|
- spec/lib/time_interval/time_pair_spec.rb
|
137
139
|
- spec/lib/time_interval/time_with_duration_spec.rb
|