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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c19e6c56117a74623c0b0be054d6b4cd555b090
4
- data.tar.gz: 2aa769a3bb0baacbc67e0dd5195e10851d78110b
3
+ metadata.gz: 1f07fe3b685cf4deb569c85716903b92d1b5011d
4
+ data.tar.gz: 5057c93856e759678b3e09de12cdc6bf74740206
5
5
  SHA512:
6
- metadata.gz: f00bda6968150c34b15b8c14574bd2645fc641cd2aba9404b8f34706890cb3f14927912092cac3ecc96295c9d11b37840eb25ea31a58a1e94f060157223e38ea
7
- data.tar.gz: 640c3eeff7454ef811de018237aebd233fc503c7b22ca6855a4ce7ab4452608ef7ba18f983cf3b8ed83992c77802ce00960f536bc15507e5b852c79727fd8580
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
- y = duration_str['Y'] ? duration_str.match(/(\d+)Y/)[1].to_i : 0
5
- m = duration_str['M'] ? duration_str.match(/(\d+)M/)[1].to_i : 0
6
- w = duration_str['W'] ? duration_str.match(/(\d+)W/)[1].to_i : 0
7
- d = duration_str['D'] ? duration_str.match(/(\d+)D/)[1].to_i : 0
8
- if duration_str['T']
9
- h = duration_str['H'] ? duration_str.match(/T.*(\d+)H/)[1].to_i : 0
10
- mi = duration_str['M'] ? duration_str.match(/T.*(\d+)M/)[1].to_i : 0
11
- s = duration_str['S'] ? duration_str.match(/T.*(\d+)S/)[1].to_i : 0
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
- seconds: s)
29
+ seconds: s)
18
30
  end
19
31
 
20
32
  attr_reader :years, :months, :weeks, :days, :hours, :minutes, :seconds
@@ -1,3 +1,3 @@
1
1
  module TimeInterval
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -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.2
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-06 00:00:00.000000000 Z
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