iso8601 0.8.7 → 0.9.0

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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/CONTRIBUTING.md +58 -0
  4. data/Gemfile +2 -2
  5. data/README.md +28 -102
  6. data/docs/date-time.md +86 -0
  7. data/docs/duration.md +77 -0
  8. data/docs/time-interval.md +120 -0
  9. data/iso8601.gemspec +43 -6
  10. data/lib/iso8601.rb +15 -7
  11. data/lib/iso8601/atomic.rb +78 -0
  12. data/lib/iso8601/date.rb +35 -10
  13. data/lib/iso8601/date_time.rb +14 -3
  14. data/lib/iso8601/days.rb +47 -0
  15. data/lib/iso8601/duration.rb +115 -99
  16. data/lib/iso8601/errors.rb +13 -1
  17. data/lib/iso8601/hours.rb +43 -0
  18. data/lib/iso8601/minutes.rb +43 -0
  19. data/lib/iso8601/months.rb +98 -0
  20. data/lib/iso8601/seconds.rb +47 -0
  21. data/lib/iso8601/time.rb +43 -15
  22. data/lib/iso8601/time_interval.rb +392 -0
  23. data/lib/iso8601/version.rb +1 -1
  24. data/lib/iso8601/weeks.rb +43 -0
  25. data/lib/iso8601/years.rb +80 -0
  26. data/spec/iso8601/date_spec.rb +0 -6
  27. data/spec/iso8601/date_time_spec.rb +0 -8
  28. data/spec/iso8601/days_spec.rb +44 -0
  29. data/spec/iso8601/duration_spec.rb +103 -99
  30. data/spec/iso8601/hours_spec.rb +44 -0
  31. data/spec/iso8601/minutes_spec.rb +44 -0
  32. data/spec/iso8601/months_spec.rb +86 -0
  33. data/spec/iso8601/seconds_spec.rb +44 -0
  34. data/spec/iso8601/time_interval_spec.rb +416 -0
  35. data/spec/iso8601/time_spec.rb +0 -6
  36. data/spec/iso8601/weeks_spec.rb +46 -0
  37. data/spec/iso8601/years_spec.rb +69 -0
  38. metadata +37 -19
  39. data/.dockerignore +0 -7
  40. data/.editorconfig +0 -9
  41. data/.gitignore +0 -19
  42. data/.rubocop.yml +0 -38
  43. data/.travis.yml +0 -19
  44. data/Dockerfile +0 -10
  45. data/Makefile +0 -19
  46. data/circle.yml +0 -13
  47. data/lib/iso8601/atoms.rb +0 -279
  48. data/spec/iso8601/atoms_spec.rb +0 -329
@@ -0,0 +1,44 @@
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
+ [: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
@@ -0,0 +1,44 @@
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
+ [: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
@@ -0,0 +1,86 @@
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
+ describe 'Atomic' do
11
+ let(:subject) { ISO8601::Months.new(1) }
12
+
13
+ it "should respond to the Atomic interface" do
14
+ [:factor,
15
+ :to_seconds,
16
+ :symbol,
17
+ :to_i,
18
+ :to_f,
19
+ :to_s,
20
+ :value,
21
+ :<=>,
22
+ :eql?,
23
+ :hash,
24
+ :valid_atom?].each { |m| expect(subject).to respond_to(m) }
25
+ end
26
+ end
27
+
28
+ describe '#factor' do
29
+ it "should return the Month factor" do
30
+ expect { ISO8601::Months.new(1).factor }.to_not raise_error
31
+ expect(ISO8601::Months.new(2).factor).to eq(2628000)
32
+ end
33
+
34
+ it "should return the Month factor for a common year" do
35
+ expect(ISO8601::Months.new(1).factor(common_year)).to eq(2678400)
36
+ end
37
+
38
+ it "should return the Month factor for a leap year" do
39
+ expect(ISO8601::Months.new(1).factor(leap_year)).to eq(2678400)
40
+ end
41
+
42
+ it "should return the Month factor based on february for a common year" do
43
+ expect(ISO8601::Months.new(1).factor(common_february)).to eq(2419200)
44
+ end
45
+
46
+ it "should return the Month factor based on february for a leap year" do
47
+ expect(ISO8601::Months.new(1).factor(leap_february)).to eq(2505600)
48
+ end
49
+ end
50
+
51
+ describe '#to_seconds' do
52
+ it "should return the amount of seconds" do
53
+ expect(ISO8601::Months.new(2).to_seconds).to eq(5256000)
54
+ end
55
+
56
+ it "should return the amount of seconds for a common year" do
57
+ expect(ISO8601::Months.new(2).to_seconds(common_year)).to eq(5097600)
58
+ end
59
+
60
+ it "should return the amount of seconds for a leap year" do
61
+ expect(ISO8601::Months.new(2).to_seconds(leap_year)).to eq(5184000)
62
+ end
63
+
64
+ it "should return the amount of seconds based on februrary for a common year" do
65
+ expect(ISO8601::Months.new(2).to_seconds(common_february)).to eq(5097600)
66
+ end
67
+
68
+ it "should return the amount of seconds based on february for a leap year" do
69
+ expect(ISO8601::Months.new(2).to_seconds(leap_february)).to eq(5184000)
70
+ expect(ISO8601::Months.new(12).to_seconds(leap_february)).to eq(31622400)
71
+ expect(ISO8601::Months.new(12).to_seconds(leap_february)).to eq(ISO8601::Years.new(1).to_seconds(leap_year))
72
+ end
73
+ end
74
+
75
+ describe '#symbol' do
76
+ it "should return the ISO symbol" do
77
+ expect(ISO8601::Months.new(1).symbol).to eq(:M)
78
+ end
79
+ end
80
+
81
+ describe '#hash' do
82
+ it "should build hash identity by value" do
83
+ expect(ISO8601::Months.new(3).hash).to eq(ISO8601::Months.new(3).hash)
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,44 @@
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
+ [: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
@@ -0,0 +1,416 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe ISO8601::TimeInterval do
6
+ describe 'pattern initialization' do
7
+ it "should raise a ISO8601::Errors::UnknownPattern if it not a valid interval pattern" do
8
+ # Invalid separators
9
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00ZP1Y2M10DT2H30M') }
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('2007-03-01T13:00:00Z~P1Y2M10D') }
14
+ .to raise_error(ISO8601::Errors::UnknownPattern)
15
+ expect { ISO8601::TimeInterval.parse('P1Y2M10DT2H30M2007-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('P1Y2M10D~2007-03-01T13:00:00Z') }
20
+ .to raise_error(ISO8601::Errors::UnknownPattern)
21
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z2008-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
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z~2008-05-11T15:30:00Z') }
26
+ .to raise_error(ISO8601::Errors::UnknownPattern)
27
+ end
28
+
29
+ describe 'with duration' do
30
+ it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
31
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/') }
32
+ .to raise_error(ISO8601::Errors::UnknownPattern)
33
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P') }
34
+ .to raise_error(ISO8601::Errors::UnknownPattern)
35
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT') }
36
+ .to raise_error(ISO8601::Errors::UnknownPattern)
37
+ end
38
+ end
39
+
40
+ describe 'with DateTimes' do
41
+ it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
42
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/2010-0-09') }
43
+ .to raise_error(ISO8601::Errors::UnknownPattern)
44
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/2010-05-09T103012+0400') }
45
+ .to raise_error(ISO8601::Errors::UnknownPattern)
46
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/2014-W15-02T10:11:12Z') }
47
+ .to raise_error(ISO8601::Errors::UnknownPattern)
48
+ end
49
+ end
50
+
51
+ it "should raise a ISO8601::Errors::UnknownPattern if start time and end time are durations" do
52
+ expect { ISO8601::TimeInterval.parse('P1Y2M10D/P1Y2M10D') }.to raise_error(ISO8601::Errors::UnknownPattern)
53
+ expect { ISO8601::TimeInterval.parse('P1Y0.5M/P1Y0.5M') }.to raise_error(ISO8601::Errors::UnknownPattern)
54
+ end
55
+
56
+ context "allowed patterns" do
57
+ it "should parse <start>/<duration>" do
58
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y') }.to_not raise_error
59
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P0.5Y') }.to_not raise_error
60
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y0.5M') }.to_not raise_error
61
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y0,5M') }.to_not raise_error
62
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y1M1D') }.to_not raise_error
63
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y1M1DT1H1M1.0S') }.to_not raise_error
64
+ expect { ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1Y1M1DT1H1M1,0S') }.to_not raise_error
65
+ end
66
+
67
+ it "should parse <duration>/<end>" do
68
+ expect { ISO8601::TimeInterval.parse('P1Y0,5M/2010-05-09T10:30:12+04') }.to_not raise_error
69
+ expect { ISO8601::TimeInterval.parse('P1Y1M1D/2010-05-09T10:30:12+04:00') }.to_not raise_error
70
+ expect { ISO8601::TimeInterval.parse('P1Y1M0.5D/2010-05-09T10:30:12-04:00') }.to_not raise_error
71
+ expect { ISO8601::TimeInterval.parse('P1Y1M0,5D/2010-05-09T10:30:12-00:00') }.to_not raise_error
72
+ expect { ISO8601::TimeInterval.parse('P1Y1M1DT1H/-2014-05-31T16:26:00Z') }.to_not raise_error
73
+ expect { ISO8601::TimeInterval.parse('P1Y1M1DT0.5H/2014-05-31T16:26:10.5Z') }.to_not raise_error
74
+ expect { ISO8601::TimeInterval.parse('P1Y1M1DT0,5H/2014-05-31T16:26:10,5Z') }.to_not raise_error
75
+ end
76
+
77
+ it "should parse <start>/<end>" do
78
+ expect { ISO8601::TimeInterval.parse('2014-001/2010-05-09T10:30') }.to_not raise_error
79
+ expect { ISO8601::TimeInterval.parse('2014121/2010-05-09T10:30:12') }.to_not raise_error
80
+ expect { ISO8601::TimeInterval.parse('2014-121T10:11:12Z/2010-05-09T10:30:12Z') }.to_not raise_error
81
+ expect { ISO8601::TimeInterval.parse('20100509T103012+0400/2010-05-09T10:30:12+04') }.to_not raise_error
82
+ expect { ISO8601::TimeInterval.parse('20100509/2010-05-09T10:30:12+04:00') }.to_not raise_error
83
+ expect { ISO8601::TimeInterval.parse('T103012+0400/2010-05-09T10:30:12-04:00') }.to_not raise_error
84
+ expect { ISO8601::TimeInterval.parse('T103012+04/2010-05-09T10:30:12-00:00') }.to_not raise_error
85
+ end
86
+ end
87
+ end
88
+
89
+ describe 'initialization with a ISO8601::Duration' do
90
+ # it "should raise a ISO8601::Errors::TypeError if parameter is not a ISO8601::Duration" do
91
+ # datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
92
+
93
+ # expect { ISO8601::TimeInterval.from_duration('hi', {}) }.to raise_error(ISO8601::Errors::TypeError)
94
+ # expect { ISO8601::TimeInterval.from_duration([], {}) }.to raise_error(ISO8601::Errors::TypeError)
95
+ # expect { ISO8601::TimeInterval.from_duration(datetime, {}) }.to raise_error(ISO8601::Errors::TypeError)
96
+ # expect { ISO8601::TimeInterval.from_duration({}, {}) }.to raise_error(ISO8601::Errors::TypeError)
97
+ # end
98
+
99
+ # it "should raise an ISO8601::Errors::TypeError if the time hash is no valid" do
100
+ # duration = ISO8601::Duration.new('P1Y1M1DT0.5H')
101
+ # datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
102
+
103
+ # expect { ISO8601::TimeInterval.from_duration(duration, { time: datetime }) }.to raise_error(ISO8601::Errors::TypeError)
104
+ # expect { ISO8601::TimeInterval.from_duration(duration, { start_time: nil }) }.to raise_error(ISO8601::Errors::TypeError)
105
+ # expect { ISO8601::TimeInterval.from_duration(duration, { start_time: datetime, end_time: datetime }) }.to raise_error(ISO8601::Errors::TypeError)
106
+ # expect { ISO8601::TimeInterval.from_duration(duration, {}) }.to raise_error(ISO8601::Errors::TypeError)
107
+ # end
108
+
109
+ it "should initialize with a valid duration and time" do
110
+ time = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
111
+ duration = ISO8601::Duration.new('P1M')
112
+
113
+ expect { ISO8601::TimeInterval.from_duration(time, duration) }.to_not raise_error
114
+ expect { ISO8601::TimeInterval.from_duration(duration, time) }.to_not raise_error
115
+ end
116
+ end
117
+
118
+ describe 'initialization with a ISO8601::DateTime' do
119
+ it "should raise a ISO8601::Errors::TypeError if parameters are not an ISO8601::DateTime instance" do
120
+ duration = ISO8601::Duration.new('P1Y1M1DT0.5H')
121
+ datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
122
+
123
+ expect { ISO8601::TimeInterval.from_datetimes(duration, datetime) }.to raise_error(ISO8601::Errors::TypeError)
124
+ expect { ISO8601::TimeInterval.from_datetimes(datetime, duration) }.to raise_error(ISO8601::Errors::TypeError)
125
+ expect { ISO8601::TimeInterval.from_datetimes(datetime, 'Hello!') }.to raise_error(ISO8601::Errors::TypeError)
126
+ expect { ISO8601::TimeInterval.from_datetimes({}, datetime) }.to raise_error(ISO8601::Errors::TypeError)
127
+ end
128
+
129
+ it "should initialize class with a valid datetimes" do
130
+ datetime = ISO8601::DateTime.new('2010-05-09T10:30:12Z')
131
+ datetime2 = ISO8601::DateTime.new('2010-05-15T10:30:12Z')
132
+
133
+ expect { ISO8601::TimeInterval.from_datetimes(datetime, datetime2) }.to_not raise_error
134
+ expect { ISO8601::TimeInterval.from_datetimes(datetime2, datetime) }.to_not raise_error
135
+ end
136
+ end
137
+
138
+ describe "#to_f" do
139
+ it "should calculate the size of time interval" do
140
+ hour = (60 * 60).to_f
141
+ pattern = 'PT1H/2010-05-09T10:30:00Z'
142
+ pattern2 = '2010-05-09T11:30:00Z/PT1H'
143
+ pattern3 = '2010-05-09T11:30:00Z/2010-05-09T12:30:00Z'
144
+
145
+ expect(ISO8601::TimeInterval.parse(pattern).to_f).to eq(hour)
146
+ expect(ISO8601::TimeInterval.parse(pattern2).to_f).to eq(hour)
147
+ expect(ISO8601::TimeInterval.parse(pattern3).to_f).to eq(hour)
148
+ end
149
+
150
+ it "should be 0" do
151
+ expect(ISO8601::TimeInterval.parse('2015-01-01/2015-01-01').to_f).to eq(0)
152
+ end
153
+ end
154
+
155
+ describe "#empty?" do
156
+ it "should check if the interval is empty" do
157
+ expect(ISO8601::TimeInterval.parse('2015-01-01/2015-01-01').empty?).to be_truthy
158
+ expect(ISO8601::TimeInterval.parse('2015-01-01/2015-01-02').empty?).to be_falsy
159
+ end
160
+ end
161
+
162
+ describe "#start_time" do
163
+ it "should return always a ISO8601::DateTime object" do
164
+ pattern = 'PT1H/2010-05-09T10:30:00Z'
165
+ pattern2 = '2010-05-09T11:30:00Z/PT1H'
166
+ pattern3 = '2010-05-09T11:30:00Z/2010-05-09T12:30:00Z'
167
+
168
+ expect(ISO8601::TimeInterval.parse(pattern).first).to be_an_instance_of(ISO8601::DateTime)
169
+ expect(ISO8601::TimeInterval.parse(pattern2).first).to be_an_instance_of(ISO8601::DateTime)
170
+ expect(ISO8601::TimeInterval.parse(pattern3).first).to be_an_instance_of(ISO8601::DateTime)
171
+ end
172
+
173
+ it "should calculate correctly the start_time" do
174
+ start_time = ISO8601::DateTime.new('2010-05-09T10:30:00Z')
175
+ pattern = 'PT1H/2010-05-09T11:30:00Z'
176
+ pattern2 = '2010-05-09T10:30:00Z/PT1H'
177
+ pattern3 = '2010-05-09T10:30:00Z/2010-05-09T12:30:00Z'
178
+
179
+ expect(ISO8601::TimeInterval.parse(pattern).first).to eq(start_time)
180
+ expect(ISO8601::TimeInterval.parse(pattern2).first).to eq(start_time)
181
+ expect(ISO8601::TimeInterval.parse(pattern3).first).to eq(start_time)
182
+ end
183
+ end
184
+
185
+ describe "#last" do
186
+ it "should return always a ISO8601::DateTime object" do
187
+ pattern = 'PT1H/2010-05-09T10:30:00Z'
188
+ pattern2 = '2010-05-09T11:30:00Z/PT1H'
189
+ pattern3 = '2010-05-09T11:30:00Z/2010-05-09T12:30:00Z'
190
+
191
+ expect(ISO8601::TimeInterval.parse(pattern).last).to be_an_instance_of(ISO8601::DateTime)
192
+ expect(ISO8601::TimeInterval.parse(pattern2).last).to be_an_instance_of(ISO8601::DateTime)
193
+ expect(ISO8601::TimeInterval.parse(pattern3).last).to be_an_instance_of(ISO8601::DateTime)
194
+ end
195
+
196
+ it "should calculate correctly the last datetime" do
197
+ end_time = ISO8601::DateTime.new('2010-05-09T10:30:00Z')
198
+ pattern = 'PT1H/2010-05-09T10:30:00Z'
199
+ pattern2 = '2010-05-09T09:30:00Z/PT1H'
200
+ pattern3 = '2010-05-09T09:30:00Z/2010-05-09T10:30:00Z'
201
+
202
+ expect(ISO8601::TimeInterval.parse(pattern).last).to eq(end_time)
203
+ expect(ISO8601::TimeInterval.parse(pattern2).last).to eq(end_time)
204
+ expect(ISO8601::TimeInterval.parse(pattern3).last).to eq(end_time)
205
+ end
206
+ end
207
+
208
+ describe "#to_s" do
209
+ it "should return the pattern if TimeInterval is initialized with a pattern" do
210
+ pattern = 'P1Y1M1DT0,5H/2014-05-31T16:26:10,5Z'
211
+ pattern2 = '2007-03-01T13:00:00Z/P1Y0,5M'
212
+
213
+ expect(ISO8601::TimeInterval.parse(pattern).to_s).to eq(pattern)
214
+ expect(ISO8601::TimeInterval.parse(pattern2).to_s).to eq(pattern2)
215
+ end
216
+
217
+ it "should build the pattern and return if TimeInterval is initialized with objects" do
218
+ duration = ISO8601::Duration.new('P1Y1M1DT0.5H')
219
+ datetime = ISO8601::DateTime.new('2010-05-09T10:30:12+00:00')
220
+ datetime2 = ISO8601::DateTime.new('2010-05-15T10:30:12+00:00')
221
+
222
+ expect(ISO8601::TimeInterval.from_duration(duration, datetime).to_s).to eq('P1Y1M1DT0.5H/2010-05-09T10:30:12+00:00')
223
+ expect(ISO8601::TimeInterval.from_duration(datetime, duration).to_s).to eq('2010-05-09T10:30:12+00:00/P1Y1M1DT0.5H')
224
+ 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')
225
+ end
226
+ end
227
+
228
+ describe "compare Time Intervals" do
229
+ before(:each) do
230
+ @small = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
231
+ @big = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT2H')
232
+ end
233
+
234
+ it "should raise TypeError when compared object is not a ISO8601::TimeInterval" do
235
+ expect { @small < 'Hello!' }.to raise_error(ArgumentError)
236
+ expect { @small > 'Hello!' }.to raise_error(ArgumentError)
237
+ end
238
+
239
+ it "should check what interval is bigger" do
240
+ expect(@small <=> @big).to eq(-1)
241
+ expect(@big <=> @small).to eq(1)
242
+ expect(@big <=> @big).to eq(0)
243
+
244
+ expect(@small > @big).to be_falsy
245
+ expect(@big > @small).to be_truthy
246
+ expect(@small > @small).to be_falsy
247
+ end
248
+
249
+ it "should check if interval is bigger or equal than other" do
250
+ expect(@small >= @big).to be_falsy
251
+ expect(@big >= @small).to be_truthy
252
+ expect(@small >= @small).to be_truthy
253
+ end
254
+
255
+ it "should check what interval is smaller" do
256
+ expect(@small < @big).to be_truthy
257
+ expect(@big < @small).to be_falsy
258
+ expect(@small < @small).to be_falsy
259
+ end
260
+
261
+ it "should check if interval is smaller or equal than other" do
262
+ expect(@small <= @big).to be_truthy
263
+ expect(@big <= @small).to be_falsy
264
+ expect(@small <= @small).to be_truthy
265
+ end
266
+
267
+ it "should check if the intervals are equals" do
268
+ expect(@small == @small).to be_truthy
269
+ expect(@small == @small.to_f).to be_falsy
270
+ expect(@small == @big).to be_falsy
271
+ expect(@small == @big.to_f).to be_falsy
272
+ end
273
+ end
274
+
275
+ describe "#eql?" do
276
+ it "should be equal only when start_time and end_time are the same" do
277
+ interval = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
278
+ interval2 = ISO8601::TimeInterval.parse('2007-03-01T14:00:00Z/PT1H')
279
+ interval3 = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
280
+
281
+ expect(interval.eql?(interval2)).to be_falsy
282
+ expect(interval.eql?(interval3)).to be_truthy
283
+ end
284
+ end
285
+
286
+ describe "#include?" do
287
+ it "raise TypeError when the parameter is not valid" do
288
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
289
+ expect { ti.include?('hola') }.to raise_error(ISO8601::Errors::TypeError)
290
+ expect { ti.include?(123) }.to raise_error(ISO8601::Errors::TypeError)
291
+ expect { ti.include?(ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')) }.to raise_error(ISO8601::Errors::TypeError)
292
+ end
293
+
294
+ it "should check if a DateTime is included" do
295
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1DT1H')
296
+ included = DateTime.new(2007, 3, 1, 15, 0, 0)
297
+ included_iso8601 = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
298
+ not_included = DateTime.new(2007, 2, 1, 15, 0, 0)
299
+ not_included_iso8601 = ISO8601::DateTime.new('2007-03-01T11:00:00Z')
300
+
301
+ expect(ti.include?(included)).to be_truthy
302
+ expect(ti.include?(included_iso8601)).to be_truthy
303
+ expect(ti.include?(not_included)).to be_falsy
304
+ expect(ti.include?(not_included_iso8601)).to be_falsy
305
+ end
306
+ end
307
+
308
+ describe "#subset?" do
309
+ it "raise TypeError when the parameter is not valid" do
310
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
311
+ dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
312
+
313
+ expect { ti.subset?('2007-03-01T18:00:00Z') }.to raise_error(ISO8601::Errors::TypeError)
314
+ expect { ti.subset?(123) }.to raise_error(ISO8601::Errors::TypeError)
315
+ expect { ti.subset?(dt) }.to raise_error(ISO8601::Errors::TypeError)
316
+ end
317
+
318
+ it "should check if an interval is subset of another one" do
319
+ ti = ISO8601::TimeInterval.parse('2015-01-15T00:00:00Z/P1D')
320
+ ti2 = ISO8601::TimeInterval.parse('2015-01-01T00:00:00Z/P1M')
321
+
322
+ expect(ti.subset?(ti)).to be_truthy
323
+ expect(ti.subset?(ti2)).to be_truthy
324
+ expect(ti2.subset?(ti)).to be_falsy
325
+ end
326
+ end
327
+
328
+ describe "#superset?" do
329
+ it "raise TypeError when the parameter is not valid" do
330
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
331
+ dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
332
+
333
+ expect { ti.superset?('2007-03-01T18:00:00Z') }.to raise_error(ISO8601::Errors::TypeError)
334
+ expect { ti.superset?(123) }.to raise_error(ISO8601::Errors::TypeError)
335
+ expect { ti.superset?(dt) }.to raise_error(ISO8601::Errors::TypeError)
336
+ end
337
+
338
+ it "should check if the interval is superset of the given one" do
339
+ ti = ISO8601::TimeInterval.parse('2015-01-01T00:00:00Z/P1M')
340
+ ti2 = ISO8601::TimeInterval.parse('2015-01-15T00:00:00Z/P1D')
341
+ ti3 = ISO8601::TimeInterval.parse('2015-03-01T00:00:00Z/P1D')
342
+
343
+ expect(ti.superset?(ti)).to be_truthy
344
+ expect(ti.superset?(ti2)).to be_truthy
345
+ expect(ti.superset?(ti3)).to be_falsy
346
+ end
347
+ end
348
+
349
+ describe "#intersect?" do
350
+ it "raise TypeError when the parameter is not valid" do
351
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
352
+ dt = DateTime.new(2007, 2, 1, 15, 0, 0)
353
+ dt_iso8601 = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
354
+
355
+ expect { ti.intersect?('hola') }.to raise_error(ISO8601::Errors::TypeError)
356
+ expect { ti.intersect?(123) }.to raise_error(ISO8601::Errors::TypeError)
357
+ expect { ti.intersect?(dt) }.to raise_error(ISO8601::Errors::TypeError)
358
+ expect { ti.intersect?(dt_iso8601) }.to raise_error(ISO8601::Errors::TypeError)
359
+ end
360
+
361
+ it "should check if two intervals intersect" do
362
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/P1DT1H')
363
+ included = ISO8601::TimeInterval.parse('2007-03-01T14:00:00Z/PT2H')
364
+ overlaped = ISO8601::TimeInterval.parse('2007-03-01T18:00:00Z/P1DT1H')
365
+ not_overlaped = ISO8601::TimeInterval.parse('2007-03-14T14:00:00Z/PT2H')
366
+
367
+ expect(ti.intersect?(included)).to be_truthy
368
+ expect(ti.intersect?(overlaped)).to be_truthy
369
+ expect(ti.intersect?(not_overlaped)).to be_falsy
370
+ end
371
+ end
372
+
373
+ describe "#intersection" do
374
+ let(:small) { ISO8601::TimeInterval.parse('2015-06-15/P1D') }
375
+ let(:big) { ISO8601::TimeInterval.parse('2015-06-01/P1M') }
376
+ let(:other) { ISO8601::TimeInterval.parse('2015-06-30/P1D') }
377
+
378
+ it "raise TypeError when the parameter is not valid" do
379
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
380
+ dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
381
+
382
+ expect { ti.intersection('2007-03-01T13:00:00Z/PT1H') }.to raise_error(ISO8601::Errors::TypeError)
383
+ expect { ti.intersection(1) }.to raise_error(ISO8601::Errors::TypeError)
384
+ expect { ti.intersection(dt) }.to raise_error(ISO8601::Errors::TypeError)
385
+ end
386
+
387
+ it "raise IntervalError when the intervals are disjoint" do
388
+ expect { small.intersection(other) }.to raise_error(ISO8601::Errors::IntervalError)
389
+ end
390
+
391
+ it "should return the smallest when one is subset of the other" do
392
+ expect(small.intersection(small)).to eq(small)
393
+ expect(big.intersection(small)).to eq(small)
394
+ expect(small.intersection(big)).to eq(small)
395
+ end
396
+ end
397
+
398
+ describe "#disjoint?" do
399
+ it "raise TypeError when the parameter is not valid" do
400
+ ti = ISO8601::TimeInterval.parse('2007-03-01T13:00:00Z/PT1H')
401
+ dt = ISO8601::DateTime.new('2007-03-01T18:00:00Z')
402
+
403
+ expect { ti.disjoint?('2007-03-01T13:00:00Z/PT1H') }.to raise_error(ISO8601::Errors::TypeError)
404
+ expect { ti.disjoint?(1) }.to raise_error(ISO8601::Errors::TypeError)
405
+ expect { ti.disjoint?(dt) }.to raise_error(ISO8601::Errors::TypeError)
406
+ end
407
+
408
+ it "should check if two intervals are disjoint" do
409
+ ti = ISO8601::TimeInterval.parse('2015-01-01T00:00:00Z/P1D')
410
+ ti2 = ISO8601::TimeInterval.parse('2015-02-01T00:00:00Z/P1D')
411
+
412
+ expect(ti.disjoint?(ti)).to be_falsy
413
+ expect(ti.disjoint?(ti2)).to be_truthy
414
+ end
415
+ end
416
+ end