iso8601 0.7.0 → 0.8.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 +15 -5
- data/README.md +7 -7
- data/iso8601.gemspec +1 -1
- data/lib/iso8601.rb +1 -1
- data/lib/iso8601/atoms.rb +126 -28
- data/lib/iso8601/date.rb +23 -4
- data/lib/iso8601/{dateTime.rb → date_time.rb} +21 -1
- data/lib/iso8601/duration.rb +187 -110
- data/lib/iso8601/errors.rb +4 -0
- data/lib/iso8601/time.rb +20 -1
- data/lib/iso8601/version.rb +1 -1
- data/spec/iso8601/atoms_spec.rb +182 -46
- data/spec/iso8601/date_spec.rb +13 -16
- data/spec/iso8601/{dateTime_spec.rb → date_time_spec.rb} +30 -31
- data/spec/iso8601/duration_spec.rb +113 -84
- data/spec/iso8601/time_spec.rb +25 -21
- data/spec/spec_helper.rb +1 -1
- metadata +7 -7
@@ -59,78 +59,77 @@ describe ISO8601::DateTime do
|
|
59
59
|
context 'reduced patterns' do
|
60
60
|
it "should parse correctly reduced dates" do
|
61
61
|
reduced_date = ISO8601::DateTime.new('20100509')
|
62
|
-
reduced_date.year.
|
63
|
-
reduced_date.month.
|
64
|
-
reduced_date.day.
|
62
|
+
expect(reduced_date.year).to eq(2010)
|
63
|
+
expect(reduced_date.month).to eq(5)
|
64
|
+
expect(reduced_date.day).to eq(9)
|
65
65
|
end
|
66
66
|
it "should parse correctly reduced times" do
|
67
67
|
reduced_time = ISO8601::DateTime.new('T101112Z')
|
68
|
-
reduced_time.hour.
|
69
|
-
reduced_time.minute.
|
70
|
-
reduced_time.second.
|
68
|
+
expect(reduced_time.hour).to eq(10)
|
69
|
+
expect(reduced_time.minute).to eq(11)
|
70
|
+
expect(reduced_time.second).to eq(12)
|
71
71
|
end
|
72
72
|
it "should parse correctly reduced date times" do
|
73
73
|
reduced_datetime = ISO8601::DateTime.new('20140531T101112Z')
|
74
|
-
reduced_datetime.year.
|
75
|
-
reduced_datetime.month.
|
76
|
-
reduced_datetime.day.
|
77
|
-
reduced_datetime.hour.
|
78
|
-
reduced_datetime.minute.
|
79
|
-
reduced_datetime.second.
|
74
|
+
expect(reduced_datetime.year).to eq(2014)
|
75
|
+
expect(reduced_datetime.month).to eq(5)
|
76
|
+
expect(reduced_datetime.day).to eq(31)
|
77
|
+
expect(reduced_datetime.hour).to eq(10)
|
78
|
+
expect(reduced_datetime.minute).to eq(11)
|
79
|
+
expect(reduced_datetime.second).to eq(12)
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
83
|
it "should return each atomic value" do
|
84
84
|
dt = ISO8601::DateTime.new('2010-05-09T12:02:01+04:00')
|
85
|
-
dt.year.
|
86
|
-
dt.month.
|
87
|
-
dt.day.
|
88
|
-
dt.hour.
|
89
|
-
dt.minute.
|
90
|
-
dt.second.
|
91
|
-
dt.zone.
|
85
|
+
expect(dt.year).to eq(2010)
|
86
|
+
expect(dt.month).to eq(5)
|
87
|
+
expect(dt.day).to eq(9)
|
88
|
+
expect(dt.hour).to eq(12)
|
89
|
+
expect(dt.minute).to eq(2)
|
90
|
+
expect(dt.second).to eq(1)
|
91
|
+
expect(dt.zone).to eq('+04:00')
|
92
92
|
end
|
93
93
|
|
94
94
|
it "should return the right sign for the given year" do
|
95
|
-
ISO8601::DateTime.new('-2014-05-31T16:26:00Z').year.
|
96
|
-
ISO8601::DateTime.new('+2014-05-31T16:26:00Z').year.
|
95
|
+
expect(ISO8601::DateTime.new('-2014-05-31T16:26:00Z').year).to eq(-2014)
|
96
|
+
expect(ISO8601::DateTime.new('+2014-05-31T16:26:00Z').year).to eq(2014)
|
97
97
|
end
|
98
98
|
|
99
99
|
it "should respond to delegated casting methods" do
|
100
100
|
dt = ISO8601::DateTime.new('2014-12-11T10:09:08Z')
|
101
|
-
dt.
|
101
|
+
expect(dt).to respond_to(:to_s, :to_time, :to_date, :to_datetime)
|
102
102
|
end
|
103
103
|
|
104
104
|
describe '#+' do
|
105
105
|
it "should return the result of the addition" do
|
106
|
-
(ISO8601::DateTime.new('2012-07-07T20:20:20Z') + 10).to_s.
|
107
|
-
(ISO8601::DateTime.new('2012-07-07T20:20:20.5Z') + 10).to_s.
|
108
|
-
(ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10).to_s.
|
106
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') + 10).to_s).to eq('2012-07-07T20:20:30+00:00')
|
107
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20.5Z') + 10).to_s).to eq('2012-07-07T20:20:30.50+00:00')
|
108
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10).to_s).to eq('2012-07-07T20:20:30+02:00')
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
112
|
describe '#-' do
|
113
113
|
it "should return the result of the substraction" do
|
114
|
-
(ISO8601::DateTime.new('2012-07-07T20:20:20Z') - 10).to_s.
|
114
|
+
expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') - 10).to_s).to eq('2012-07-07T20:20:10+00:00')
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
118
|
describe '#to_a' do
|
119
119
|
it "should return an array of atoms" do
|
120
120
|
dt = ISO8601::DateTime.new('2014-05-31T19:29:39Z').to_a
|
121
|
-
dt.
|
122
|
-
dt.
|
121
|
+
expect(dt).to be_kind_of(Array)
|
122
|
+
expect(dt).to eq([2014, 5, 31, 19, 29, 39, '+00:00'])
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
126
126
|
describe '#hash' do
|
127
127
|
it "should return the datetime hash" do
|
128
128
|
subject = ISO8601::DateTime.new('2014-08-16T20:11:10Z')
|
129
|
-
contrast = ::DateTime.new(2014
|
129
|
+
contrast = ISO8601::DateTime.new('2014-08-16T20:11:10Z')
|
130
130
|
|
131
|
-
expect(subject).to
|
131
|
+
expect(subject.hash == contrast.hash).to be_truthy
|
132
132
|
expect(subject.hash).to eq(contrast.hash)
|
133
133
|
end
|
134
134
|
end
|
135
|
-
|
136
135
|
end
|
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
describe ISO8601::Duration do
|
5
|
+
RSpec.describe ISO8601::Duration do
|
6
6
|
it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
|
7
|
+
expect { ISO8601::Duration.new('') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
7
8
|
expect { ISO8601::Duration.new('P') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
8
9
|
expect { ISO8601::Duration.new('PT') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
9
10
|
expect { ISO8601::Duration.new('P1YT') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
@@ -43,33 +44,42 @@ describe ISO8601::Duration do
|
|
43
44
|
end
|
44
45
|
it "should raise a TypeError when the base is not a ISO8601::DateTime" do
|
45
46
|
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', ISO8601::DateTime.new('2010-01-01')) }.to_not raise_error
|
46
|
-
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', '2010-01-01') }.to raise_error(TypeError)
|
47
|
-
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', 2010) }.to raise_error(TypeError)
|
47
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', '2010-01-01') }.to raise_error(ISO8601::Errors::TypeError)
|
48
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', 2010) }.to raise_error(ISO8601::Errors::TypeError)
|
48
49
|
expect {
|
49
50
|
d = ISO8601::Duration.new('P1Y1M1DT1H1M1S', ISO8601::DateTime.new('2010-01-01'))
|
50
51
|
d.base = 2012
|
51
|
-
}.to raise_error(TypeError)
|
52
|
+
}.to raise_error(ISO8601::Errors::TypeError)
|
52
53
|
end
|
53
54
|
|
54
55
|
it "should return a Duration instance from a Numeric input" do
|
55
|
-
|
56
|
+
subject = ISO8601::Duration.new(36993906, ISO8601::DateTime.new('2012-01-01'))
|
57
|
+
contrast = ISO8601::Duration.new('P1Y2M3DT4H5M6S', ISO8601::DateTime.new('2012-01-01'))
|
58
|
+
expect(subject.to_seconds).to eq(contrast.to_seconds)
|
56
59
|
end
|
57
60
|
|
58
61
|
describe '#base' do
|
59
62
|
it "should return the base datetime" do
|
60
63
|
dt = ISO8601::DateTime.new('2010-01-01')
|
61
64
|
dt2 = ISO8601::DateTime.new('2012-01-01')
|
62
|
-
ISO8601::Duration.new('P1Y1M1DT1H1M1S').base.
|
63
|
-
ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base.
|
64
|
-
ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base.
|
65
|
+
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S').base).to be_an_instance_of(NilClass)
|
66
|
+
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base).to be_an_instance_of(ISO8601::DateTime)
|
67
|
+
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base).to eq(dt)
|
65
68
|
d = ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base = dt2
|
66
|
-
d.
|
69
|
+
expect(d).to eq(dt2)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#pattern' do
|
74
|
+
it "should return the duration pattern" do
|
75
|
+
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S').pattern).to eq('P1Y1M1DT1H1M1S')
|
76
|
+
expect(ISO8601::Duration.new(60).pattern).to eq('PT60S')
|
67
77
|
end
|
68
78
|
end
|
69
79
|
|
70
80
|
describe '#to_s' do
|
71
81
|
it "should return the duration as a string" do
|
72
|
-
ISO8601::Duration.new('P1Y1M1DT1H1M1S').to_s.
|
82
|
+
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S').to_s).to eq('P1Y1M1DT1H1M1S')
|
73
83
|
end
|
74
84
|
end
|
75
85
|
|
@@ -79,165 +89,184 @@ describe ISO8601::Duration do
|
|
79
89
|
end
|
80
90
|
|
81
91
|
it "should return the result of the addition" do
|
82
|
-
(ISO8601::Duration.new('
|
83
|
-
(ISO8601::Duration.new('P1Y1M1DT1H1M1S') + ISO8601::Duration.new('PT10S')).
|
92
|
+
expect((ISO8601::Duration.new('P11Y1M1DT1H1M1S') + ISO8601::Duration.new('P1Y1M1DT1H1M1S')).to_s).to eq('P12Y2M2DT2H2M2S')
|
93
|
+
expect((ISO8601::Duration.new('P1Y1M1DT1H1M1S') + ISO8601::Duration.new('PT10S')).to_s).to eq('P1Y1M1DT1H1M11S')
|
94
|
+
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S') + ISO8601::Duration.new('PT10S')).to be_an_instance_of(ISO8601::Duration)
|
84
95
|
end
|
85
96
|
end
|
86
97
|
|
87
98
|
describe '#-' do
|
88
|
-
it "should raise an ISO8601::Errors::DurationBaseError" do
|
99
|
+
it "should raise an ISO8601::Errors::DurationBaseError when bases mismatch" do
|
89
100
|
expect { ISO8601::Duration.new('PT1H', ISO8601::DateTime.new('2000-01-01')) - ISO8601::Duration.new('PT1H') }.to raise_error(ISO8601::Errors::DurationBaseError)
|
90
101
|
end
|
91
102
|
|
92
103
|
it "should return the result of the substraction" do
|
93
|
-
(ISO8601::Duration.new('P1Y1M1DT1H1M1S') - ISO8601::Duration.new('PT10S')).
|
94
|
-
(ISO8601::Duration.new('P1Y1M1DT1H1M11S') - ISO8601::Duration.new('PT10S')).to_s.
|
95
|
-
(ISO8601::Duration.new('
|
96
|
-
(ISO8601::Duration.new('PT12S') - ISO8601::Duration.new('
|
97
|
-
(ISO8601::Duration.new('
|
98
|
-
(ISO8601::Duration.new('PT1S') - ISO8601::Duration.new('
|
104
|
+
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S') - ISO8601::Duration.new('PT10S')).to be_an_instance_of(ISO8601::Duration)
|
105
|
+
expect((ISO8601::Duration.new('P1Y1M1DT1H1M11S') - ISO8601::Duration.new('PT10S')).to_s).to eq('P1Y1M1DT1H1M1S')
|
106
|
+
expect((ISO8601::Duration.new('P1Y1M1DT1H1M11S') - ISO8601::Duration.new('P1Y1M1DT1H1M11S')).to_s).to eq('PT0S')
|
107
|
+
expect((ISO8601::Duration.new('PT12S') - ISO8601::Duration.new('PT12S')).to_s).to eq('PT0S')
|
108
|
+
expect((ISO8601::Duration.new('PT12S') - ISO8601::Duration.new('PT1S')).to_s).to eq('PT11S')
|
109
|
+
expect((ISO8601::Duration.new('PT1S') - ISO8601::Duration.new('PT12S')).to_s).to eq('-PT11S')
|
110
|
+
expect((ISO8601::Duration.new('PT1S') - ISO8601::Duration.new('-PT12S')).to_s).to eq('PT13S')
|
99
111
|
end
|
100
112
|
end
|
101
113
|
|
102
114
|
describe '#to_seconds' do
|
103
115
|
context 'positive durations' do
|
104
116
|
it "should return the seconds of a P[n]Y duration in a common year" do
|
105
|
-
ISO8601::Duration.new('P2Y', ISO8601::DateTime.new('2010-01-01')).to_seconds.
|
117
|
+
expect(ISO8601::Duration.new('P2Y', ISO8601::DateTime.new('2010-01-01')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2010, 1))
|
106
118
|
end
|
107
119
|
it "should return the seconds of a P[n]Y duration in a leap year" do
|
108
|
-
ISO8601::Duration.new('P2Y', ISO8601::DateTime.new('2000-01-01')).to_seconds.
|
120
|
+
expect(ISO8601::Duration.new('P2Y', ISO8601::DateTime.new('2000-01-01')).to_seconds).to eq(Time.utc(2002, 1) - Time.utc(2000, 1))
|
109
121
|
end
|
110
122
|
it "should return the seconds of a P[n]Y[n]M duration in a common year" do
|
111
|
-
ISO8601::Duration.new('P2Y3M', ISO8601::DateTime.new('2010-01-01')).to_seconds.
|
123
|
+
expect(ISO8601::Duration.new('P2Y3M', ISO8601::DateTime.new('2010-01-01')).to_seconds).to eq(Time.utc(2012, 4) - Time.utc(2010, 1))
|
112
124
|
end
|
113
125
|
it "should return the seconds of a P[n]Y[n]M duration in a leap year" do
|
114
|
-
ISO8601::Duration.new('P2Y3M', ISO8601::DateTime.new('2000-01-01')).to_seconds.
|
126
|
+
expect(ISO8601::Duration.new('P2Y3M', ISO8601::DateTime.new('2000-01-01')).to_seconds).to eq(Time.utc(2002, 4) - Time.utc(2000, 1))
|
115
127
|
end
|
116
128
|
it "should return the seconds of a P[n]M duration in a common year" do
|
117
|
-
ISO8601::Duration.new('P1M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
118
|
-
ISO8601::Duration.new('P2M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
119
|
-
ISO8601::Duration.new('P19M', ISO8601::DateTime.new('2012-05-01')).to_seconds.
|
120
|
-
ISO8601::Duration.new('P14M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
129
|
+
expect(ISO8601::Duration.new('P1M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 2) - Time.utc(2012, 1))
|
130
|
+
expect(ISO8601::Duration.new('P2M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 3) - Time.utc(2012, 1))
|
131
|
+
expect(ISO8601::Duration.new('P19M', ISO8601::DateTime.new('2012-05-01')).to_seconds).to eq(Time.utc(2014, 12) - Time.utc(2012, 5))
|
132
|
+
expect(ISO8601::Duration.new('P14M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2013, 3) - Time.utc(2012, 1))
|
121
133
|
end
|
122
134
|
it "should return the seconds of a P[n]M duration in a leap year" do
|
123
|
-
ISO8601::Duration.new('P1M', ISO8601::DateTime.new('2000-01-01')).to_seconds.
|
124
|
-
ISO8601::Duration.new('P2M', ISO8601::DateTime.new('2000-01-01')).to_seconds.
|
125
|
-
ISO8601::Duration.new('P14M', ISO8601::DateTime.new('2000-01-01')).to_seconds.
|
135
|
+
expect(ISO8601::Duration.new('P1M', ISO8601::DateTime.new('2000-01-01')).to_seconds).to eq(Time.utc(2000, 2) - Time.utc(2000, 1))
|
136
|
+
expect(ISO8601::Duration.new('P2M', ISO8601::DateTime.new('2000-01-01')).to_seconds).to eq(Time.utc(2000, 3) - Time.utc(2000, 1))
|
137
|
+
expect(ISO8601::Duration.new('P14M', ISO8601::DateTime.new('2000-01-01')).to_seconds).to eq(Time.utc(2001, 3) - Time.utc(2000, 1))
|
126
138
|
end
|
127
139
|
it "should return the seconds of a P[n]Y[n]M[n]D duration" do
|
128
|
-
ISO8601::Duration.new('P2Y11D', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
129
|
-
ISO8601::Duration.new('P1Y1M1D', ISO8601::DateTime.new('2010-05-01')).to_seconds.
|
140
|
+
expect(ISO8601::Duration.new('P2Y11D', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2014, 1, 12) - Time.utc(2012, 1))
|
141
|
+
expect(ISO8601::Duration.new('P1Y1M1D', ISO8601::DateTime.new('2010-05-01')).to_seconds).to eq(Time.utc(2011, 6, 2) - Time.utc(2010, 5))
|
130
142
|
end
|
131
143
|
it "should return the seconds of a P[n]D duration" do
|
132
|
-
ISO8601::Duration.new('P1D', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
133
|
-
ISO8601::Duration.new('P11D', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
134
|
-
ISO8601::Duration.new('P3M11D', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
144
|
+
expect(ISO8601::Duration.new('P1D', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 1, 2) - Time.utc(2012, 1, 1))
|
145
|
+
expect(ISO8601::Duration.new('P11D', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 1, 12) - Time.utc(2012, 1, 1))
|
146
|
+
expect(ISO8601::Duration.new('P3M11D', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 4, 12) - Time.utc(2012, 1))
|
135
147
|
end
|
136
148
|
it "should return the seconds of a P[n]W duration" do
|
137
|
-
ISO8601::Duration.new('P2W', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
138
|
-
ISO8601::Duration.new('P2W', ISO8601::DateTime.new('2012-02-01')).to_seconds.
|
149
|
+
expect(ISO8601::Duration.new('P2W', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 1, 15) - Time.utc(2012, 1))
|
150
|
+
expect(ISO8601::Duration.new('P2W', ISO8601::DateTime.new('2012-02-01')).to_seconds).to eq(Time.utc(2012, 2, 15) - Time.utc(2012, 2))
|
139
151
|
end
|
140
152
|
|
141
153
|
it "should return the seconds of a PT[n]H duration" do
|
142
|
-
ISO8601::Duration.new('PT5H', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
143
|
-
ISO8601::Duration.new('P1YT5H', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
154
|
+
expect(ISO8601::Duration.new('PT5H', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 1, 1, 5) - Time.utc(2012, 1))
|
155
|
+
expect(ISO8601::Duration.new('P1YT5H', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2013, 1, 1, 5) - Time.utc(2012, 1))
|
144
156
|
end
|
145
157
|
|
146
158
|
it "should return the seconds of a PT[n]H[n]M duration" do
|
147
|
-
ISO8601::Duration.new('PT5M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
148
|
-
ISO8601::Duration.new('PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
149
|
-
ISO8601::Duration.new('PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
159
|
+
expect(ISO8601::Duration.new('PT5M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 1, 1, 0, 5) - Time.utc(2012, 1))
|
160
|
+
expect(ISO8601::Duration.new('PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 1, 1, 1, 5) - Time.utc(2012, 1))
|
161
|
+
expect(ISO8601::Duration.new('PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(ISO8601::Duration.new('PT65M', ISO8601::DateTime.new('2012-01-01')).to_seconds)
|
150
162
|
end
|
151
163
|
|
152
164
|
it "should return the seconds of a PT[n]H[n]M duration" do
|
153
|
-
ISO8601::Duration.new('PT10S', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
154
|
-
ISO8601::Duration.new('PT10.4S', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
165
|
+
expect(ISO8601::Duration.new('PT10S', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2012, 1, 1, 0, 0, 10) - Time.utc(2012, 1))
|
166
|
+
expect(ISO8601::Duration.new('PT10.4S', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(10.4)
|
155
167
|
end
|
156
168
|
end
|
157
169
|
|
158
170
|
context 'negative durations' do
|
159
171
|
it "should return the seconds of a -P[n]Y duration" do
|
160
|
-
ISO8601::Duration.new('-P2Y', ISO8601::DateTime.new('2010-01-01')).to_seconds.
|
172
|
+
expect(ISO8601::Duration.new('-P2Y', ISO8601::DateTime.new('2010-01-01')).to_seconds).to eq(Time.utc(2008, 1) - Time.utc(2010, 1))
|
161
173
|
end
|
162
174
|
it "should return the seconds of a -P[n]Y duration in a leap year" do
|
163
|
-
ISO8601::Duration.new('-P2Y', ISO8601::DateTime.new('2001-01-01')).to_seconds.
|
175
|
+
expect(ISO8601::Duration.new('-P2Y', ISO8601::DateTime.new('2001-01-01')).to_seconds).to eq(Time.utc(1999, 1) - Time.utc(2001, 1))
|
164
176
|
end
|
165
177
|
it "should return the seconds of a -P[n]Y[n]M duration in a common year" do
|
166
|
-
ISO8601::Duration.new('-P2Y3M', ISO8601::DateTime.new('2010-01-01')).to_seconds.
|
178
|
+
expect(ISO8601::Duration.new('-P2Y3M', ISO8601::DateTime.new('2010-01-01')).to_seconds).to eq(Time.utc(2007, 10) - Time.utc(2010, 1))
|
167
179
|
end
|
168
180
|
it "should return the seconds of a -P[n]Y[n]M duration in a leap year" do
|
169
|
-
ISO8601::Duration.new('-P2Y3M', ISO8601::DateTime.new('2001-01-01')).to_seconds.
|
181
|
+
expect(ISO8601::Duration.new('-P2Y3M', ISO8601::DateTime.new('2001-01-01')).to_seconds).to eq(Time.utc(1998, 10) - Time.utc(2001, 1))
|
170
182
|
end
|
171
183
|
it "should return the seconds of a -P[n]M duration in a common year" do
|
172
|
-
ISO8601::Duration.new('-P1M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
173
|
-
ISO8601::Duration.new('-P1M', ISO8601::DateTime.new('2012-02-01')).to_seconds.
|
174
|
-
ISO8601::Duration.new('-P2M', ISO8601::DateTime.new('2012-03-01')).to_seconds.
|
175
|
-
ISO8601::Duration.new('-P36M', ISO8601::DateTime.new('2013-03-01')).to_seconds.
|
176
|
-
ISO8601::Duration.new('-P39M', ISO8601::DateTime.new('2013-03-01')).to_seconds.
|
177
|
-
ISO8601::Duration.new('-P156M', ISO8601::DateTime.new('2013-03-01')).to_seconds.
|
184
|
+
expect(ISO8601::Duration.new('-P1M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2011, 12) - Time.utc(2012, 1))
|
185
|
+
expect(ISO8601::Duration.new('-P1M', ISO8601::DateTime.new('2012-02-01')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 2))
|
186
|
+
expect(ISO8601::Duration.new('-P2M', ISO8601::DateTime.new('2012-03-01')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 3))
|
187
|
+
expect(ISO8601::Duration.new('-P36M', ISO8601::DateTime.new('2013-03-01')).to_seconds).to eq(Time.utc(2010, 3) - Time.utc(2013, 3))
|
188
|
+
expect(ISO8601::Duration.new('-P39M', ISO8601::DateTime.new('2013-03-01')).to_seconds).to eq(Time.utc(2009, 12) - Time.utc(2013, 3))
|
189
|
+
expect(ISO8601::Duration.new('-P156M', ISO8601::DateTime.new('2013-03-01')).to_seconds).to eq(Time.utc(2000, 3) - Time.utc(2013, 3))
|
178
190
|
end
|
179
191
|
it "should return the seconds of a -P[n]M duration in a leap year" do
|
180
|
-
ISO8601::Duration.new('-P1M', ISO8601::DateTime.new('2000-02-01')).to_seconds.
|
181
|
-
ISO8601::Duration.new('-P2M', ISO8601::DateTime.new('2000-03-01')).to_seconds.
|
182
|
-
ISO8601::Duration.new('-P14M', ISO8601::DateTime.new('2001-03-01')).to_seconds.
|
192
|
+
expect(ISO8601::Duration.new('-P1M', ISO8601::DateTime.new('2000-02-01')).to_seconds).to eq(Time.utc(2000, 1) - Time.utc(2000, 2))
|
193
|
+
expect(ISO8601::Duration.new('-P2M', ISO8601::DateTime.new('2000-03-01')).to_seconds).to eq(Time.utc(2000, 1) - Time.utc(2000, 3))
|
194
|
+
expect(ISO8601::Duration.new('-P14M', ISO8601::DateTime.new('2001-03-01')).to_seconds).to eq(Time.utc(2000, 1) - Time.utc(2001, 3))
|
183
195
|
end
|
184
196
|
it "should return the seconds of a -P[n]Y[n]M[n]D duration" do
|
185
|
-
ISO8601::Duration.new('-P2Y11D', ISO8601::DateTime.new('2014-01-12')).to_seconds.
|
186
|
-
ISO8601::Duration.new('-P1Y1M1D', ISO8601::DateTime.new('2010-05-01')).to_seconds.
|
197
|
+
expect(ISO8601::Duration.new('-P2Y11D', ISO8601::DateTime.new('2014-01-12')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2014, 1, 12))
|
198
|
+
expect(ISO8601::Duration.new('-P1Y1M1D', ISO8601::DateTime.new('2010-05-01')).to_seconds).to eq(Time.utc(2009, 3, 31) - Time.utc(2010, 5))
|
187
199
|
end
|
188
200
|
it "should return the seconds of a -P[n]D duration" do
|
189
|
-
ISO8601::Duration.new('-P1D', ISO8601::DateTime.new('2012-01-02')).to_seconds.
|
190
|
-
ISO8601::Duration.new('-P11D', ISO8601::DateTime.new('2012-01-12')).to_seconds.
|
191
|
-
ISO8601::Duration.new('-P3M11D', ISO8601::DateTime.new('2012-04-12')).to_seconds.
|
201
|
+
expect(ISO8601::Duration.new('-P1D', ISO8601::DateTime.new('2012-01-02')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 2))
|
202
|
+
expect(ISO8601::Duration.new('-P11D', ISO8601::DateTime.new('2012-01-12')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 12))
|
203
|
+
expect(ISO8601::Duration.new('-P3M11D', ISO8601::DateTime.new('2012-04-12')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 4, 12))
|
192
204
|
end
|
193
205
|
it "should return the seconds of a -P[n]W duration" do
|
194
|
-
ISO8601::Duration.new('-P2W', ISO8601::DateTime.new('2012-01-15')).to_seconds.
|
195
|
-
ISO8601::Duration.new('-P2W', ISO8601::DateTime.new('2012-02-01')).to_seconds.
|
206
|
+
expect(ISO8601::Duration.new('-P2W', ISO8601::DateTime.new('2012-01-15')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 15))
|
207
|
+
expect(ISO8601::Duration.new('-P2W', ISO8601::DateTime.new('2012-02-01')).to_seconds).to eq(Time.utc(2012, 2) - Time.utc(2012, 2, 15))
|
196
208
|
end
|
197
209
|
|
198
210
|
it "should return the seconds of a -PT[n]H duration" do
|
199
|
-
ISO8601::Duration.new('-PT5H', ISO8601::DateTime.new('2012-01-01T05')).to_seconds.
|
200
|
-
ISO8601::Duration.new('-P1YT5H', ISO8601::DateTime.new('2013-01-01T05')).to_seconds.
|
211
|
+
expect(ISO8601::Duration.new('-PT5H', ISO8601::DateTime.new('2012-01-01T05')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 1, 5))
|
212
|
+
expect(ISO8601::Duration.new('-P1YT5H', ISO8601::DateTime.new('2013-01-01T05')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2013, 1, 1, 5))
|
201
213
|
end
|
202
214
|
|
203
215
|
it "should return the seconds of a -PT[n]H[n]M duration" do
|
204
|
-
ISO8601::Duration.new('-PT5M', ISO8601::DateTime.new('2012-01-01T00:05')).to_seconds.
|
205
|
-
ISO8601::Duration.new('-PT1H5M', ISO8601::DateTime.new('2012-01-01T01:05')).to_seconds.
|
206
|
-
ISO8601::Duration.new('-PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
216
|
+
expect(ISO8601::Duration.new('-PT5M', ISO8601::DateTime.new('2012-01-01T00:05')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 1, 0, 5))
|
217
|
+
expect(ISO8601::Duration.new('-PT1H5M', ISO8601::DateTime.new('2012-01-01T01:05')).to_seconds).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 1, 1, 5))
|
218
|
+
expect(ISO8601::Duration.new('-PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(ISO8601::Duration.new('-PT65M', ISO8601::DateTime.new('2012-01-01')).to_seconds)
|
207
219
|
end
|
208
220
|
|
209
221
|
it "should return the seconds of a -PT[n]H[n]M duration" do
|
210
|
-
ISO8601::Duration.new('-PT10S', ISO8601::DateTime.new('2012-01-01T00:00:00')).to_seconds.
|
211
|
-
ISO8601::Duration.new('-PT10.4S', ISO8601::DateTime.new('2012-01-01')).to_seconds.
|
222
|
+
expect(ISO8601::Duration.new('-PT10S', ISO8601::DateTime.new('2012-01-01T00:00:00')).to_seconds).to eq(Time.utc(2011, 12, 31, 23, 59, 50) - Time.utc(2012, 1))
|
223
|
+
expect(ISO8601::Duration.new('-PT10.4S', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(-10.4)
|
212
224
|
end
|
213
225
|
end
|
214
226
|
end
|
215
227
|
|
216
|
-
describe '
|
217
|
-
|
218
|
-
|
228
|
+
describe '#abs' do
|
229
|
+
let(:positive) { ISO8601::Duration.new('PT1H') }
|
230
|
+
let(:negative) { ISO8601::Duration.new('-PT1H') }
|
231
|
+
|
232
|
+
it "should return a kind of duration" do
|
233
|
+
expect(negative.abs).to be_instance_of(ISO8601::Duration)
|
219
234
|
end
|
220
|
-
it "should return
|
221
|
-
|
235
|
+
it "should return the absolute value of the duration" do
|
236
|
+
expect(negative.abs).to eq(positive)
|
222
237
|
end
|
223
238
|
end
|
224
239
|
|
225
|
-
describe '
|
226
|
-
it "should
|
227
|
-
ISO8601::Duration.new('
|
240
|
+
describe '#==' do
|
241
|
+
it "should equal by computed value" do
|
242
|
+
expect(ISO8601::Duration.new('PT1H') == ISO8601::Duration.new('PT1H')).to be_truthy
|
243
|
+
expect(ISO8601::Duration.new('PT1H') == ISO8601::Duration.new('PT60M')).to be_truthy
|
228
244
|
end
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
245
|
+
end
|
246
|
+
|
247
|
+
describe '#eql?' do
|
248
|
+
it "should respond to #eql?" do
|
249
|
+
subject = ISO8601::Duration.new('PT1H')
|
250
|
+
expect(subject).to respond_to(:eql?)
|
233
251
|
end
|
252
|
+
it "should equal by hash identity" do
|
253
|
+
expect(ISO8601::Duration.new('PT1H').eql? ISO8601::Duration.new('PT1H')).to be_truthy
|
254
|
+
expect(ISO8601::Duration.new('PT1H').eql? ISO8601::Duration.new('PT60M')).to be_falsy
|
255
|
+
end
|
256
|
+
|
234
257
|
end
|
235
258
|
|
236
259
|
describe '#hash' do
|
237
|
-
it "should
|
260
|
+
it "should respond to #hash" do
|
238
261
|
subject = ISO8601::Duration.new('PT1H')
|
239
262
|
|
240
263
|
expect(subject).to respond_to(:hash)
|
241
264
|
end
|
265
|
+
it "should build hash identity by value" do
|
266
|
+
subject = ISO8601::Duration.new('PT1H')
|
267
|
+
contrast = ISO8601::Duration.new('PT1H')
|
268
|
+
|
269
|
+
expect(subject.hash == contrast.hash).to be_truthy
|
270
|
+
end
|
242
271
|
end
|
243
272
|
end
|
data/spec/iso8601/time_spec.rb
CHANGED
@@ -32,52 +32,51 @@ describe ISO8601::Time do
|
|
32
32
|
context 'reduced patterns' do
|
33
33
|
it "should parse correctly reduced times" do
|
34
34
|
reduced_time = ISO8601::Time.new('T101112Z')
|
35
|
-
reduced_time.hour.
|
36
|
-
reduced_time.minute.
|
37
|
-
reduced_time.second.
|
35
|
+
expect(reduced_time.hour).to eq(10)
|
36
|
+
expect(reduced_time.minute).to eq(11)
|
37
|
+
expect(reduced_time.second).to eq(12)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should return each atomic value" do
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
t = ISO8601::Time.new('T12:02:01+04:00', ::Date.parse('2010-05-09'))
|
43
|
+
expect(t.hour).to eq(12)
|
44
|
+
expect(t.minute).to eq(2)
|
45
|
+
expect(t.second).to eq(1)
|
46
|
+
expect(t.zone).to eq('+04:00')
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should respond to delegated casting methods" do
|
50
|
-
|
51
|
-
dt.should respond_to(:to_s, :to_time, :to_date, :to_datetime)
|
50
|
+
expect(ISO8601::Time.new('T10:09:08Z')).to respond_to(:to_s, :to_time, :to_date, :to_datetime)
|
52
51
|
end
|
53
52
|
|
54
53
|
describe '#+' do
|
55
54
|
it "should return the result of the addition" do
|
56
|
-
(ISO8601::Time.new('T20:20:20+02:00') + 10).to_s.
|
57
|
-
(ISO8601::Time.new('T20:20:20.11+02:00') + 10).to_s.
|
55
|
+
expect((ISO8601::Time.new('T20:20:20+02:00') + 10).to_s).to eq('T20:20:30+02:00')
|
56
|
+
expect((ISO8601::Time.new('T20:20:20.11+02:00') + 10).to_s).to eq('T20:20:30.11+02:00')
|
58
57
|
end
|
59
58
|
end
|
60
59
|
|
61
60
|
describe '#-' do
|
62
61
|
it "should return the result of the substraction" do
|
63
|
-
(ISO8601::Time.new('T20:20:20+01:00') - 10).to_s.
|
64
|
-
(ISO8601::Time.new('T20:20:20.11+02:00') - 10).to_s.
|
62
|
+
expect((ISO8601::Time.new('T20:20:20+01:00') - 10).to_s).to eq('T20:20:10+01:00')
|
63
|
+
expect((ISO8601::Time.new('T20:20:20.11+02:00') - 10).to_s).to eq('T20:20:10.11+02:00')
|
65
64
|
end
|
66
65
|
end
|
67
66
|
|
68
67
|
describe '#to_a' do
|
69
68
|
it "should return an array of atoms" do
|
70
|
-
ISO8601::Time.new('T19:29:39Z').to_a.
|
69
|
+
expect(ISO8601::Time.new('T19:29:39Z').to_a).to eq([19, 29, 39, '+00:00'])
|
71
70
|
end
|
72
71
|
end
|
73
72
|
|
74
73
|
describe '#atoms' do
|
75
74
|
it "should return an array of atoms" do
|
76
|
-
ISO8601::Time.new('T19:29:39+04:00').atoms.
|
77
|
-
ISO8601::Time.new('T19:29:39Z').atoms.
|
78
|
-
ISO8601::Time.new('T19:29:39').atoms.
|
79
|
-
ISO8601::Time.new('T19:29').atoms.
|
80
|
-
ISO8601::Time.new('T19').atoms.
|
75
|
+
expect(ISO8601::Time.new('T19:29:39+04:00').atoms).to eq([19, 29, 39, '+04:00'])
|
76
|
+
expect(ISO8601::Time.new('T19:29:39Z').atoms).to eq([19, 29, 39, 'Z'])
|
77
|
+
expect(ISO8601::Time.new('T19:29:39').atoms).to eq([19, 29, 39])
|
78
|
+
expect(ISO8601::Time.new('T19:29').atoms).to eq([19, 29])
|
79
|
+
expect(ISO8601::Time.new('T19').atoms).to eq([19])
|
81
80
|
end
|
82
81
|
end
|
83
82
|
|
@@ -87,6 +86,11 @@ describe ISO8601::Time do
|
|
87
86
|
|
88
87
|
expect(subject).to respond_to(:hash)
|
89
88
|
end
|
90
|
-
|
89
|
+
it "should return the same hash" do
|
90
|
+
subject = ISO8601::Time.new('T20:11:10Z')
|
91
|
+
contrast = ISO8601::Time.new('T20:11:10Z')
|
91
92
|
|
93
|
+
expect(subject.hash).to eq(contrast.hash)
|
94
|
+
end
|
95
|
+
end
|
92
96
|
end
|