iso8601 0.12.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe ISO8601::Hours do
4
- describe 'Atomic' do
5
- let(:subject) { ISO8601::Hours.new(1) }
6
-
7
- it "should respond to the Atomic interface" do
8
- %i[factor
9
- to_seconds
10
- symbol
11
- to_i
12
- to_f
13
- to_s
14
- value
15
- <=>
16
- eql?
17
- hash
18
- valid_atom?].each { |m| expect(subject).to respond_to(m) }
19
- end
20
- end
21
-
22
- describe '#factor' do
23
- it "should return the Hour factor" do
24
- expect(ISO8601::Hours.new(2).factor).to eq(3600)
25
- end
26
-
27
- it "should return the amount of seconds" do
28
- expect(ISO8601::Hours.new(2).to_seconds).to eq(7200)
29
- expect(ISO8601::Hours.new(-2).to_seconds).to eq(-7200)
30
- end
31
- end
32
-
33
- describe '#symbol' do
34
- it "should return the ISO symbol" do
35
- expect(ISO8601::Hours.new(1).symbol).to eq(:H)
36
- end
37
- end
38
-
39
- describe '#hash' do
40
- it "should build hash identity by value" do
41
- expect(ISO8601::Hours.new(3).hash).to eq(ISO8601::Hours.new(3).hash)
42
- end
43
- end
44
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe ISO8601::Minutes do
4
- describe 'Atomic' do
5
- let(:subject) { ISO8601::Minutes.new(1) }
6
-
7
- it "should respond to the Atomic interface" do
8
- %i[factor
9
- to_seconds
10
- symbol
11
- to_i
12
- to_f
13
- to_s
14
- value
15
- <=>
16
- eql?
17
- hash
18
- valid_atom?].each { |m| expect(subject).to respond_to(m) }
19
- end
20
- end
21
-
22
- describe '#factor' do
23
- it "should return the Minute factor" do
24
- expect(ISO8601::Minutes.new(2).factor).to eq(60)
25
- end
26
-
27
- it "should return the amount of seconds" do
28
- expect(ISO8601::Minutes.new(2).to_seconds).to eq(120)
29
- expect(ISO8601::Minutes.new(-2).to_seconds).to eq(-120)
30
- end
31
- end
32
-
33
- describe '#symbol' do
34
- it "should return the ISO symbol" do
35
- expect(ISO8601::Minutes.new(1).symbol).to eq(:M)
36
- end
37
- end
38
-
39
- describe '#hash' do
40
- it "should build hash identity by value" do
41
- expect(ISO8601::Minutes.new(3).hash).to eq(ISO8601::Minutes.new(3).hash)
42
- end
43
- end
44
- end
@@ -1,98 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe ISO8601::Months do
4
- let(:common_year) { ISO8601::DateTime.new('2010-01-01') }
5
- let(:leap_year) { ISO8601::DateTime.new('2000-01-01') }
6
-
7
- let(:common_february) { ISO8601::DateTime.new('2010-02-01') }
8
- let(:leap_february) { ISO8601::DateTime.new('2000-02-01') }
9
-
10
- let(:common_november) { ISO8601::DateTime.new('2017-11-01') }
11
-
12
- let(:common_december) { ISO8601::DateTime.new('2017-12-01') }
13
- let(:leap_december) { ISO8601::DateTime.new('2000-12-01') }
14
-
15
- describe 'Atomic' do
16
- let(:subject) { ISO8601::Months.new(1) }
17
-
18
- it "should respond to the Atomic interface" do
19
- %i[factor
20
- to_seconds
21
- symbol
22
- to_i
23
- to_f
24
- to_s
25
- value
26
- <=>
27
- eql?
28
- hash
29
- valid_atom?].each { |m| expect(subject).to respond_to(m) }
30
- end
31
- end
32
-
33
- describe '#factor' do
34
- it "should return the Month factor" do
35
- expect { ISO8601::Months.new(1).factor }.to_not raise_error
36
- expect(ISO8601::Months.new(2).factor).to eq(2628000)
37
- expect(ISO8601::Months.new(0).factor).to eq(2628000)
38
- end
39
-
40
- it "should return the Month factor for a common year" do
41
- expect(ISO8601::Months.new(1).factor(common_year)).to eq(2678400)
42
- end
43
-
44
- it "should return the Month factor for a leap year" do
45
- expect(ISO8601::Months.new(1).factor(leap_year)).to eq(2678400)
46
- end
47
-
48
- it "should return the Month factor based on february for a common year" do
49
- expect(ISO8601::Months.new(1).factor(common_february)).to eq(2419200)
50
- end
51
-
52
- it "should return the Month factor based on february for a leap year" do
53
- expect(ISO8601::Months.new(1).factor(leap_february)).to eq(2505600)
54
- end
55
- end
56
-
57
- describe '#to_seconds' do
58
- it "should return the amount of seconds" do
59
- expect(ISO8601::Months.new(2).to_seconds).to eq(5256000)
60
- end
61
-
62
- it "should return the amount of seconds for a common year" do
63
- expect(ISO8601::Months.new(2).to_seconds(common_year)).to eq(5097600)
64
- expect(ISO8601::Months.new(1).to_seconds(common_year)).to eq(2678400)
65
- expect(ISO8601::Months.new(0).to_seconds(common_year)).to eq(0)
66
- expect(ISO8601::Months.new(0).to_seconds(common_december)).to eq(0)
67
- expect(ISO8601::Months.new(2).to_seconds(common_november)).to eq(5270400)
68
- expect(ISO8601::Months.new(1).to_seconds(common_november)).to eq(2592000)
69
- expect(ISO8601::Months.new(0).to_seconds(common_november)).to eq(0)
70
- end
71
-
72
- it "should return the amount of seconds for a leap year" do
73
- expect(ISO8601::Months.new(2).to_seconds(leap_year)).to eq(5184000)
74
- end
75
-
76
- it "should return the amount of seconds based on februrary for a common year" do
77
- expect(ISO8601::Months.new(2).to_seconds(common_february)).to eq(5097600)
78
- end
79
-
80
- it "should return the amount of seconds based on february for a leap year" do
81
- expect(ISO8601::Months.new(2).to_seconds(leap_february)).to eq(5184000)
82
- expect(ISO8601::Months.new(12).to_seconds(leap_february)).to eq(31622400)
83
- expect(ISO8601::Months.new(12).to_seconds(leap_february)).to eq(ISO8601::Years.new(1).to_seconds(leap_year))
84
- end
85
- end
86
-
87
- describe '#symbol' do
88
- it "should return the ISO symbol" do
89
- expect(ISO8601::Months.new(1).symbol).to eq(:M)
90
- end
91
- end
92
-
93
- describe '#hash' do
94
- it "should build hash identity by value" do
95
- expect(ISO8601::Months.new(3).hash).to eq(ISO8601::Months.new(3).hash)
96
- end
97
- end
98
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe ISO8601::Seconds do
4
- describe 'Atomic' do
5
- let(:subject) { ISO8601::Seconds.new(1) }
6
-
7
- it "should respond to the Atomic interface" do
8
- %i[factor
9
- to_seconds
10
- symbol
11
- to_i
12
- to_f
13
- to_s
14
- value
15
- <=>
16
- eql?
17
- hash
18
- valid_atom?].each { |m| expect(subject).to respond_to(m) }
19
- end
20
- end
21
-
22
- describe '#factor' do
23
- it "should return the Second factor" do
24
- expect(ISO8601::Seconds.new(2).factor).to eq(1)
25
- end
26
-
27
- it "should return the amount of seconds" do
28
- expect(ISO8601::Seconds.new(2).to_seconds).to eq(2)
29
- expect(ISO8601::Seconds.new(-2).to_seconds).to eq(-2)
30
- end
31
- end
32
-
33
- describe '#symbol' do
34
- it "should return the ISO symbol" do
35
- expect(ISO8601::Seconds.new(1).symbol).to eq(:S)
36
- end
37
- end
38
-
39
- describe '#hash' do
40
- it "should build hash identity by value" do
41
- expect(ISO8601::Seconds.new(3).hash).to eq(ISO8601::Seconds.new(3).hash)
42
- end
43
- end
44
- end
@@ -1,520 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe ISO8601::TimeInterval do
4
- describe 'pattern initialization' do
5
- it "should raise a ISO8601::Errors::UnknownPattern if it not a valid interval pattern" do
6
- # Invalid separators
7
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00ZP1Y2M10DT2H30M') }
8
- .to raise_error(ISO8601::Errors::UnknownPattern)
9
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z-P1Y2M10D') }
10
- .to raise_error(ISO8601::Errors::UnknownPattern)
11
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z~P1Y2M10D') }
12
- .to raise_error(ISO8601::Errors::UnknownPattern)
13
- expect { ISO8601::TimeInterval.parse('P1Y2M10DT2H30M2007-03-01T13:00:00Z') }
14
- .to raise_error(ISO8601::Errors::UnknownPattern)
15
- expect { ISO8601::TimeInterval.parse('P1Y2M10D-2007-03-01T13:00:00Z') }
16
- .to raise_error(ISO8601::Errors::UnknownPattern)
17
- expect { ISO8601::TimeInterval.parse('P1Y2M10D~2007-03-01T13:00:00Z') }
18
- .to raise_error(ISO8601::Errors::UnknownPattern)
19
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z2008-05-11T15:30:00Z') }
20
- .to raise_error(ISO8601::Errors::UnknownPattern)
21
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z-2008-05-11T15:30:00Z') }
22
- .to raise_error(ISO8601::Errors::UnknownPattern)
23
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z~2008-05-11T15:30:00Z') }
24
- .to raise_error(ISO8601::Errors::UnknownPattern)
25
- end
26
-
27
- describe 'with duration' do
28
- it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
29
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/') }
30
- .to raise_error(ISO8601::Errors::UnknownPattern)
31
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P') }
32
- .to raise_error(ISO8601::Errors::UnknownPattern)
33
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT') }
34
- .to raise_error(ISO8601::Errors::UnknownPattern)
35
- end
36
- end
37
-
38
- describe 'with DateTimes' do
39
- it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
40
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/2010-0-09') }
41
- .to raise_error(ISO8601::Errors::UnknownPattern)
42
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/2010-05-09T103012+0400') }
43
- .to raise_error(ISO8601::Errors::UnknownPattern)
44
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/2014-W15-02T10:11:12Z') }
45
- .to raise_error(ISO8601::Errors::UnknownPattern)
46
- end
47
- end
48
-
49
- it "should raise a ISO8601::Errors::UnknownPattern if start time and end time are durations" do
50
- expect { ISO8601::TimeInterval.parse('P1Y2M10D/P1Y2M10D') }.to raise_error(ISO8601::Errors::UnknownPattern)
51
- expect { ISO8601::TimeInterval.parse('P1Y0.5M/P1Y0.5M') }.to raise_error(ISO8601::Errors::UnknownPattern)
52
- end
53
-
54
- context "allowed patterns" do
55
- it "should parse <start>/<duration>" do
56
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y') }.to_not raise_error
57
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y1M1D') }.to_not raise_error
58
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y1M1DT1H1M1.0S') }.to_not raise_error
59
- expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y1M1DT1H1M1,0S') }.to_not raise_error
60
- end
61
-
62
- it "should parse <duration>/<end>" do
63
- expect { ISO8601::TimeInterval.parse('P1Y1M1D/2010-05-09T10:30:12+04:00') }.to_not raise_error
64
- expect { ISO8601::TimeInterval.parse('P1Y1M1DT1H/-2014-05-31T16:26:00Z') }.to_not raise_error
65
- expect { ISO8601::TimeInterval.parse('P1Y1M1DT0.5H/2014-05-31T16:26:10.5Z') }.to_not raise_error
66
- expect { ISO8601::TimeInterval.parse('P1Y1M1DT0,5H/2014-05-31T16:26:10,5Z') }.to_not raise_error
67
- end
68
-
69
- it "should parse <start>/<end>" do
70
- expect { ISO8601::TimeInterval.parse('2014-001/2010-05-09T10:30') }.to_not raise_error
71
- expect { ISO8601::TimeInterval.parse('2014121/2010-05-09T10:30:12') }.to_not raise_error
72
- expect { ISO8601::TimeInterval.parse('2014-121T10:11:12Z/2010-05-09T10:30:12Z') }.to_not raise_error
73
- expect { ISO8601::TimeInterval.parse('20100509T103012+0400/2010-05-09T10:30:12+04') }.to_not raise_error
74
- expect { ISO8601::TimeInterval.parse('20100509/2010-05-09T10:30:12+04:00') }.to_not raise_error
75
- expect { ISO8601::TimeInterval.parse('T103012+0400/2010-05-09T10:30:12-04:00') }.to_not raise_error
76
- expect { ISO8601::TimeInterval.parse('T103012+04/2010-05-09T10:30:12-00:00') }.to_not raise_error
77
- end
78
- end
79
- end
80
-
81
- describe 'initialization with a ISO8601::Duration' do
82
- # it "should raise a ISO8601::Errors::TypeError if parameter is not a ISO8601::Duration" do
83
- # datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
84
-
85
- # expect { ISO8601::TimeInterval.from_duration('hi', {}) }.to raise_error(ISO8601::Errors::TypeError)
86
- # expect { ISO8601::TimeInterval.from_duration([], {}) }.to raise_error(ISO8601::Errors::TypeError)
87
- # expect { ISO8601::TimeInterval.from_duration(datetime, {}) }.to raise_error(ISO8601::Errors::TypeError)
88
- # expect { ISO8601::TimeInterval.from_duration({}, {}) }.to raise_error(ISO8601::Errors::TypeError)
89
- # end
90
-
91
- # it "should raise an ISO8601::Errors::TypeError if the time hash is no valid" do
92
- # duration = ISO8601::Duration.new('P1Y1M1DT0.5H')
93
- # datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
94
-
95
- # expect { ISO8601::TimeInterval.from_duration(duration, { time: datetime }) }.to raise_error(ISO8601::Errors::TypeError)
96
- # expect { ISO8601::TimeInterval.from_duration(duration, { start_time: nil }) }.to raise_error(ISO8601::Errors::TypeError)
97
- # expect { ISO8601::TimeInterval.from_duration(duration, { start_time: datetime, end_time: datetime }) }.to raise_error(ISO8601::Errors::TypeError)
98
- # expect { ISO8601::TimeInterval.from_duration(duration, {}) }.to raise_error(ISO8601::Errors::TypeError)
99
- # end
100
-
101
- it "should initialize with a valid duration and time" do
102
- time = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
103
- duration = ISO8601::Duration.new('P1M')
104
-
105
- expect { ISO8601::TimeInterval.from_duration(time, duration) }.to_not raise_error
106
- expect { ISO8601::TimeInterval.from_duration(duration, time) }.to_not raise_error
107
- end
108
- end
109
-
110
- describe 'initialization with a ISO8601::DateTime' do
111
- it "should raise a ISO8601::Errors::TypeError if parameters are not an ISO8601::DateTime instance" do
112
- duration = ISO8601::Duration.new('P1Y1M1DT0.5H')
113
- datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
114
-
115
- expect { ISO8601::TimeInterval.from_datetimes(duration, datetime) }.to raise_error(ISO8601::Errors::TypeError)
116
- expect { ISO8601::TimeInterval.from_datetimes(datetime, duration) }.to raise_error(ISO8601::Errors::TypeError)
117
- expect { ISO8601::TimeInterval.from_datetimes(datetime, 'Hello!') }.to raise_error(ISO8601::Errors::TypeError)
118
- expect { ISO8601::TimeInterval.from_datetimes({}, datetime) }.to raise_error(ISO8601::Errors::TypeError)
119
- end
120
-
121
- it "should initialize class with a valid datetimes" do
122
- datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
123
- datetime2 = ISO8601::DateTime.new('2010-05-15T10:30:12Z')
124
-
125
- expect { ISO8601::TimeInterval.from_datetimes(datetime, datetime2) }.to_not raise_error
126
- expect { ISO8601::TimeInterval.from_datetimes(datetime2, datetime) }.to_not raise_error
127
- end
128
- end
129
-
130
- describe "#to_f" do
131
- it "should calculate the size of time interval <start>/<end>" do
132
- hour = (60 * 60).to_f
133
- pattern1 = '2010-05-09T11:30:00Z/2010-05-09T12:30:00Z'
134
- pattern2 = '2010-05-09T11:30:00+01:00/2010-05-09T12:30:00+01:00'
135
-
136
- expect(ISO8601::TimeInterval.parse(pattern1).to_f).to eq(hour)
137
- expect(ISO8601::TimeInterval.parse(pattern2).to_f).to eq(hour)
138
- end
139
-
140
- it "should calculate the size of time interval <start>/<duration>" do
141
- hour = (60 * 60).to_f
142
- pattern1 = '2010-05-09T11:30:00Z/PT1H'
143
- pattern2 = '2010-05-09T11:30:00+01:00/PT1H'
144
-
145
- expect(ISO8601::TimeInterval.parse(pattern1).to_f).to eq(hour)
146
- expect(ISO8601::TimeInterval.parse(pattern2).to_f).to eq(hour)
147
- end
148
-
149
- it "should calculate the size of time interval <duration>/<end>" do
150
- hour = (60 * 60).to_f
151
- pattern1 = 'PT1H/2010-05-09T11:30:00Z'
152
- pattern2 = 'PT1H/2010-05-09T11:30:00-09:00'
153
-
154
- expect(ISO8601::TimeInterval.parse(pattern1).to_f).to eq(hour)
155
- expect(ISO8601::TimeInterval.parse(pattern2).to_f).to eq(hour)
156
- end
157
- it "should be 0" do
158
- expect(ISO8601::TimeInterval.parse('2015-01-01/2015-01-01').to_f).to eq(0)
159
- end
160
- end
161
-
162
- describe "#empty?" do
163
- it "should check if the interval is empty" do
164
- expect(ISO8601::TimeInterval.parse('2015-01-01/2015-01-01').empty?).to be_truthy
165
- expect(ISO8601::TimeInterval.parse('2015-01-01/2015-01-02').empty?).to be_falsy
166
- end
167
- end
168
-
169
- describe "#start_time" do
170
- it "should return always a ISO8601::DateTime object" do
171
- pattern = 'PT1H/2010-05-09T10:30:00Z'
172
- pattern2 = '2010-05-09T11:30:00Z/PT1H'
173
- pattern3 = '2010-05-09T11:30:00Z/2010-05-09T12:30:00Z'
174
-
175
- expect(ISO8601::TimeInterval.parse(pattern).first).to be_an_instance_of(ISO8601::DateTime)
176
- expect(ISO8601::TimeInterval.parse(pattern2).first).to be_an_instance_of(ISO8601::DateTime)
177
- expect(ISO8601::TimeInterval.parse(pattern3).first).to be_an_instance_of(ISO8601::DateTime)
178
- end
179
-
180
- it "should calculate correctly the start_time" do
181
- start_time = ISO8601::DateTime.new('2010-05-09T10:30:00Z')
182
- pattern = 'PT1H/2010-05-09T11:30:00Z'
183
- pattern2 = '2010-05-09T10:30:00Z/PT1H'
184
- pattern3 = '2010-05-09T10:30:00Z/2010-05-09T12:30:00Z'
185
-
186
- expect(ISO8601::TimeInterval.parse(pattern).first).to eq(start_time)
187
- expect(ISO8601::TimeInterval.parse(pattern2).first).to eq(start_time)
188
- expect(ISO8601::TimeInterval.parse(pattern3).first).to eq(start_time)
189
- end
190
-
191
- describe "November" do
192
- pairs = [
193
- { pattern: 'P1Y/2017-11-09T07:00:00Z',
194
- start_time: ISO8601::DateTime.new('2016-11-09T07:00:00Z') },
195
- { pattern: 'P1M/2017-11-09T07:00:00Z',
196
- start_time: ISO8601::DateTime.new('2017-10-09T07:00:00Z') },
197
- { pattern: 'P1D/2017-11-09T07:00:00Z',
198
- start_time: ISO8601::DateTime.new('2017-11-08T07:00:00Z') },
199
- { pattern: 'PT1H/2017-11-09T07:00:00Z',
200
- start_time: ISO8601::DateTime.new('2017-11-09T06:00:00Z') }
201
- ]
202
-
203
- pairs.each do |pair|
204
- it "should calculate correctly the start_time for #{pair[:pattern]}" do
205
- expect(ISO8601::TimeInterval.parse(pair[:pattern]).first.to_s).to eq(pair[:start_time].to_s)
206
- end
207
- end
208
- end
209
-
210
- describe "December" do
211
- pairs = [
212
- { pattern: 'P1Y/2017-12-09T07:00:00Z',
213
- start_time: ISO8601::DateTime.new('2016-12-09T07:00:00Z') },
214
- { pattern: 'P1M/2017-12-09T07:00:00Z',
215
- start_time: ISO8601::DateTime.new('2017-11-09T07:00:00Z') },
216
- { pattern: 'P3D/2017-12-06T18:30:00Z',
217
- start_time: ISO8601::DateTime.new('2017-12-03T18:30:00Z') },
218
- { pattern: 'P1D/2017-12-09T07:00:00Z',
219
- start_time: ISO8601::DateTime.new('2017-12-08T07:00:00Z') },
220
- { pattern: 'PT1H/2017-12-09T07:00:00Z',
221
- start_time: ISO8601::DateTime.new('2017-12-09T06:00:00Z') }
222
- ]
223
-
224
- pairs.each do |pair|
225
- it "should calculate correctly the start_time for #{pair[:pattern]}" do
226
- expect(ISO8601::TimeInterval.parse(pair[:pattern]).first.to_s).to eq(pair[:start_time].to_s)
227
- end
228
- end
229
- end
230
-
231
- describe "January" do
232
- pairs = [
233
- { pattern: 'P1Y/2017-01-01T00:00:00Z',
234
- start_time: ISO8601::DateTime.new('2016-01-01T00:00:00Z') },
235
- { pattern: 'P1M/2017-01-01T00:00:00Z',
236
- start_time: ISO8601::DateTime.new('2016-12-01T00:00:00Z') },
237
- { pattern: 'P1D/2017-01-01T00:00:00Z',
238
- start_time: ISO8601::DateTime.new('2016-12-31T00:00:00Z') },
239
- { pattern: 'PT1H/2017-01-01T01:00:00Z',
240
- start_time: ISO8601::DateTime.new('2017-01-01T00:00:00Z') }
241
- ]
242
-
243
- pairs.each do |pair|
244
- it "should calculate correctly the start_time for #{pair[:pattern]}" do
245
- expect(ISO8601::TimeInterval.parse(pair[:pattern]).first.to_s).to eq(pair[:start_time].to_s)
246
- end
247
- end
248
- end
249
-
250
- describe "February" do
251
- pairs = [
252
- { pattern: 'P1Y/2017-02-01T00:00:00Z',
253
- start_time: ISO8601::DateTime.new('2016-02-01T00:00:00Z') },
254
- { pattern: 'P1M/2017-02-01T00:00:00Z',
255
- start_time: ISO8601::DateTime.new('2017-01-01T00:00:00Z') },
256
- { pattern: 'P1D/2017-02-01T00:00:00Z',
257
- start_time: ISO8601::DateTime.new('2017-01-31T00:00:00Z') },
258
- { pattern: 'PT1H/2017-02-01T01:00:00Z',
259
- start_time: ISO8601::DateTime.new('2017-02-01T00:00:00Z') }
260
- ]
261
-
262
- pairs.each do |pair|
263
- it "should calculate correctly the start_time for #{pair[:pattern]}" do
264
- expect(ISO8601::TimeInterval.parse(pair[:pattern]).first.to_s).to eq(pair[:start_time].to_s)
265
- end
266
- end
267
- end
268
-
269
- describe "March" do
270
- pairs = [
271
- { pattern: 'P1Y/2017-03-01T00:00:00Z',
272
- start_time: ISO8601::DateTime.new('2016-03-01T00:00:00Z') },
273
- { pattern: 'P1M/2017-03-01T00:00:00Z',
274
- start_time: ISO8601::DateTime.new('2017-02-01T00:00:00Z') },
275
- { pattern: 'P1D/2017-03-01T00:00:00Z',
276
- start_time: ISO8601::DateTime.new('2017-02-28T00:00:00Z') },
277
- { pattern: 'PT1H/2017-03-01T01:00:00Z',
278
- start_time: ISO8601::DateTime.new('2017-03-01T00:00:00Z') }
279
- ]
280
-
281
- pairs.each do |pair|
282
- it "should calculate correctly the start_time for #{pair[:pattern]}" do
283
- expect(ISO8601::TimeInterval.parse(pair[:pattern]).first.to_s).to eq(pair[:start_time].to_s)
284
- end
285
- end
286
- end
287
- end
288
-
289
- describe "#last" do
290
- it "should return always a ISO8601::DateTime object" do
291
- pattern = 'PT1H/2010-05-09T10:30:00Z'
292
- pattern2 = '2010-05-09T11:30:00Z/PT1H'
293
- pattern3 = '2010-05-09T11:30:00Z/2010-05-09T12:30:00Z'
294
-
295
- expect(ISO8601::TimeInterval.parse(pattern).last).to be_an_instance_of(ISO8601::DateTime)
296
- expect(ISO8601::TimeInterval.parse(pattern2).last).to be_an_instance_of(ISO8601::DateTime)
297
- expect(ISO8601::TimeInterval.parse(pattern3).last).to be_an_instance_of(ISO8601::DateTime)
298
- end
299
-
300
- it "should calculate correctly the last datetime" do
301
- end_time = ISO8601::DateTime.new('2010-05-09T10:30:00Z')
302
- pattern = 'PT1H/2010-05-09T10:30:00Z'
303
- pattern2 = '2010-05-09T09:30:00Z/PT1H'
304
- pattern3 = '2010-05-09T09:30:00Z/2010-05-09T10:30:00Z'
305
-
306
- expect(ISO8601::TimeInterval.parse(pattern).last).to eq(end_time)
307
- expect(ISO8601::TimeInterval.parse(pattern2).last).to eq(end_time)
308
- expect(ISO8601::TimeInterval.parse(pattern3).last).to eq(end_time)
309
- end
310
- end
311
-
312
- describe "#to_s" do
313
- it "should return the pattern if TimeInterval is initialized with a pattern" do
314
- pattern = 'P1Y1M1DT0,5S/2014-05-31T16:26:10Z'
315
- pattern2 = '2007-03-01T13:00:00Z/P1Y'
316
-
317
- expect(ISO8601::TimeInterval.parse(pattern).to_s).to eq(pattern)
318
- expect(ISO8601::TimeInterval.parse(pattern2).to_s).to eq(pattern2)
319
- end
320
-
321
- it "should build the pattern and return if TimeInterval is initialized with objects" do
322
- duration = ISO8601::Duration.new('P1Y1M1DT0.5H')
323
- datetime = ISO8601::DateTime.new('2010-05-09T10:30:12+00:00')
324
- datetime2 = ISO8601::DateTime.new('2010-05-15T10:30:12+00:00')
325
-
326
- expect(ISO8601::TimeInterval.from_duration(duration, datetime).to_s).to eq('P1Y1M1DT0.5H/2010-05-09T10:30:12+00:00')
327
- expect(ISO8601::TimeInterval.from_duration(datetime, duration).to_s).to eq('2010-05-09T10:30:12+00:00/P1Y1M1DT0.5H')
328
- expect(ISO8601::TimeInterval.from_datetimes(datetime, datetime2).to_s).to eq('2010-05-09T10:30:12+00:00/2010-05-15T10:30:12+00:00')
329
- end
330
- end
331
-
332
- describe "compare Time Intervals" do
333
- before(:each) do
334
- @small = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
335
- @big = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT2H')
336
- end
337
-
338
- it "should raise TypeError when compared object is not a ISO8601::TimeInterval" do
339
- expect { @small < 'Hello!' }.to raise_error(ArgumentError)
340
- expect { @small > 'Hello!' }.to raise_error(ArgumentError)
341
- end
342
-
343
- it "should check what interval is bigger" do
344
- expect(@small <=> @big).to eq(-1)
345
- expect(@big <=> @small).to eq(1)
346
- expect(@big <=> @big).to eq(0)
347
-
348
- expect(@small > @big).to be_falsy
349
- expect(@big > @small).to be_truthy
350
- expect(@small > @small).to be_falsy
351
- end
352
-
353
- it "should check if interval is bigger or equal than other" do
354
- expect(@small >= @big).to be_falsy
355
- expect(@big >= @small).to be_truthy
356
- expect(@small >= @small).to be_truthy
357
- end
358
-
359
- it "should check what interval is smaller" do
360
- expect(@small < @big).to be_truthy
361
- expect(@big < @small).to be_falsy
362
- expect(@small < @small).to be_falsy
363
- end
364
-
365
- it "should check if interval is smaller or equal than other" do
366
- expect(@small <= @big).to be_truthy
367
- expect(@big <= @small).to be_falsy
368
- expect(@small <= @small).to be_truthy
369
- end
370
-
371
- it "should check if the intervals are equals" do
372
- expect(@small == @small).to be_truthy
373
- expect(@small == @small.to_f).to be_falsy
374
- expect(@small == @big).to be_falsy
375
- expect(@small == @big.to_f).to be_falsy
376
- end
377
- end
378
-
379
- describe "#eql?" do
380
- it "should be equal only when start_time and end_time are the same" do
381
- interval = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
382
- interval2 = ISO8601::TimeInterval.parse('2007-03-01T14:00:00Z/PT1H')
383
- interval3 = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
384
-
385
- expect(interval.eql?(interval2)).to be_falsy
386
- expect(interval.eql?(interval3)).to be_truthy
387
- end
388
- end
389
-
390
- describe "#include?" do
391
- it "raise TypeError when the parameter is not valid" do
392
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
393
- expect { ti.include?('hola') }.to raise_error(ISO8601::Errors::TypeError)
394
- expect { ti.include?(123) }.to raise_error(ISO8601::Errors::TypeError)
395
- expect { ti.include?(ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')) }.to raise_error(ISO8601::Errors::TypeError)
396
- end
397
-
398
- it "should check if a DateTime is included" do
399
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1DT1H')
400
- included = DateTime.new(2007, 3, 1, 15, 0, 0)
401
- included_iso8601 = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
402
- not_included = DateTime.new(2007, 2, 1, 15, 0, 0)
403
- not_included_iso8601 = ISO8601::DateTime.new('2007-03-01T11:00:00Z')
404
-
405
- expect(ti.include?(included)).to be_truthy
406
- expect(ti.include?(included_iso8601)).to be_truthy
407
- expect(ti.include?(not_included)).to be_falsy
408
- expect(ti.include?(not_included_iso8601)).to be_falsy
409
- end
410
- end
411
-
412
- describe "#subset?" do
413
- it "raise TypeError when the parameter is not valid" do
414
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
415
- dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
416
-
417
- expect { ti.subset?('2007-03-01T18:00:00Z') }.to raise_error(ISO8601::Errors::TypeError)
418
- expect { ti.subset?(123) }.to raise_error(ISO8601::Errors::TypeError)
419
- expect { ti.subset?(dt) }.to raise_error(ISO8601::Errors::TypeError)
420
- end
421
-
422
- it "should check if an interval is subset of another one" do
423
- ti = ISO8601::TimeInterval.parse('2015-01-15T00:00:00Z/P1D')
424
- ti2 = ISO8601::TimeInterval.parse('2015-01-01T00:00:00Z/P1M')
425
-
426
- expect(ti.subset?(ti)).to be_truthy
427
- expect(ti.subset?(ti2)).to be_truthy
428
- expect(ti2.subset?(ti)).to be_falsy
429
- end
430
- end
431
-
432
- describe "#superset?" do
433
- it "raise TypeError when the parameter is not valid" do
434
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
435
- dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
436
-
437
- expect { ti.superset?('2007-03-01T18:00:00Z') }.to raise_error(ISO8601::Errors::TypeError)
438
- expect { ti.superset?(123) }.to raise_error(ISO8601::Errors::TypeError)
439
- expect { ti.superset?(dt) }.to raise_error(ISO8601::Errors::TypeError)
440
- end
441
-
442
- it "should check if the interval is superset of the given one" do
443
- ti = ISO8601::TimeInterval.parse('2015-01-01T00:00:00Z/P1M')
444
- ti2 = ISO8601::TimeInterval.parse('2015-01-15T00:00:00Z/P1D')
445
- ti3 = ISO8601::TimeInterval.parse('2015-03-01T00:00:00Z/P1D')
446
-
447
- expect(ti.superset?(ti)).to be_truthy
448
- expect(ti.superset?(ti2)).to be_truthy
449
- expect(ti.superset?(ti3)).to be_falsy
450
- end
451
- end
452
-
453
- describe "#intersect?" do
454
- it "raise TypeError when the parameter is not valid" do
455
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
456
- dt = DateTime.new(2007, 2, 1, 15, 0, 0)
457
- dt_iso8601 = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
458
-
459
- expect { ti.intersect?('hola') }.to raise_error(ISO8601::Errors::TypeError)
460
- expect { ti.intersect?(123) }.to raise_error(ISO8601::Errors::TypeError)
461
- expect { ti.intersect?(dt) }.to raise_error(ISO8601::Errors::TypeError)
462
- expect { ti.intersect?(dt_iso8601) }.to raise_error(ISO8601::Errors::TypeError)
463
- end
464
-
465
- it "should check if two intervals intersect" do
466
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1DT1H')
467
- included = ISO8601::TimeInterval.parse('2007-03-01T14:00:00Z/PT2H')
468
- overlaped = ISO8601::TimeInterval.parse('2007-03-01T18:00:00Z/P1DT1H')
469
- not_overlaped = ISO8601::TimeInterval.parse('2007-03-14T14:00:00Z/PT2H')
470
-
471
- expect(ti.intersect?(included)).to be_truthy
472
- expect(ti.intersect?(overlaped)).to be_truthy
473
- expect(ti.intersect?(not_overlaped)).to be_falsy
474
- end
475
- end
476
-
477
- describe "#intersection" do
478
- let(:small) { ISO8601::TimeInterval.parse('2015-06-15/P1D') }
479
- let(:big) { ISO8601::TimeInterval.parse('2015-06-01/P1M') }
480
- let(:other) { ISO8601::TimeInterval.parse('2015-06-30/P1D') }
481
-
482
- it "raise TypeError when the parameter is not valid" do
483
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
484
- dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
485
-
486
- expect { ti.intersection('2007-03-01T13:00:00Z/PT1H') }.to raise_error(ISO8601::Errors::TypeError)
487
- expect { ti.intersection(1) }.to raise_error(ISO8601::Errors::TypeError)
488
- expect { ti.intersection(dt) }.to raise_error(ISO8601::Errors::TypeError)
489
- end
490
-
491
- it "raise IntervalError when the intervals are disjoint" do
492
- expect { small.intersection(other) }.to raise_error(ISO8601::Errors::IntervalError)
493
- end
494
-
495
- it "should return the smallest when one is subset of the other" do
496
- expect(small.intersection(small)).to eq(small)
497
- expect(big.intersection(small)).to eq(small)
498
- expect(small.intersection(big)).to eq(small)
499
- end
500
- end
501
-
502
- describe "#disjoint?" do
503
- it "raise TypeError when the parameter is not valid" do
504
- ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
505
- dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
506
-
507
- expect { ti.disjoint?('2007-03-01T13:00:00Z/PT1H') }.to raise_error(ISO8601::Errors::TypeError)
508
- expect { ti.disjoint?(1) }.to raise_error(ISO8601::Errors::TypeError)
509
- expect { ti.disjoint?(dt) }.to raise_error(ISO8601::Errors::TypeError)
510
- end
511
-
512
- it "should check if two intervals are disjoint" do
513
- ti = ISO8601::TimeInterval.parse('2015-01-01T00:00:00Z/P1D')
514
- ti2 = ISO8601::TimeInterval.parse('2015-02-01T00:00:00Z/P1D')
515
-
516
- expect(ti.disjoint?(ti)).to be_falsy
517
- expect(ti.disjoint?(ti2)).to be_truthy
518
- end
519
- end
520
- end