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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/CONTRIBUTING.md +58 -0
- data/Gemfile +2 -2
- data/README.md +28 -102
- data/docs/date-time.md +86 -0
- data/docs/duration.md +77 -0
- data/docs/time-interval.md +120 -0
- data/iso8601.gemspec +43 -6
- data/lib/iso8601.rb +15 -7
- data/lib/iso8601/atomic.rb +78 -0
- data/lib/iso8601/date.rb +35 -10
- data/lib/iso8601/date_time.rb +14 -3
- data/lib/iso8601/days.rb +47 -0
- data/lib/iso8601/duration.rb +115 -99
- data/lib/iso8601/errors.rb +13 -1
- data/lib/iso8601/hours.rb +43 -0
- data/lib/iso8601/minutes.rb +43 -0
- data/lib/iso8601/months.rb +98 -0
- data/lib/iso8601/seconds.rb +47 -0
- data/lib/iso8601/time.rb +43 -15
- data/lib/iso8601/time_interval.rb +392 -0
- data/lib/iso8601/version.rb +1 -1
- data/lib/iso8601/weeks.rb +43 -0
- data/lib/iso8601/years.rb +80 -0
- data/spec/iso8601/date_spec.rb +0 -6
- data/spec/iso8601/date_time_spec.rb +0 -8
- data/spec/iso8601/days_spec.rb +44 -0
- data/spec/iso8601/duration_spec.rb +103 -99
- data/spec/iso8601/hours_spec.rb +44 -0
- data/spec/iso8601/minutes_spec.rb +44 -0
- data/spec/iso8601/months_spec.rb +86 -0
- data/spec/iso8601/seconds_spec.rb +44 -0
- data/spec/iso8601/time_interval_spec.rb +416 -0
- data/spec/iso8601/time_spec.rb +0 -6
- data/spec/iso8601/weeks_spec.rb +46 -0
- data/spec/iso8601/years_spec.rb +69 -0
- metadata +37 -19
- data/.dockerignore +0 -7
- data/.editorconfig +0 -9
- data/.gitignore +0 -19
- data/.rubocop.yml +0 -38
- data/.travis.yml +0 -19
- data/Dockerfile +0 -10
- data/Makefile +0 -19
- data/circle.yml +0 -13
- data/lib/iso8601/atoms.rb +0 -279
- data/spec/iso8601/atoms_spec.rb +0 -329
data/lib/iso8601/version.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module ISO8601
|
4
|
+
##
|
5
|
+
# A Weeks atom in a {ISO8601::Duration}
|
6
|
+
class Weeks
|
7
|
+
include Atomic
|
8
|
+
|
9
|
+
AVERAGE_FACTOR = 604800
|
10
|
+
|
11
|
+
##
|
12
|
+
# @param [Numeric] atom The atom value
|
13
|
+
def initialize(atom)
|
14
|
+
valid_atom?(atom)
|
15
|
+
|
16
|
+
@atom = atom
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# The Week factor
|
21
|
+
#
|
22
|
+
# @return [Numeric]
|
23
|
+
def factor
|
24
|
+
AVERAGE_FACTOR
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# The amount of seconds
|
29
|
+
#
|
30
|
+
# @return [Numeric]
|
31
|
+
def to_seconds
|
32
|
+
AVERAGE_FACTOR * atom
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# The atom symbol.
|
37
|
+
#
|
38
|
+
# @return [Symbol]
|
39
|
+
def symbol
|
40
|
+
:W
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module ISO8601
|
4
|
+
##
|
5
|
+
# A Years atom in a {ISO8601::Duration}
|
6
|
+
#
|
7
|
+
# A "calendar year" is the cyclic time interval in a calendar which is
|
8
|
+
# required for one revolution of the Earth around the Sun and approximated to
|
9
|
+
# an integral number of "calendar days".
|
10
|
+
#
|
11
|
+
# A "duration year" is the duration of 365 or 366 "calendar days" depending
|
12
|
+
# on the start and/or the end of the corresponding time interval within the
|
13
|
+
# specific "calendar year".
|
14
|
+
class Years
|
15
|
+
include Atomic
|
16
|
+
|
17
|
+
##
|
18
|
+
# The "duration year" average is calculated through time intervals of 400
|
19
|
+
# "duration years". Each cycle of 400 "duration years" has 303 "common
|
20
|
+
# years" of 365 "calendar days" and 97 "leap years" of 366 "calendar days".
|
21
|
+
AVERAGE_FACTOR = ((365 * 303 + 366 * 97) / 400) * 86400
|
22
|
+
|
23
|
+
##
|
24
|
+
# @param [Numeric] atom The atom value
|
25
|
+
def initialize(atom)
|
26
|
+
valid_atom?(atom)
|
27
|
+
|
28
|
+
@atom = atom
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# The Year factor
|
33
|
+
#
|
34
|
+
# @param [ISO8601::DateTime, nil] base (nil) The base datetime to compute
|
35
|
+
# the year length.
|
36
|
+
#
|
37
|
+
# @return [Integer]
|
38
|
+
def factor(base = nil)
|
39
|
+
valid_base?(base)
|
40
|
+
|
41
|
+
return AVERAGE_FACTOR if base.nil?
|
42
|
+
return adjusted_factor(1, base) if atom.zero?
|
43
|
+
|
44
|
+
adjusted_factor(atom, base)
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# The amount of seconds
|
49
|
+
#
|
50
|
+
# @param [ISO8601::DateTime, nil] base (nil) The base datetime to compute
|
51
|
+
# the year length.
|
52
|
+
#
|
53
|
+
# @return [Numeric]
|
54
|
+
def to_seconds(base = nil)
|
55
|
+
valid_base?(base)
|
56
|
+
|
57
|
+
return (AVERAGE_FACTOR * atom) if base.nil?
|
58
|
+
|
59
|
+
::Time.utc(year(atom, base)) - ::Time.utc(base.year)
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# The atom symbol.
|
64
|
+
#
|
65
|
+
# @return [Symbol]
|
66
|
+
def symbol
|
67
|
+
:Y
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def adjusted_factor(atom, base)
|
73
|
+
(::Time.utc((base.year + atom).to_i) - ::Time.utc(base.year)) / atom
|
74
|
+
end
|
75
|
+
|
76
|
+
def year(atom, base)
|
77
|
+
(base.year + atom).to_i
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/spec/iso8601/date_spec.rb
CHANGED
@@ -57,18 +57,12 @@ describe ISO8601::Date do
|
|
57
57
|
it "should return the result of the addition of a number" do
|
58
58
|
expect((ISO8601::Date.new('2012-07-07') + 7).to_s).to eq('2012-07-14')
|
59
59
|
end
|
60
|
-
it "should return the result of the addition of a Duration" do
|
61
|
-
expect((ISO8601::Date.new('2012-07-07') + ISO8601::Duration.new('P7D')).to_s).to eq('2012-07-14')
|
62
|
-
end
|
63
60
|
end
|
64
61
|
|
65
62
|
describe '#-' do
|
66
63
|
it "should return the result of the subtraction" do
|
67
64
|
expect((ISO8601::Date.new('2012-07-07') - 7).to_s).to eq('2012-06-30')
|
68
65
|
end
|
69
|
-
it "should return the result of the subtraction" do
|
70
|
-
expect((ISO8601::Date.new('2012-07-07') - ISO8601::Duration.new('P7D')).to_s).to eq('2012-06-30')
|
71
|
-
end
|
72
66
|
end
|
73
67
|
|
74
68
|
describe '#to_a' do
|
@@ -111,19 +111,12 @@ describe ISO8601::DateTime do
|
|
111
111
|
expect((ISO8601::DateTime.new('2012-07-07T20:20:20.5Z') + 10).second).to eq(30.5)
|
112
112
|
expect((ISO8601::DateTime.new('2012-07-07T20:20:20+02:00') + 10.09).second).to eq(30.1)
|
113
113
|
end
|
114
|
-
it "should return the result of the addition of a Duration" do
|
115
|
-
expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') + ISO8601::Duration.new("PT10S")).to_s).to eq('2012-07-07T20:20:30+00:00')
|
116
|
-
expect((ISO8601::DateTime.new('2015-07-07T20:20:20Z') + ISO8601::Duration.new("P10D")).to_s).to eq('2015-07-17T20:20:20+00:00')
|
117
|
-
end
|
118
114
|
end
|
119
115
|
|
120
116
|
describe '#-' do
|
121
117
|
it "should return the result of the subtraction of a number" do
|
122
118
|
expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') - 10).to_s).to eq('2012-07-07T20:20:10+00:00')
|
123
119
|
end
|
124
|
-
it "should return the result of the subtraction of a Duration" do
|
125
|
-
expect((ISO8601::DateTime.new('2012-07-07T20:20:20Z') - ISO8601::Duration.new(10)).to_s).to eq('2012-07-07T20:20:10+00:00')
|
126
|
-
end
|
127
120
|
end
|
128
121
|
|
129
122
|
describe '#to_a' do
|
@@ -158,7 +151,6 @@ describe ISO8601::DateTime do
|
|
158
151
|
|
159
152
|
it "should identify as the same when two dates with different timezones are the same timestamp" do
|
160
153
|
expect(ISO8601::DateTime.new('2014-10-11T12:13:14Z') == ISO8601::DateTime.new('2014-10-11T13:13:14+01:00')).to be_truthy
|
161
|
-
|
162
154
|
end
|
163
155
|
end
|
164
156
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe ISO8601::Days do
|
4
|
+
describe 'Atomic' do
|
5
|
+
let(:subject) { ISO8601::Days.new(1) }
|
6
|
+
|
7
|
+
it "should respond to the Atomic interface" do
|
8
|
+
expect(subject).to respond_to(:factor)
|
9
|
+
expect(subject).to respond_to(:to_seconds)
|
10
|
+
expect(subject).to respond_to(:symbol)
|
11
|
+
expect(subject).to respond_to(:to_i)
|
12
|
+
expect(subject).to respond_to(:to_f)
|
13
|
+
expect(subject).to respond_to(:to_s)
|
14
|
+
expect(subject).to respond_to(:value)
|
15
|
+
expect(subject).to respond_to(:<=>)
|
16
|
+
expect(subject).to respond_to(:eql?)
|
17
|
+
expect(subject).to respond_to(:hash)
|
18
|
+
expect(subject).to respond_to(:valid_atom?)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#factor' do
|
23
|
+
it "should return the Day factor" do
|
24
|
+
expect(ISO8601::Days.new(2).factor).to eq(86400)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return the amount of seconds" do
|
28
|
+
expect(ISO8601::Days.new(2).to_seconds).to eq(172800)
|
29
|
+
expect(ISO8601::Days.new(-2).to_seconds).to eq(-172800)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#symbol' do
|
34
|
+
it "should return the ISO symbol" do
|
35
|
+
expect(ISO8601::Days.new(1).symbol).to eq(:D)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#hash' do
|
40
|
+
it "should build hash identity by value" do
|
41
|
+
expect(ISO8601::Days.new(3).hash).to eq(ISO8601::Days.new(3).hash)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -3,6 +3,12 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
RSpec.describe ISO8601::Duration do
|
6
|
+
let(:common_year) { ISO8601::DateTime.new('2010-01-01') }
|
7
|
+
let(:leap_year) { ISO8601::DateTime.new('2000-01-01') }
|
8
|
+
|
9
|
+
let(:common_february) { ISO8601::DateTime.new('2010-02-01') }
|
10
|
+
let(:leap_february) { ISO8601::DateTime.new('2000-02-01') }
|
11
|
+
|
6
12
|
it "should raise a ISO8601::Errors::UnknownPattern for any unknown pattern" do
|
7
13
|
expect { ISO8601::Duration.new('') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
8
14
|
expect { ISO8601::Duration.new('P') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
@@ -14,11 +20,13 @@ RSpec.describe ISO8601::Duration do
|
|
14
20
|
expect { ISO8601::Duration.new('~P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
15
21
|
expect { ISO8601::Duration.new('.P1Y') }.to raise_error(ISO8601::Errors::UnknownPattern)
|
16
22
|
end
|
23
|
+
|
17
24
|
it "should raise a ISO8601::Errors::InvalidFraction for any invalid patterns" do
|
18
25
|
expect { ISO8601::Duration.new('P1.5Y0.5M') }.to raise_error(ISO8601::Errors::InvalidFractions)
|
19
26
|
expect { ISO8601::Duration.new('P1.5Y1M') }.to raise_error(ISO8601::Errors::InvalidFractions)
|
20
27
|
expect { ISO8601::Duration.new('P1.5MT10.5S') }.to raise_error(ISO8601::Errors::InvalidFractions)
|
21
28
|
end
|
29
|
+
|
22
30
|
it "should parse any allowed pattern" do
|
23
31
|
expect { ISO8601::Duration.new('P1Y') }.to_not raise_error
|
24
32
|
expect { ISO8601::Duration.new('P0.5Y') }.to_not raise_error
|
@@ -42,32 +50,11 @@ RSpec.describe ISO8601::Duration do
|
|
42
50
|
expect { ISO8601::Duration.new('+P1Y') }.to_not raise_error
|
43
51
|
expect { ISO8601::Duration.new('-P1Y') }.to_not raise_error
|
44
52
|
end
|
45
|
-
it "should raise a TypeError when the base is not a ISO8601::DateTime" do
|
46
|
-
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S', ISO8601::DateTime.new('2010-01-01')) }.to_not raise_error
|
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)
|
49
|
-
expect {
|
50
|
-
d = ISO8601::Duration.new('P1Y1M1DT1H1M1S', ISO8601::DateTime.new('2010-01-01'))
|
51
|
-
d.base = 2012
|
52
|
-
}.to raise_error(ISO8601::Errors::TypeError)
|
53
|
-
end
|
54
53
|
|
55
|
-
it "should
|
56
|
-
|
57
|
-
|
58
|
-
expect(
|
59
|
-
end
|
60
|
-
|
61
|
-
describe '#base' do
|
62
|
-
it "should return the base datetime" do
|
63
|
-
dt = ISO8601::DateTime.new('2010-01-01')
|
64
|
-
dt2 = ISO8601::DateTime.new('2012-01-01')
|
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)
|
68
|
-
d = ISO8601::Duration.new('P1Y1M1DT1H1M1S', dt).base = dt2
|
69
|
-
expect(d).to eq(dt2)
|
70
|
-
end
|
54
|
+
it "should raise a TypeError when the base is not a ISO8601::DateTime" do
|
55
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S').to_seconds(common_year) }.to_not raise_error
|
56
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S').to_seconds('2010-01-01') }.to raise_error(ISO8601::Errors::TypeError)
|
57
|
+
expect { ISO8601::Duration.new('P1Y1M1DT1H1M1S').to_seconds(2010) }.to raise_error(ISO8601::Errors::TypeError)
|
71
58
|
end
|
72
59
|
|
73
60
|
describe '#pattern' do
|
@@ -84,22 +71,24 @@ RSpec.describe ISO8601::Duration do
|
|
84
71
|
end
|
85
72
|
|
86
73
|
describe '#+' do
|
87
|
-
it "should raise an ISO8601::Errors::DurationBaseError" do
|
88
|
-
expect { ISO8601::Duration.new('PT1H', ISO8601::DateTime.new('2000-01-01')) + ISO8601::Duration.new('PT1H') }.to raise_error(ISO8601::Errors::DurationBaseError)
|
89
|
-
end
|
90
|
-
|
91
74
|
it "should return the result of the addition" do
|
92
75
|
expect((ISO8601::Duration.new('P11Y1M1DT1H1M1S') + ISO8601::Duration.new('P1Y1M1DT1H1M1S')).to_s).to eq('P12Y2M2DT2H2M2S')
|
93
76
|
expect((ISO8601::Duration.new('P1Y1M1DT1H1M1S') + ISO8601::Duration.new('PT10S')).to_s).to eq('P1Y1M1DT1H1M11S')
|
94
77
|
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S') + ISO8601::Duration.new('PT10S')).to be_an_instance_of(ISO8601::Duration)
|
95
78
|
end
|
96
|
-
end
|
97
79
|
|
98
|
-
|
99
|
-
|
100
|
-
expect
|
80
|
+
it "should perform addition operation with Numeric class" do
|
81
|
+
day = 60 * 60 * 24
|
82
|
+
expect((ISO8601::Duration.new('P11Y1M1DT1H1M1S') + day).to_s).to eq('P11Y1M2DT1H1M1S')
|
83
|
+
expect((ISO8601::Duration.new('P11Y1M1DT1H1M1S') + (2 * day).to_f).to_s).to eq('P11Y1M3DT1H1M1S')
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should raise ISO8601::Errors::TypeError when other object is not Numeric or ISO8601::Duration" do
|
87
|
+
expect { ISO8601::Duration.new('PT1H') + 'wololo' }.to raise_error(ISO8601::Errors::TypeError)
|
101
88
|
end
|
89
|
+
end
|
102
90
|
|
91
|
+
describe '#-' do
|
103
92
|
it "should return the result of the subtraction" do
|
104
93
|
expect(ISO8601::Duration.new('P1Y1M1DT1H1M1S') - ISO8601::Duration.new('PT10S')).to be_an_instance_of(ISO8601::Duration)
|
105
94
|
expect((ISO8601::Duration.new('P1Y1M1DT1H1M11S') - ISO8601::Duration.new('PT10S')).to_s).to eq('P1Y1M1DT1H1M1S')
|
@@ -109,126 +98,138 @@ RSpec.describe ISO8601::Duration do
|
|
109
98
|
expect((ISO8601::Duration.new('PT1S') - ISO8601::Duration.new('PT12S')).to_s).to eq('-PT11S')
|
110
99
|
expect((ISO8601::Duration.new('PT1S') - ISO8601::Duration.new('-PT12S')).to_s).to eq('PT13S')
|
111
100
|
end
|
112
|
-
end
|
113
101
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
expect(ISO8601::Duration.new('
|
102
|
+
it "should perform subtract operation with Numeric class" do
|
103
|
+
day = 60 * 60 * 24
|
104
|
+
minute = 60
|
105
|
+
expect((ISO8601::Duration.new('P11Y1M1DT1H1M1S') - day).to_s).to eq('P11Y1MT1H1M1S')
|
106
|
+
expect((ISO8601::Duration.new('P11Y1M1DT1H1M1S') - (2 * minute).to_f).to_s).to eq('P11Y1M1DT59M1S')
|
118
107
|
end
|
119
108
|
end
|
120
109
|
|
121
110
|
describe '#to_seconds' do
|
122
111
|
context 'positive durations' do
|
123
112
|
it "should return the seconds of a P[n]Y duration in a common year" do
|
124
|
-
expect(ISO8601::Duration.new('P2Y'
|
113
|
+
expect(ISO8601::Duration.new('P2Y').to_seconds(common_year)).to eq(Time.utc(2012, 1) - Time.utc(2010, 1))
|
125
114
|
end
|
115
|
+
|
126
116
|
it "should return the seconds of a P[n]Y duration in a leap year" do
|
127
|
-
expect(ISO8601::Duration.new('P2Y'
|
117
|
+
expect(ISO8601::Duration.new('P2Y').to_seconds(leap_year)).to eq(Time.utc(2002, 1) - Time.utc(2000, 1))
|
128
118
|
end
|
119
|
+
|
129
120
|
it "should return the seconds of a P[n]Y[n]M duration in a common year" do
|
130
|
-
expect(ISO8601::Duration.new('P2Y3M'
|
121
|
+
expect(ISO8601::Duration.new('P2Y3M').to_seconds(common_year)).to eq(Time.utc(2012, 4) - Time.utc(2010, 1))
|
131
122
|
end
|
123
|
+
|
132
124
|
it "should return the seconds of a P[n]Y[n]M duration in a leap year" do
|
133
|
-
expect(ISO8601::Duration.new('P2Y3M'
|
125
|
+
expect(ISO8601::Duration.new('P2Y3M').to_seconds(leap_year)).to eq(Time.utc(2002, 4) - Time.utc(2000, 1))
|
134
126
|
end
|
127
|
+
|
135
128
|
it "should return the seconds of a P[n]M duration in a common year" do
|
136
|
-
expect(ISO8601::Duration.new('P1M'
|
137
|
-
expect(ISO8601::Duration.new('
|
138
|
-
expect(ISO8601::Duration.new('
|
139
|
-
expect(ISO8601::Duration.new('P14M', ISO8601::DateTime.new('2012-01-01')).to_seconds).to eq(Time.utc(2013, 3) - Time.utc(2012, 1))
|
129
|
+
expect(ISO8601::Duration.new('P1M').to_seconds(common_year)).to eq(2678400)
|
130
|
+
expect(ISO8601::Duration.new('P19M').to_seconds(ISO8601::DateTime.new('2012-05-01'))).to eq(Time.utc(2014, 12) - Time.utc(2012, 5))
|
131
|
+
expect(ISO8601::Duration.new('P14M').to_seconds(common_year)).to eq(Time.utc(2011, 3) - Time.utc(2010, 1))
|
140
132
|
end
|
133
|
+
|
141
134
|
it "should return the seconds of a P[n]M duration in a leap year" do
|
142
|
-
expect(ISO8601::Duration.new('P1M'
|
143
|
-
expect(ISO8601::Duration.new('P2M'
|
144
|
-
expect(ISO8601::Duration.new('P14M'
|
135
|
+
expect(ISO8601::Duration.new('P1M').to_seconds(leap_year)).to eq(Time.utc(2000, 2) - Time.utc(2000, 1))
|
136
|
+
expect(ISO8601::Duration.new('P2M').to_seconds(leap_year)).to eq(Time.utc(2000, 3) - Time.utc(2000, 1))
|
137
|
+
expect(ISO8601::Duration.new('P14M').to_seconds(leap_year)).to eq(Time.utc(2001, 3) - Time.utc(2000, 1))
|
145
138
|
end
|
139
|
+
|
146
140
|
it "should return the seconds of a P[n]Y[n]M[n]D duration" do
|
147
|
-
expect(ISO8601::Duration.new('P2Y11D'
|
148
|
-
expect(ISO8601::Duration.new('P1Y1M1D'
|
141
|
+
expect(ISO8601::Duration.new('P2Y11D').to_seconds(common_year)).to eq(Time.utc(2012, 1, 12) - Time.utc(2010, 1))
|
142
|
+
expect(ISO8601::Duration.new('P1Y1M1D').to_seconds(ISO8601::DateTime.new('2010-05-01'))).to eq(Time.utc(2011, 6, 2) - Time.utc(2010, 5))
|
149
143
|
end
|
144
|
+
|
150
145
|
it "should return the seconds of a P[n]D duration" do
|
151
|
-
expect(ISO8601::Duration.new('P1D'
|
152
|
-
expect(ISO8601::Duration.new('P11D'
|
153
|
-
expect(ISO8601::Duration.new('P3M11D'
|
146
|
+
expect(ISO8601::Duration.new('P1D').to_seconds(common_year)).to eq(Time.utc(2010, 1, 2) - Time.utc(2010, 1, 1))
|
147
|
+
expect(ISO8601::Duration.new('P11D').to_seconds(common_year)).to eq(Time.utc(2010, 1, 12) - Time.utc(2010, 1, 1))
|
148
|
+
expect(ISO8601::Duration.new('P3M11D').to_seconds(common_year)).to eq(Time.utc(2010, 4, 12) - Time.utc(2010, 1))
|
154
149
|
end
|
150
|
+
|
155
151
|
it "should return the seconds of a P[n]W duration" do
|
156
|
-
expect(ISO8601::Duration.new('P2W'
|
157
|
-
expect(ISO8601::Duration.new('P2W', ISO8601::DateTime.new('2012-02-01')).to_seconds).to eq(Time.utc(2012, 2, 15) - Time.utc(2012, 2))
|
152
|
+
expect(ISO8601::Duration.new('P2W').to_seconds).to eq(1209600)
|
158
153
|
end
|
159
154
|
|
160
155
|
it "should return the seconds of a PT[n]H duration" do
|
161
|
-
expect(ISO8601::Duration.new('PT5H'
|
162
|
-
expect(ISO8601::Duration.new('P1YT5H'
|
156
|
+
expect(ISO8601::Duration.new('PT5H').to_seconds).to eq(18000)
|
157
|
+
expect(ISO8601::Duration.new('P1YT5H').to_seconds(common_year)).to eq(Time.utc(2011, 1, 1, 5) - Time.utc(2010, 1))
|
163
158
|
end
|
164
159
|
|
165
160
|
it "should return the seconds of a PT[n]H[n]M duration" do
|
166
|
-
expect(ISO8601::Duration.new('PT5M'
|
167
|
-
expect(ISO8601::Duration.new('PT1H5M'
|
168
|
-
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)
|
161
|
+
expect(ISO8601::Duration.new('PT5M').to_seconds).to eq(60 * 5)
|
162
|
+
expect(ISO8601::Duration.new('PT1H5M').to_seconds).to eq(3900)
|
169
163
|
end
|
170
164
|
|
171
165
|
it "should return the seconds of a PT[n]H[n]M duration" do
|
172
|
-
expect(ISO8601::Duration.new('PT10S'
|
173
|
-
expect(ISO8601::Duration.new('PT10.4S'
|
166
|
+
expect(ISO8601::Duration.new('PT10S').to_seconds).to eq(10)
|
167
|
+
expect(ISO8601::Duration.new('PT10.4S').to_seconds).to eq(10.4)
|
174
168
|
end
|
175
169
|
end
|
176
170
|
|
177
171
|
context 'negative durations' do
|
178
172
|
it "should return the seconds of a -P[n]Y duration" do
|
179
|
-
expect(ISO8601::Duration.new('-P2Y'
|
173
|
+
expect(ISO8601::Duration.new('-P2Y').to_seconds(common_year)).to eq(Time.utc(2008, 1) - Time.utc(2010, 1))
|
180
174
|
end
|
175
|
+
|
181
176
|
it "should return the seconds of a -P[n]Y duration in a leap year" do
|
182
|
-
expect(ISO8601::Duration.new('-P2Y'
|
177
|
+
expect(ISO8601::Duration.new('-P2Y').to_seconds(ISO8601::DateTime.new('2001-01-01'))).to eq(Time.utc(1999, 1) - Time.utc(2001, 1))
|
183
178
|
end
|
179
|
+
|
184
180
|
it "should return the seconds of a -P[n]Y[n]M duration in a common year" do
|
185
|
-
expect(ISO8601::Duration.new('-P2Y3M'
|
181
|
+
expect(ISO8601::Duration.new('-P2Y3M').to_seconds(ISO8601::DateTime.new('2010-01-01'))).to eq(Time.utc(2007, 10) - Time.utc(2010, 1))
|
186
182
|
end
|
183
|
+
|
187
184
|
it "should return the seconds of a -P[n]Y[n]M duration in a leap year" do
|
188
|
-
expect(ISO8601::Duration.new('-P2Y3M'
|
185
|
+
expect(ISO8601::Duration.new('-P2Y3M').to_seconds(ISO8601::DateTime.new('2001-01-01'))).to eq(Time.utc(1998, 10) - Time.utc(2001, 1))
|
189
186
|
end
|
187
|
+
|
190
188
|
it "should return the seconds of a -P[n]M duration in a common year" do
|
191
|
-
expect(ISO8601::Duration.new('-P1M'
|
192
|
-
expect(ISO8601::Duration.new('-P1M'
|
193
|
-
expect(ISO8601::Duration.new('-P2M'
|
194
|
-
expect(ISO8601::Duration.new('-P36M'
|
195
|
-
expect(ISO8601::Duration.new('-P39M'
|
196
|
-
expect(ISO8601::Duration.new('-P156M'
|
189
|
+
expect(ISO8601::Duration.new('-P1M').to_seconds(ISO8601::DateTime.new('2012-01-01'))).to eq(Time.utc(2011, 12) - Time.utc(2012, 1))
|
190
|
+
expect(ISO8601::Duration.new('-P1M').to_seconds(ISO8601::DateTime.new('2012-02-01'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 2))
|
191
|
+
expect(ISO8601::Duration.new('-P2M').to_seconds(ISO8601::DateTime.new('2012-03-01'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 3))
|
192
|
+
expect(ISO8601::Duration.new('-P36M').to_seconds(ISO8601::DateTime.new('2013-03-01'))).to eq(Time.utc(2010, 3) - Time.utc(2013, 3))
|
193
|
+
expect(ISO8601::Duration.new('-P39M').to_seconds(ISO8601::DateTime.new('2013-03-01'))).to eq(Time.utc(2009, 12) - Time.utc(2013, 3))
|
194
|
+
expect(ISO8601::Duration.new('-P156M').to_seconds(ISO8601::DateTime.new('2013-03-01'))).to eq(Time.utc(2000, 3) - Time.utc(2013, 3))
|
197
195
|
end
|
196
|
+
|
198
197
|
it "should return the seconds of a -P[n]M duration in a leap year" do
|
199
|
-
expect(ISO8601::Duration.new('-P1M'
|
200
|
-
expect(ISO8601::Duration.new('-P2M'
|
201
|
-
expect(ISO8601::Duration.new('-P14M'
|
198
|
+
expect(ISO8601::Duration.new('-P1M').to_seconds(ISO8601::DateTime.new('2000-02-01'))).to eq(Time.utc(2000, 1) - Time.utc(2000, 2))
|
199
|
+
expect(ISO8601::Duration.new('-P2M').to_seconds(ISO8601::DateTime.new('2000-03-01'))).to eq(Time.utc(2000, 1) - Time.utc(2000, 3))
|
200
|
+
expect(ISO8601::Duration.new('-P14M').to_seconds(ISO8601::DateTime.new('2001-03-01'))).to eq(Time.utc(2000, 1) - Time.utc(2001, 3))
|
202
201
|
end
|
202
|
+
|
203
203
|
it "should return the seconds of a -P[n]Y[n]M[n]D duration" do
|
204
|
-
expect(ISO8601::Duration.new('-P2Y11D'
|
205
|
-
expect(ISO8601::Duration.new('-P1Y1M1D'
|
204
|
+
expect(ISO8601::Duration.new('-P2Y11D').to_seconds(ISO8601::DateTime.new('2014-01-12'))).to eq(Time.utc(2012, 1) - Time.utc(2014, 1, 12))
|
205
|
+
expect(ISO8601::Duration.new('-P1Y1M1D').to_seconds(ISO8601::DateTime.new('2010-05-01'))).to eq(Time.utc(2009, 3, 31) - Time.utc(2010, 5))
|
206
206
|
end
|
207
|
+
|
207
208
|
it "should return the seconds of a -P[n]D duration" do
|
208
|
-
expect(ISO8601::Duration.new('-P1D'
|
209
|
-
expect(ISO8601::Duration.new('-P11D'
|
210
|
-
expect(ISO8601::Duration.new('-P3M11D'
|
209
|
+
expect(ISO8601::Duration.new('-P1D').to_seconds(ISO8601::DateTime.new('2012-01-02'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 2))
|
210
|
+
expect(ISO8601::Duration.new('-P11D').to_seconds(ISO8601::DateTime.new('2012-01-12'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 12))
|
211
|
+
expect(ISO8601::Duration.new('-P3M11D').to_seconds(ISO8601::DateTime.new('2012-04-12'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 4, 12))
|
211
212
|
end
|
213
|
+
|
212
214
|
it "should return the seconds of a -P[n]W duration" do
|
213
|
-
expect(ISO8601::Duration.new('-P2W'
|
214
|
-
expect(ISO8601::Duration.new('-P2W', ISO8601::DateTime.new('2012-02-01')).to_seconds).to eq(Time.utc(2012, 2) - Time.utc(2012, 2, 15))
|
215
|
+
expect(ISO8601::Duration.new('-P2W').to_seconds).to eq(-1209600)
|
215
216
|
end
|
216
217
|
|
217
218
|
it "should return the seconds of a -PT[n]H duration" do
|
218
|
-
expect(ISO8601::Duration.new('-PT5H'
|
219
|
-
expect(ISO8601::Duration.new('-P1YT5H'
|
219
|
+
expect(ISO8601::Duration.new('-PT5H').to_seconds(ISO8601::DateTime.new('2012-01-01T05'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 1, 5))
|
220
|
+
expect(ISO8601::Duration.new('-P1YT5H').to_seconds(ISO8601::DateTime.new('2013-01-01T05'))).to eq(Time.utc(2012, 1) - Time.utc(2013, 1, 1, 5))
|
220
221
|
end
|
221
222
|
|
222
223
|
it "should return the seconds of a -PT[n]H[n]M duration" do
|
223
|
-
expect(ISO8601::Duration.new('-PT5M'
|
224
|
-
expect(ISO8601::Duration.new('-PT1H5M'
|
225
|
-
expect(ISO8601::Duration.new('-PT1H5M'
|
224
|
+
expect(ISO8601::Duration.new('-PT5M').to_seconds(ISO8601::DateTime.new('2012-01-01T00:05'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 1, 0, 5))
|
225
|
+
expect(ISO8601::Duration.new('-PT1H5M').to_seconds(ISO8601::DateTime.new('2012-01-01T01:05'))).to eq(Time.utc(2012, 1) - Time.utc(2012, 1, 1, 1, 5))
|
226
|
+
expect(ISO8601::Duration.new('-PT1H5M').to_seconds(common_year)).to eq(ISO8601::Duration.new('-PT65M').to_seconds(common_year))
|
226
227
|
end
|
227
228
|
|
228
229
|
it "should return the seconds of a -PT[n]H[n]M duration" do
|
229
|
-
expect(ISO8601::Duration.new('-PT10S'
|
230
|
-
expect(ISO8601::Duration.new('-PT10.4S'
|
231
|
-
expect(ISO8601::Duration.new('-PT10,4S'
|
230
|
+
expect(ISO8601::Duration.new('-PT10S').to_seconds(ISO8601::DateTime.new('2012-01-01T00:00:00'))).to eq(Time.utc(2011, 12, 31, 23, 59, 50) - Time.utc(2012, 1))
|
231
|
+
expect(ISO8601::Duration.new('-PT10.4S').to_seconds).to eq(-10.4)
|
232
|
+
expect(ISO8601::Duration.new('-PT10,4S').to_seconds).to eq(-10.4)
|
232
233
|
end
|
233
234
|
end
|
234
235
|
end
|
@@ -240,6 +241,7 @@ RSpec.describe ISO8601::Duration do
|
|
240
241
|
it "should return a kind of duration" do
|
241
242
|
expect(negative.abs).to be_instance_of(ISO8601::Duration)
|
242
243
|
end
|
244
|
+
|
243
245
|
it "should return the absolute value of the duration" do
|
244
246
|
expect(negative.abs).to eq(positive)
|
245
247
|
end
|
@@ -250,6 +252,12 @@ RSpec.describe ISO8601::Duration do
|
|
250
252
|
expect(ISO8601::Duration.new('PT1H') == ISO8601::Duration.new('PT1H')).to be_truthy
|
251
253
|
expect(ISO8601::Duration.new('PT1H') == ISO8601::Duration.new('PT60M')).to be_truthy
|
252
254
|
end
|
255
|
+
|
256
|
+
it "should equal by a Numeric value" do
|
257
|
+
hour = 60 * 60
|
258
|
+
expect(ISO8601::Duration.new('PT1H') == hour).to be_truthy
|
259
|
+
expect(ISO8601::Duration.new('PT2H') == hour).to be_falsy
|
260
|
+
end
|
253
261
|
end
|
254
262
|
|
255
263
|
describe '#eql?' do
|
@@ -257,24 +265,20 @@ RSpec.describe ISO8601::Duration do
|
|
257
265
|
subject = ISO8601::Duration.new('PT1H')
|
258
266
|
expect(subject).to respond_to(:eql?)
|
259
267
|
end
|
268
|
+
|
260
269
|
it "should equal by hash identity" do
|
261
270
|
expect(ISO8601::Duration.new('PT1H').eql? ISO8601::Duration.new('PT1H')).to be_truthy
|
262
271
|
expect(ISO8601::Duration.new('PT1H').eql? ISO8601::Duration.new('PT60M')).to be_falsy
|
263
272
|
end
|
264
|
-
|
265
273
|
end
|
266
274
|
|
267
275
|
describe '#hash' do
|
268
276
|
it "should respond to #hash" do
|
269
|
-
|
270
|
-
|
271
|
-
expect(subject).to respond_to(:hash)
|
277
|
+
expect(ISO8601::Duration.new('PT1H')).to respond_to(:hash)
|
272
278
|
end
|
273
|
-
it "should build hash identity by value" do
|
274
|
-
subject = ISO8601::Duration.new('PT1H')
|
275
|
-
contrast = ISO8601::Duration.new('PT1H')
|
276
279
|
|
277
|
-
|
280
|
+
it "should build hash identity by value" do
|
281
|
+
expect(ISO8601::Duration.new('PT1H').hash).to eq(ISO8601::Duration.new('PT1H').hash)
|
278
282
|
end
|
279
283
|
end
|
280
284
|
end
|