iso8601 0.2 → 0.4.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.
- data/.gitignore +13 -2
- data/.rspec +2 -0
- data/Gemfile +6 -2
- data/LICENSE +22 -503
- data/README.md +26 -5
- data/Rakefile +2 -1
- data/iso8601.gemspec +24 -20
- data/lib/iso8601.rb +8 -6
- data/lib/iso8601/atoms.rb +65 -32
- data/lib/iso8601/dateTime.rb +45 -22
- data/lib/iso8601/duration.rb +113 -57
- data/lib/iso8601/errors.rb +3 -2
- data/spec/iso8601/atoms_spec.rb +176 -0
- data/spec/iso8601/dateTime_spec.rb +120 -0
- data/spec/iso8601/duration_spec.rb +163 -0
- data/spec/spec_helper.rb +4 -0
- metadata +62 -54
- data/test/iso8601/test_atoms.rb +0 -100
- data/test/iso8601/test_dateTime.rb +0 -72
- data/test/iso8601/test_duration.rb +0 -95
- data/test/test_iso8601.rb +0 -4
data/lib/iso8601/errors.rb
CHANGED
@@ -0,0 +1,176 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe ISO8601::Atom do
|
6
|
+
it "should raise a TypeError when receives anything but a Numeric value" do
|
7
|
+
expect { ISO8601::Atom.new('1') }.to raise_error(TypeError)
|
8
|
+
expect { ISO8601::Atom.new(true) }.to raise_error(TypeError)
|
9
|
+
expect { ISO8601::Atom.new(-1.1) }.to_not raise_error(TypeError)
|
10
|
+
expect { ISO8601::Atom.new(-1) }.to_not raise_error(TypeError)
|
11
|
+
expect { ISO8601::Atom.new(0) }.to_not raise_error(TypeError)
|
12
|
+
expect { ISO8601::Atom.new(1) }.to_not raise_error(TypeError)
|
13
|
+
expect { ISO8601::Atom.new(1.1) }.to_not raise_error(TypeError)
|
14
|
+
end
|
15
|
+
it "should raise a TypeError when receives anything but a ISO8601::DateTime instance or nil" do
|
16
|
+
expect { ISO8601::Atom.new(1, ISO8601::DateTime.new('2012-07-07')) }.to_not raise_error(TypeError)
|
17
|
+
expect { ISO8601::Atom.new(1, nil) }.to_not raise_error(TypeError)
|
18
|
+
expect { ISO8601::Atom.new(1, true) }.to raise_error(TypeError)
|
19
|
+
expect { ISO8601::Atom.new(1, 'foo') }.to raise_error(TypeError)
|
20
|
+
expect { ISO8601::Atom.new(1, 10) }.to raise_error(TypeError)
|
21
|
+
end
|
22
|
+
it "should create a new atom" do
|
23
|
+
ISO8601::Atom.new(-1).should be_an_instance_of(ISO8601::Atom)
|
24
|
+
end
|
25
|
+
describe '#to_i' do
|
26
|
+
it "should return an integer" do
|
27
|
+
ISO8601::Atom.new(1).to_i.should be_a_kind_of(Integer)
|
28
|
+
ISO8601::Atom.new(1.0).to_i.should be_a_kind_of(Integer)
|
29
|
+
end
|
30
|
+
it "should return the integer part of the given number" do
|
31
|
+
n = 1.0
|
32
|
+
ISO8601::Atom.new(n).to_i.should == n.to_i
|
33
|
+
end
|
34
|
+
end
|
35
|
+
describe '#factor' do
|
36
|
+
it "should raise a NotImplementedError" do
|
37
|
+
expect { ISO8601::Atom.new(1).factor }.to raise_error(NotImplementedError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe ISO8601::Years do
|
43
|
+
describe '#factor' do
|
44
|
+
it "should return the Year factor" do
|
45
|
+
expect { ISO8601::Years.new(1).factor }.to_not raise_error(NotImplementedError)
|
46
|
+
ISO8601::Years.new(2).factor.should == 31536000
|
47
|
+
ISO8601::Years.new(1).factor.should == 31536000
|
48
|
+
end
|
49
|
+
it "should return the Year factor for a common year" do
|
50
|
+
ISO8601::Years.new(1, ISO8601::DateTime.new("2010-01-01")).factor.should == 31536000
|
51
|
+
end
|
52
|
+
it "should return the Year factor for a leap year" do
|
53
|
+
ISO8601::Years.new(1, ISO8601::DateTime.new("2000-01-01")).factor.should == 31622400
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#to_seconds' do
|
58
|
+
it "should return the amount of seconds" do
|
59
|
+
ISO8601::Years.new(2).to_seconds.should == 63072000
|
60
|
+
ISO8601::Years.new(-2).to_seconds.should == -63072000
|
61
|
+
end
|
62
|
+
it "should return the amount of seconds for a common year" do
|
63
|
+
base = ISO8601::DateTime.new('2010-01-01')
|
64
|
+
ISO8601::Years.new(2, base).to_seconds.should == 63072000
|
65
|
+
ISO8601::Years.new(12, base).to_seconds.should == 378691200
|
66
|
+
end
|
67
|
+
it "should return the amount of seconds for a leap year" do
|
68
|
+
base = ISO8601::DateTime.new('2000-01-01')
|
69
|
+
ISO8601::Years.new(2, base).to_seconds.should == 63158400
|
70
|
+
ISO8601::Years.new(15, base).to_seconds.should == 473385600
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe ISO8601::Months do
|
76
|
+
describe '#factor' do
|
77
|
+
it "should return the Month factor" do
|
78
|
+
expect { ISO8601::Months.new(1).factor }.to_not raise_error(NotImplementedError)
|
79
|
+
ISO8601::Months.new(2).factor.should == 2628000
|
80
|
+
end
|
81
|
+
it "should return the Month factor for a common year" do
|
82
|
+
ISO8601::Months.new(1, ISO8601::DateTime.new('2010-01-01')).factor.should == 2678400
|
83
|
+
end
|
84
|
+
it "should return the Month factor for a leap year" do
|
85
|
+
ISO8601::Months.new(1, ISO8601::DateTime.new('2000-01-01')).factor.should == 2678400
|
86
|
+
end
|
87
|
+
it "should return the Month factor based on february for a common year" do
|
88
|
+
ISO8601::Months.new(1, ISO8601::DateTime.new('2010-02-01')).factor.should == 2419200
|
89
|
+
end
|
90
|
+
it "should return the Month factor based on february for a leap year" do
|
91
|
+
ISO8601::Months.new(1, ISO8601::DateTime.new('2000-02-01')).factor.should == 2505600
|
92
|
+
end
|
93
|
+
end
|
94
|
+
describe '#to_seconds' do
|
95
|
+
it "should return the amount of seconds" do
|
96
|
+
ISO8601::Months.new(2).to_seconds.should == 5256000
|
97
|
+
end
|
98
|
+
it "should return the amount of seconds for a common year" do
|
99
|
+
ISO8601::Months.new(2, ISO8601::DateTime.new('2010-01-01')).to_seconds.should == 5097600
|
100
|
+
end
|
101
|
+
it "should return the amount of seconds for a leap year" do
|
102
|
+
ISO8601::Months.new(2, ISO8601::DateTime.new('2000-01-01')).to_seconds.should == 5184000
|
103
|
+
end
|
104
|
+
it "should return the amount of seconds based on februrary for a common year" do
|
105
|
+
ISO8601::Months.new(2, ISO8601::DateTime.new('2010-02-01')).to_seconds.should == 5097600
|
106
|
+
end
|
107
|
+
it "should return the amount of seconds based on february for a leap year" do
|
108
|
+
ISO8601::Months.new(2, ISO8601::DateTime.new('2000-02-01')).to_seconds.should == 5184000
|
109
|
+
ISO8601::Months.new(12, ISO8601::DateTime.new('2000-02-01')).to_seconds.should == 31622400
|
110
|
+
ISO8601::Months.new(12, ISO8601::DateTime.new('2000-02-01')).to_seconds.should == ISO8601::Years.new(1, ISO8601::DateTime.new("2000-02-01")).to_seconds
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe ISO8601::Weeks do
|
116
|
+
describe '#factor' do
|
117
|
+
it "should return the Week factor" do
|
118
|
+
ISO8601::Weeks.new(2).factor.should == 604800
|
119
|
+
end
|
120
|
+
end
|
121
|
+
describe '#to_seconds' do
|
122
|
+
it "should return the amount of seconds" do
|
123
|
+
ISO8601::Weeks.new(2).to_seconds.should == 1209600
|
124
|
+
ISO8601::Weeks.new(-2).to_seconds.should == -1209600
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe ISO8601::Days do
|
130
|
+
describe '#factor' do
|
131
|
+
it "should return the Day factor" do
|
132
|
+
ISO8601::Days.new(2).factor.should == 86400
|
133
|
+
end
|
134
|
+
it "should return the amount of seconds" do
|
135
|
+
ISO8601::Days.new(2).to_seconds.should == 172800
|
136
|
+
ISO8601::Days.new(-2).to_seconds.should == -172800
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe ISO8601::Hours do
|
142
|
+
describe '#factor' do
|
143
|
+
it "should return the Hour factor" do
|
144
|
+
ISO8601::Hours.new(2).factor.should == 3600
|
145
|
+
end
|
146
|
+
it "should return the amount of seconds" do
|
147
|
+
ISO8601::Hours.new(2).to_seconds.should == 7200
|
148
|
+
ISO8601::Hours.new(-2).to_seconds.should == -7200
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe ISO8601::Minutes do
|
154
|
+
describe '#factor' do
|
155
|
+
it "should return the Minute factor" do
|
156
|
+
ISO8601::Minutes.new(2).factor.should == 60
|
157
|
+
end
|
158
|
+
it "should return the amount of seconds" do
|
159
|
+
ISO8601::Minutes.new(2).to_seconds.should == 120
|
160
|
+
ISO8601::Minutes.new(-2).to_seconds.should == -120
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe ISO8601::Seconds do
|
166
|
+
describe '#factor' do
|
167
|
+
it "should return the Second factor" do
|
168
|
+
ISO8601::Seconds.new(2).factor.should == 1
|
169
|
+
end
|
170
|
+
it "should return the amount of seconds" do
|
171
|
+
ISO8601::Seconds.new(2).to_seconds.should == 2
|
172
|
+
ISO8601::Seconds.new(-2).to_seconds.should == -2
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe ISO8601::DateTime do
|
6
|
+
it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
|
7
|
+
expect { ISO8601::DateTime.new('2') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
8
|
+
expect { ISO8601::DateTime.new('201') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
9
|
+
expect { ISO8601::DateTime.new('2010-') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
10
|
+
expect { ISO8601::DateTime.new('2010-') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
11
|
+
expect { ISO8601::DateTime.new('20-05') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
12
|
+
expect { ISO8601::DateTime.new('2010-0') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
13
|
+
expect { ISO8601::DateTime.new('2010-0-09') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
14
|
+
expect { ISO8601::DateTime.new('2010-1-09') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
15
|
+
expect { ISO8601::DateTime.new('201001-09') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
16
|
+
expect { ISO8601::DateTime.new('201-0109') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
17
|
+
expect { ISO8601::DateTime.new('2010-05-09T103012+0400') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
18
|
+
expect { ISO8601::DateTime.new('20100509T10:30:12+04:00') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
19
|
+
end
|
20
|
+
it "should raise a RangeError for a correct pattern but an invalid date" do
|
21
|
+
expect { ISO8601::DateTime.new('2010-01-32') }.to raise_error(RangeError)
|
22
|
+
expect { ISO8601::DateTime.new('2010-02-30') }.to raise_error(RangeError)
|
23
|
+
expect { ISO8601::DateTime.new('2010-13-30') }.to raise_error(RangeError)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should parse the reduced precision year (just the century)" do
|
27
|
+
expect { ISO8601::DateTime.new('20') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
28
|
+
ISO8601::DateTime.new('20').year.should == 2000
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should parse any allowed pattern" do
|
32
|
+
expect { ISO8601::DateTime.new('2010') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
33
|
+
expect { ISO8601::DateTime.new('2010-05') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
34
|
+
expect { ISO8601::DateTime.new('2010-05-09') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
35
|
+
expect { ISO8601::DateTime.new('2010-05-09T10') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
36
|
+
expect { ISO8601::DateTime.new('2010-05-09T10:30') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
37
|
+
expect { ISO8601::DateTime.new('2010-05-09T10:30:12') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
38
|
+
expect { ISO8601::DateTime.new('2010-05-09T10:30:12Z') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
39
|
+
expect { ISO8601::DateTime.new('2010-05-09T10:30:12+04') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
40
|
+
expect { ISO8601::DateTime.new('2010-05-09T10:30:12+04:00') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
41
|
+
expect { ISO8601::DateTime.new('2010-05-09T10:30:12-04:00') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
42
|
+
end
|
43
|
+
it "should parse correctly any allowed reduced pattern" do
|
44
|
+
expect { ISO8601::DateTime.new('20') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
45
|
+
#expect { ISO8601::DateTime.new('201005') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
46
|
+
#ISO8601::DateTime.new('201005').year.should == 2000
|
47
|
+
#ISO8601::DateTime.new('201005').month.should == 10
|
48
|
+
#ISO8601::DateTime.new('201005').day.should == 5
|
49
|
+
|
50
|
+
expect { ISO8601::DateTime.new('20100509') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
51
|
+
ISO8601::DateTime.new('20100509').year.should == 2010
|
52
|
+
ISO8601::DateTime.new('20100509').month.should == 5
|
53
|
+
ISO8601::DateTime.new('20100509').day.should == 9
|
54
|
+
|
55
|
+
expect { ISO8601::DateTime.new('20100509T103012') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
56
|
+
expect { ISO8601::DateTime.new('20100509T103012Z') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
57
|
+
expect { ISO8601::DateTime.new('20100509T103012+04') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
58
|
+
expect { ISO8601::DateTime.new('20100509T103012+0400') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
59
|
+
expect { ISO8601::DateTime.new('20100509T103012-0400') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return each atomic value" do
|
63
|
+
dt = ISO8601::DateTime.new('2010-05-09T12:02:01+04:00')
|
64
|
+
dt.century.should == 20
|
65
|
+
dt.year.should == 2010
|
66
|
+
dt.month.should == 5
|
67
|
+
dt.day.should == 9
|
68
|
+
dt.hour.should == 12
|
69
|
+
dt.minute.should == 2
|
70
|
+
dt.second.should == 1
|
71
|
+
dt.timezone[:full].should == '+04:00'
|
72
|
+
dt.timezone[:sign].should == '+'
|
73
|
+
dt.timezone[:hour].should == 4
|
74
|
+
dt.timezone[:minute].should == 0
|
75
|
+
dt = ISO8601::DateTime.new('2010-05-09T10Z')
|
76
|
+
dt.century.should == 20
|
77
|
+
dt.year.should == 2010
|
78
|
+
dt.month.should == 5
|
79
|
+
dt.day.should == 9
|
80
|
+
dt.hour.should == 10
|
81
|
+
dt.minute.should == nil
|
82
|
+
dt.second.should == nil
|
83
|
+
dt.timezone[:full].should == 0
|
84
|
+
dt.timezone[:sign].should == nil
|
85
|
+
dt.timezone[:hour].should == 0
|
86
|
+
dt.timezone[:minute].should == 0
|
87
|
+
end
|
88
|
+
describe '#to_s' do
|
89
|
+
it "should return the string representation" do
|
90
|
+
ISO8601::DateTime.new('2010-05-09').to_s.should == '2010-05-09'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
describe '#to_time' do
|
94
|
+
it "should return a Time instance" do
|
95
|
+
ISO8601::DateTime.new('20').to_time.should be_an_instance_of(Time)
|
96
|
+
ISO8601::DateTime.new('2010').to_time.should be_an_instance_of(Time)
|
97
|
+
ISO8601::DateTime.new('2010-05').to_time.should be_an_instance_of(Time)
|
98
|
+
ISO8601::DateTime.new('2010-05-09').to_time.should be_an_instance_of(Time)
|
99
|
+
ISO8601::DateTime.new('2010-05-09T12').to_time.should be_an_instance_of(Time)
|
100
|
+
ISO8601::DateTime.new('2010-05-09T12:02').to_time.should be_an_instance_of(Time)
|
101
|
+
ISO8601::DateTime.new('2010-05-09T12:02:01').to_time.should be_an_instance_of(Time)
|
102
|
+
ISO8601::DateTime.new('2010-05-09T12:02:01+04').to_time.should be_an_instance_of(Time)
|
103
|
+
ISO8601::DateTime.new('2010-05-09T12:02:01+04:00').to_time.should be_an_instance_of(Time)
|
104
|
+
ISO8601::DateTime.new('2010-05-09T12:02:01-04:00').to_time.should be_an_instance_of(Time)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#+' do
|
109
|
+
it "should return the result of the addition" do
|
110
|
+
(ISO8601::DateTime.new('2012-07-07T20:20:20Z') + 10).to_time.should == ISO8601::DateTime.new('2012-07-07T20:20:30Z').to_time
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe '#-' do
|
115
|
+
it "should return the result of the substraction" do
|
116
|
+
(ISO8601::DateTime.new('2012-07-07T20:20:20Z') - 10).to_time.should == ISO8601::DateTime.new('2012-07-07T20:20:10+00:00').to_time
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe ISO8601::Duration do
|
6
|
+
it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
|
7
|
+
expect { ISO8601::Duration.new('P') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
8
|
+
expect { ISO8601::Duration.new('PT') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
9
|
+
expect { ISO8601::Duration.new('P1YT') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
10
|
+
expect { ISO8601::Duration.new('T') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
11
|
+
expect { ISO8601::Duration.new('PW') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
12
|
+
expect { ISO8601::Duration.new('P1Y1W') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
13
|
+
expect { ISO8601::Duration.new('~P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
14
|
+
expect { ISO8601::Duration.new('.P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
15
|
+
end
|
16
|
+
it "should parse any allowed pattern" do
|
17
|
+
expect { ISO8601::Duration.new('P1Y') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
18
|
+
expect { ISO8601::Duration.new('P1Y1M') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
19
|
+
expect { ISO8601::Duration.new('P1Y1M1D') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
20
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
21
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
22
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
23
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1.0S') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
24
|
+
expect { ISO8601::Duration.new('P1W') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
25
|
+
expect { ISO8601::Duration.new('+P1Y') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
26
|
+
expect { ISO8601::Duration.new('-P1Y') }.to_not raise_error(ISO8601::Errors::UnknownPattern)
|
27
|
+
end
|
28
|
+
it "should raise a TypeError when the base is not a ISO8601::DateTime" do
|
29
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', ISO8601::DateTime.new('2010-01-01')) }.to_not raise_error(TypeError)
|
30
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', '2010-01-01') }.to raise_error(TypeError)
|
31
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', 2010) }.to raise_error(TypeError)
|
32
|
+
expect {
|
33
|
+
d = ISO8601::Duration.new('P1Y1M1DT1H1M1S', ISO8601::DateTime.new('2010-01-01'))
|
34
|
+
d.base = 2012
|
35
|
+
}.to raise_error(TypeError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return a Duration instance from a Numeric input" do
|
39
|
+
puts ISO8601::Duration.new(37065906, ISO8601::DateTime.new('2012-01-01')).should be_an_instance_of ISO8601::Duration
|
40
|
+
ISO8601::Duration.new(36993906, ISO8601::DateTime.new('2012-01-01')).should == ISO8601::Duration.new('P1Y2M3DT4H5M6S', ISO8601::DateTime.new('2012-01-01'))
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#base' do
|
45
|
+
it "should return the base datetime" do
|
46
|
+
dt = ISO8601::DateTime.new('2010-01-01')
|
47
|
+
dt2 = ISO8601::DateTime.new('2012-01-01')
|
48
|
+
ISO8601::Duration.new('P1Y1M1DT1H1M1S').base.should be_an_instance_of(NilClass)
|
49
|
+
ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base.should be_an_instance_of(ISO8601::DateTime)
|
50
|
+
ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base.should equal(dt)
|
51
|
+
d = ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base = dt2
|
52
|
+
d.should equal(dt2)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#to_s' do
|
57
|
+
it "should return the duration as a string" do
|
58
|
+
ISO8601::Duration.new('P1Y1M1DT1H1M1S').to_s.should == 'P1Y1M1DT1H1M1S'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#+' do
|
63
|
+
it "should raise an ISO8601::Errors::DurationBaseError" do
|
64
|
+
expect { ISO8601::Duration.new('PT1H', ISO8601::DateTime.new('2000-01-01')) + ISO8601::Duration.new('PT1H') }.to raise_error(ISO8601::Errors::DurationBaseError)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should return the result of the addition" do
|
68
|
+
(ISO8601::Duration.new('P1Y1M1DT1H1M1S') + ISO8601::Duration.new('PT10S')).to_s.should == 'P1Y1M1DT1H1M11S'
|
69
|
+
(ISO8601::Duration.new('P1Y1M1DT1H1M1S') + ISO8601::Duration.new('PT10S')).should be_an_instance_of(ISO8601::Duration)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#-' do
|
74
|
+
it "should raise an ISO8601::Errors::DurationBaseError" do
|
75
|
+
expect { ISO8601::Duration.new('PT1H', ISO8601::DateTime.new('2000-01-01')) - ISO8601::Duration.new('PT1H') }.to raise_error(ISO8601::Errors::DurationBaseError)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return the result of the substraction" do
|
79
|
+
(ISO8601::Duration.new('P1Y1M1DT1H1M1S') - ISO8601::Duration.new('PT10S')).should be_an_instance_of(ISO8601::Duration)
|
80
|
+
(ISO8601::Duration.new('P1Y1M1DT1H1M11S') - ISO8601::Duration.new('PT10S')).to_s.should == 'P1Y1M1DT1H1M1S'
|
81
|
+
(ISO8601::Duration.new('PT12S') - ISO8601::Duration.new('PT12S')).to_s.should == 'PT0S'
|
82
|
+
(ISO8601::Duration.new('PT12S') - ISO8601::Duration.new('PT1S')).to_s.should == 'PT11S'
|
83
|
+
(ISO8601::Duration.new('PT1S') - ISO8601::Duration.new('PT12S')).to_s.should == '-PT11S'
|
84
|
+
(ISO8601::Duration.new('PT1S') - ISO8601::Duration.new('-PT12S')).to_s.should == 'PT13S'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe '#to_seconds' do
|
89
|
+
it "should return the seconds of a P[n]Y duration in a common year" do
|
90
|
+
ISO8601::Duration.new('P2Y', ISO8601::DateTime.new('2010-01-01')).to_seconds.should == (Time.utc(2012, 1) - Time.utc(2010, 1))
|
91
|
+
end
|
92
|
+
it "should return the seconds of a P[n]Y duration in a leap year" do
|
93
|
+
ISO8601::Duration.new('P2Y', ISO8601::DateTime.new('2000-01-01')).to_seconds.should == (Time.utc(2002, 1) - Time.utc(2000, 1))
|
94
|
+
end
|
95
|
+
it "should return the seconds of a P[n]Y[n]M duration in a common year" do
|
96
|
+
ISO8601::Duration.new('P2Y3M', ISO8601::DateTime.new('2010-01-01')).to_seconds.should == (Time.utc(2012, 4) - Time.utc(2010, 1))
|
97
|
+
end
|
98
|
+
it "should return the seconds of a P[n]Y[n]M duration in a leap year" do
|
99
|
+
ISO8601::Duration.new('P2Y3M', ISO8601::DateTime.new('2000-01-01')).to_seconds.should == (Time.utc(2002, 4) - Time.utc(2000, 1))
|
100
|
+
end
|
101
|
+
it "should return the seconds of a P[n]M duration in a common year" do
|
102
|
+
ISO8601::Duration.new('P1M', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 2) - Time.utc(2012, 1))
|
103
|
+
ISO8601::Duration.new('P2M', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 3) - Time.utc(2012, 1))
|
104
|
+
ISO8601::Duration.new('P14M', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2013, 3) - Time.utc(2012, 1))
|
105
|
+
end
|
106
|
+
it "should return the seconds of a P[n]M duration in a leap year" do
|
107
|
+
ISO8601::Duration.new('P1M', ISO8601::DateTime.new('2000-01-01')).to_seconds.should == (Time.utc(2000, 2) - Time.utc(2000, 1))
|
108
|
+
ISO8601::Duration.new('P2M', ISO8601::DateTime.new('2000-01-01')).to_seconds.should == (Time.utc(2000, 3) - Time.utc(2000, 1))
|
109
|
+
ISO8601::Duration.new('P14M', ISO8601::DateTime.new('2000-01-01')).to_seconds.should == (Time.utc(2001, 3) - Time.utc(2000, 1))
|
110
|
+
end
|
111
|
+
it "should return the seconds of a P[n]Y[n]M[n]D duration" do
|
112
|
+
ISO8601::Duration.new('P2Y11D', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2014, 1, 12) - Time.utc(2012, 1))
|
113
|
+
ISO8601::Duration.new('P1Y1M1D', ISO8601::DateTime.new('2010-05-01')).to_seconds.should == (Time.utc(2011, 6, 2) - Time.utc(2010, 5))
|
114
|
+
end
|
115
|
+
it "should return the seconds of a P[n]D duration" do
|
116
|
+
ISO8601::Duration.new('P1D', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 1, 2) - Time.utc(2012, 1, 1))
|
117
|
+
ISO8601::Duration.new('P11D', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 1, 12) - Time.utc(2012, 1, 1))
|
118
|
+
ISO8601::Duration.new('P3M11D', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 4, 12) - Time.utc(2012, 1))
|
119
|
+
end
|
120
|
+
it "should return the seconds of a P[n]W duration" do
|
121
|
+
ISO8601::Duration.new('P2W', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 1, 15) - Time.utc(2012, 1))
|
122
|
+
ISO8601::Duration.new('P2W', ISO8601::DateTime.new('2012-02-01')).to_seconds.should == (Time.utc(2012, 2, 15) - Time.utc(2012, 2))
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should return the seconds of a PT[n]H duration" do
|
126
|
+
ISO8601::Duration.new('PT5H', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 1, 1, 5) - Time.utc(2012, 1))
|
127
|
+
ISO8601::Duration.new('P1YT5H', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2013, 1, 1, 5) - Time.utc(2012, 1))
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should return the seconds of a PT[n]H[n]M duration" do
|
131
|
+
ISO8601::Duration.new('PT5M', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 1, 1, 0, 5) - Time.utc(2012, 1))
|
132
|
+
ISO8601::Duration.new('PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 1, 1, 1, 5) - Time.utc(2012, 1))
|
133
|
+
ISO8601::Duration.new('PT1H5M', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == ISO8601::Duration.new('PT65M', ISO8601::DateTime.new('2012-01-01')).to_seconds
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should return the seconds of a PT[n]H[n]M duration" do
|
137
|
+
ISO8601::Duration.new('PT10S', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == (Time.utc(2012, 1, 1, 0, 0, 10) - Time.utc(2012, 1))
|
138
|
+
ISO8601::Duration.new('PT10.4S', ISO8601::DateTime.new('2012-01-01')).to_seconds.should == 10.4
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
describe '#==' do
|
144
|
+
it "should raise an ISO8601::Errors::DurationBaseError" do
|
145
|
+
expect { ISO8601::Duration.new('PT1H', ISO8601::DateTime.new('2000-01-01')) == ISO8601::Duration.new('PT1H') }.to raise_error(ISO8601::Errors::DurationBaseError)
|
146
|
+
end
|
147
|
+
it "should return True" do
|
148
|
+
ISO8601::Duration.new('PT1H').should == ISO8601::Duration.new('PT1H')
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe '#to_abs' do
|
153
|
+
it "should return a kind of duration" do
|
154
|
+
ISO8601::Duration.new('-PT1H').abs.should be_an_instance_of ISO8601::Duration
|
155
|
+
end
|
156
|
+
it "should return the absolute value of the duration" do
|
157
|
+
ISO8601::Duration.new('-PT1H').abs.should == ISO8601::Duration.new('PT1H')
|
158
|
+
(ISO8601::Duration.new('PT1H') - ISO8601::Duration.new('PT2H')).abs.should == ISO8601::Duration.new('PT1H')
|
159
|
+
(ISO8601::Duration.new('PT1H') - ISO8601::Duration.new('-PT2H')).abs.should == ISO8601::Duration.new('PT3H')
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|