edtf 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -2
- data/Gemfile +8 -3
- data/README.md +8 -8
- data/features/parser/date_times.feature +3 -3
- data/features/print/level_0_edtf.feature +61 -61
- data/features/step_definitions/edtf_steps.rb +25 -25
- data/features/support/env.rb +1 -1
- data/lib/edtf/date.rb +6 -3
- data/lib/edtf/date_time.rb +10 -1
- data/lib/edtf/parser.rb +344 -343
- data/lib/edtf/parser.y +29 -28
- data/lib/edtf/set.rb +3 -1
- data/lib/edtf/version.rb +1 -1
- data/spec/edtf/date_spec.rb +39 -39
- data/spec/edtf/epoch_spec.rb +15 -15
- data/spec/edtf/interval_spec.rb +35 -35
- data/spec/edtf/parser_spec.rb +123 -123
- data/spec/edtf/season_spec.rb +22 -21
- data/spec/edtf/set_spec.rb +71 -71
- data/spec/edtf/uncertainty_spec.rb +42 -42
- data/spec/spec_helper.rb +1 -1
- metadata +5 -4
data/lib/edtf/parser.y
CHANGED
@@ -34,7 +34,8 @@ rule
|
|
34
34
|
|
35
35
|
|
36
36
|
date_time : date T time {
|
37
|
-
|
37
|
+
result = DateTime.new(val[0].year, val[0].month, val[0].day, *val[2])
|
38
|
+
result.skip_timezone = (val[2].length == 3)
|
38
39
|
}
|
39
40
|
|
40
41
|
time : base_time
|
@@ -53,8 +54,8 @@ rule
|
|
53
54
|
positive_zone_offset : zone_offset_hour
|
54
55
|
| '0' '0' ':' '0' '0' { result = 0 }
|
55
56
|
;
|
56
|
-
|
57
|
-
|
57
|
+
|
58
|
+
|
58
59
|
zone_offset_hour : d01_13 ':' minute { result = Rational(val[0] * 60 + val[2], 1440) }
|
59
60
|
| '1' '4' ':' '0' '0' { result = Rational(840, 1440) }
|
60
61
|
| '0' '0' ':' d01_59 { result = Rational(val[3], 1440) }
|
@@ -66,7 +67,7 @@ rule
|
|
66
67
|
|
67
68
|
month : d01_12
|
68
69
|
day : d01_31
|
69
|
-
|
70
|
+
|
70
71
|
year_month : year '-' month { result = [val[0], val[2]] }
|
71
72
|
|
72
73
|
# We raise an exception if there are two many days for the month, but
|
@@ -134,7 +135,7 @@ rule
|
|
134
135
|
}
|
135
136
|
|
136
137
|
level_1_start : date | partial_uncertain_or_approximate | unspecified | partial_unspecified | UNKNOWN
|
137
|
-
|
138
|
+
|
138
139
|
level_1_end : level_1_start | OPEN
|
139
140
|
|
140
141
|
|
@@ -150,7 +151,7 @@ rule
|
|
150
151
|
result.precision = :year
|
151
152
|
}
|
152
153
|
;
|
153
|
-
|
154
|
+
|
154
155
|
long_year :
|
155
156
|
positive_digit digit digit digit digit {
|
156
157
|
result = val.zip([10000,1000,100,10,1]).reduce(0) { |s,(a,b)| s += a * b }
|
@@ -233,11 +234,11 @@ rule
|
|
233
234
|
| list_elements { result = EDTF::Set.new(*val[0]) }
|
234
235
|
| later { result = EDTF::Set.new(val[0]).later! }
|
235
236
|
;
|
236
|
-
|
237
|
+
|
237
238
|
list_elements : list_element { result = [val[0]].flatten }
|
238
239
|
| list_elements ',' list_element { result = val[0] + [val[2]].flatten }
|
239
240
|
;
|
240
|
-
|
241
|
+
|
241
242
|
list_element : atomic
|
242
243
|
| consecutives
|
243
244
|
;
|
@@ -289,18 +290,18 @@ rule
|
|
289
290
|
result.unspecified!(:month)
|
290
291
|
}
|
291
292
|
;
|
292
|
-
|
293
|
+
|
293
294
|
|
294
295
|
partial_uncertain_or_approximate : pua_base
|
295
296
|
| '(' pua_base ')' UA { result = uoa(val[1], val[3]) }
|
296
|
-
|
297
|
+
|
297
298
|
pua_base :
|
298
299
|
pua_year { result = val[0].year_precision! }
|
299
300
|
| pua_year_month { result = val[0][0].month_precision! }
|
300
301
|
| pua_year_month_day { result = val[0].day_precision! }
|
301
|
-
|
302
|
+
|
302
303
|
pua_year : year UA { result = uoa(Date.new(val[0]), val[1], :year) }
|
303
|
-
|
304
|
+
|
304
305
|
pua_year_month :
|
305
306
|
pua_year '-' month ua {
|
306
307
|
result = [uoa(val[0].change(:month => val[2]), val[3], [:month, :year])]
|
@@ -315,7 +316,7 @@ rule
|
|
315
316
|
result = [uoa(val[0].change(:month => val[2]), val[4], [:month]), true]
|
316
317
|
}
|
317
318
|
;
|
318
|
-
|
319
|
+
|
319
320
|
pua_year_month_day :
|
320
321
|
pua_year_month '-' day ua {
|
321
322
|
result = uoa(val[0][0].change(:day => val[2]), val[3], val[0][1] ? [:day] : nil)
|
@@ -355,7 +356,7 @@ rule
|
|
355
356
|
# result = [uoa(result, val[7], [:year]), true]
|
356
357
|
# }
|
357
358
|
;
|
358
|
-
|
359
|
+
|
359
360
|
ua : { result = [] } | UA
|
360
361
|
|
361
362
|
# ---- Auxiliary Rules ----
|
@@ -363,7 +364,7 @@ rule
|
|
363
364
|
digit : '0' { result = 0 }
|
364
365
|
| positive_digit
|
365
366
|
;
|
366
|
-
|
367
|
+
|
367
368
|
positive_digit : '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
|
368
369
|
|
369
370
|
d01_12 : '0' positive_digit { result = val[1] }
|
@@ -375,7 +376,7 @@ rule
|
|
375
376
|
d01_13 : d01_12
|
376
377
|
| '1' '3' { result = 13 }
|
377
378
|
;
|
378
|
-
|
379
|
+
|
379
380
|
d01_23 : '0' positive_digit { result = val[1] }
|
380
381
|
| '1' digit { result = 10 + val[1] }
|
381
382
|
| '2' '0' { result = 20 }
|
@@ -410,7 +411,7 @@ rule
|
|
410
411
|
| '4' digit { result = 40 + val[1] }
|
411
412
|
| '5' digit { result = 50 + val[1] }
|
412
413
|
;
|
413
|
-
|
414
|
+
|
414
415
|
d00_59 : '0' '0'
|
415
416
|
| d01_59
|
416
417
|
;
|
@@ -442,32 +443,32 @@ require 'strscan'
|
|
442
443
|
:level => 2,
|
443
444
|
:debug => false
|
444
445
|
}.freeze
|
445
|
-
|
446
|
+
|
446
447
|
class << self; attr_reader :defaults; end
|
447
|
-
|
448
|
+
|
448
449
|
attr_reader :options
|
449
|
-
|
450
|
+
|
450
451
|
def initialize(options = {})
|
451
452
|
@options = Parser.defaults.merge(options)
|
452
453
|
end
|
453
|
-
|
454
|
+
|
454
455
|
def debug?
|
455
456
|
!!(options[:debug] || ENV['DEBUG'])
|
456
457
|
end
|
457
|
-
|
458
|
+
|
458
459
|
def parse(input)
|
459
460
|
parse!(input)
|
460
461
|
rescue => e
|
461
462
|
warn e.message if debug?
|
462
463
|
nil
|
463
464
|
end
|
464
|
-
|
465
|
+
|
465
466
|
def parse!(input)
|
466
467
|
@yydebug = debug?
|
467
468
|
@src = StringScanner.new(input)
|
468
469
|
do_parse
|
469
470
|
end
|
470
|
-
|
471
|
+
|
471
472
|
def on_error(tid, value, stack)
|
472
473
|
raise ArgumentError,
|
473
474
|
"failed to parse date: unexpected '#{value}' at #{stack.inspect}"
|
@@ -479,9 +480,9 @@ require 'strscan'
|
|
479
480
|
end
|
480
481
|
date
|
481
482
|
end
|
482
|
-
|
483
|
+
|
483
484
|
alias uoa apply_uncertainty
|
484
|
-
|
485
|
+
|
485
486
|
def next_token
|
486
487
|
case
|
487
488
|
when @src.eos?
|
@@ -489,7 +490,7 @@ require 'strscan'
|
|
489
490
|
# when @src.scan(/\s+/)
|
490
491
|
# ignore whitespace
|
491
492
|
when @src.scan(/\(/)
|
492
|
-
['(', @src.matched]
|
493
|
+
['(', @src.matched]
|
493
494
|
# when @src.scan(/\)\?~-/)
|
494
495
|
# [:PUA, [:uncertain!, :approximate!]]
|
495
496
|
# when @src.scan(/\)\?-/)
|
@@ -552,4 +553,4 @@ require 'strscan'
|
|
552
553
|
end
|
553
554
|
|
554
555
|
|
555
|
-
# -*- racc -*-
|
556
|
+
# -*- racc -*-
|
data/lib/edtf/set.rb
CHANGED
data/lib/edtf/version.rb
CHANGED
data/spec/edtf/date_spec.rb
CHANGED
@@ -2,14 +2,14 @@ describe 'Date/DateTime' do
|
|
2
2
|
|
3
3
|
describe 'class methods' do
|
4
4
|
it 'responds to edtf' do
|
5
|
-
Date.
|
5
|
+
expect(Date).to respond_to(:edtf)
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
describe 'instance methods' do
|
10
10
|
[:uncertain?, :approximate?, :unspecified?, :uncertain, :approximate, :unspecified].each do |method|
|
11
11
|
it "responds to #{method}" do
|
12
|
-
Date.new.respond_to?(method).
|
12
|
+
expect(Date.new.respond_to?(method)).to eq(true)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
@@ -22,19 +22,19 @@ describe 'Date/DateTime' do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it 'copies uncertainty' do
|
25
|
-
date.dup.
|
25
|
+
expect(date.dup).to be_uncertain
|
26
26
|
end
|
27
27
|
|
28
28
|
it 'copies approximate' do
|
29
|
-
date.dup.
|
29
|
+
expect(date.dup).to be_approximate
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'copies precision' do
|
33
|
-
date.dup.precision.
|
33
|
+
expect(date.dup.precision).to eq(:month)
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'copies uncertainty by value' do
|
37
|
-
|
37
|
+
expect { date.dup.certain! }.not_to change { date.uncertain? }
|
38
38
|
end
|
39
39
|
|
40
40
|
end
|
@@ -43,12 +43,12 @@ describe 'Date/DateTime' do
|
|
43
43
|
let(:date) { Date.today }
|
44
44
|
|
45
45
|
it 'has day precision by default' do
|
46
|
-
date.
|
46
|
+
expect(date).to be_day_precision
|
47
47
|
end
|
48
48
|
|
49
49
|
it '#day_precison returns a new date object' do
|
50
|
-
date.day_precision.
|
51
|
-
date.day_precision.
|
50
|
+
expect(date.day_precision).not_to equal(date)
|
51
|
+
expect(date.day_precision).to eq(date)
|
52
52
|
end
|
53
53
|
|
54
54
|
end
|
@@ -58,38 +58,38 @@ describe 'Date/DateTime' do
|
|
58
58
|
let(:date) { Date.edtf('2004?') }
|
59
59
|
|
60
60
|
it 'returns a new date with the negated year' do
|
61
|
-
date.negate.year.
|
61
|
+
expect(date.negate.year).to eq(date.year * -1)
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'returns a new date with the same month' do
|
65
|
-
date.negate.month.
|
65
|
+
expect(date.negate.month).to eq(date.month)
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'returns a new date with the same day' do
|
69
|
-
date.negate.day.
|
69
|
+
expect(date.negate.day).to eq(date.day)
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'returns a new date with the same precision' do
|
73
|
-
date.negate.precision.
|
73
|
+
expect(date.negate.precision).to eq(date.precision)
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
77
|
describe '#succ' do
|
78
78
|
|
79
79
|
it 'the successor of 2004 is 2005' do
|
80
|
-
Date.edtf('2004').succ.year.
|
80
|
+
expect(Date.edtf('2004').succ.year).to eq(2005)
|
81
81
|
end
|
82
82
|
|
83
83
|
it 'the successor of 2004-03 is 2004-04' do
|
84
|
-
Date.edtf('2004-03').succ.strftime('%Y-%m').
|
84
|
+
expect(Date.edtf('2004-03').succ.strftime('%Y-%m')).to eq('2004-04')
|
85
85
|
end
|
86
86
|
|
87
87
|
it 'the successor of 2004-03-01 is 2004-03-02' do
|
88
|
-
Date.edtf('2004-03-01').succ.strftime('%Y-%m-%d').
|
88
|
+
expect(Date.edtf('2004-03-01').succ.strftime('%Y-%m-%d')).to eq('2004-03-02')
|
89
89
|
end
|
90
90
|
|
91
91
|
it "the successor of 1999 has year precision" do
|
92
|
-
Date.edtf('1999').succ.
|
92
|
+
expect(Date.edtf('1999').succ).to be_year_precision
|
93
93
|
end
|
94
94
|
|
95
95
|
end
|
@@ -97,7 +97,7 @@ describe 'Date/DateTime' do
|
|
97
97
|
describe '#next' do
|
98
98
|
|
99
99
|
it 'returns the successor when given no argument' do
|
100
|
-
Date.edtf('1999').next[0].year.
|
100
|
+
expect(Date.edtf('1999').next[0].year).to eq(2000)
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'returns an array of the next 3 elements when passed 3 as an argument' do
|
@@ -111,18 +111,18 @@ describe 'Date/DateTime' do
|
|
111
111
|
let(:date) { Date.edtf('2004-09?~') }
|
112
112
|
|
113
113
|
it 'returns a copy of the date if given empty option hash' do
|
114
|
-
date.change({}).
|
114
|
+
expect(date.change({})).to eq(date)
|
115
115
|
end
|
116
116
|
|
117
117
|
it 'the returned copy is not identical at object level' do
|
118
|
-
date.change({}).
|
118
|
+
expect(date.change({})).not_to equal(date)
|
119
119
|
end
|
120
120
|
|
121
121
|
describe 'when given a new year' do
|
122
122
|
let(:changeset) { { :year => 1999 } }
|
123
123
|
|
124
124
|
it 'returns a new date instance with the changed year' do
|
125
|
-
date.change(changeset).year.
|
125
|
+
expect(date.change(changeset).year).to eq(1999)
|
126
126
|
end
|
127
127
|
|
128
128
|
end
|
@@ -131,13 +131,13 @@ describe 'Date/DateTime' do
|
|
131
131
|
let(:changeset) { { :precision => :year } }
|
132
132
|
|
133
133
|
it 'returns a new date instance with the changed precision' do
|
134
|
-
date.change(changeset).precision.
|
134
|
+
expect(date.change(changeset).precision).to eq(:year)
|
135
135
|
end
|
136
136
|
|
137
137
|
end
|
138
138
|
|
139
139
|
it 'copies extended values by value' do
|
140
|
-
|
140
|
+
expect { date.change({}).approximate! }.not_to change { date.approximate? }
|
141
141
|
end
|
142
142
|
|
143
143
|
end
|
@@ -146,22 +146,22 @@ describe 'Date/DateTime' do
|
|
146
146
|
|
147
147
|
let(:date) { Date.new }
|
148
148
|
|
149
|
-
it { Date.new.
|
149
|
+
it { expect(Date.new).not_to be_uncertain }
|
150
150
|
|
151
151
|
[:year, :month, :day].each do |part|
|
152
152
|
it "should not be uncertain by default (#{part})" do
|
153
|
-
Date.new.uncertain?(part).
|
153
|
+
expect(Date.new.uncertain?(part)).to eq(false)
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should be uncertain if set to uncertain (#{part})" do
|
157
157
|
date.uncertain.send("#{part}=", true)
|
158
|
-
date.uncertain?(part).
|
158
|
+
expect(date.uncertain?(part)).to eq(true)
|
159
159
|
end
|
160
160
|
|
161
161
|
([:year, :month, :day] - [part]).each do |other|
|
162
162
|
it "#{other} should not be uncertain if #{part} is uncertain" do
|
163
163
|
date.uncertain.send("#{part}=", true)
|
164
|
-
date.uncertain?(other).
|
164
|
+
expect(date.uncertain?(other)).to eq(false)
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
@@ -172,11 +172,11 @@ describe 'Date/DateTime' do
|
|
172
172
|
describe 'uncertain/approximate hash' do
|
173
173
|
|
174
174
|
it 'is 0 by default' do
|
175
|
-
Date.new().send(:ua_hash).
|
175
|
+
expect(Date.new().send(:ua_hash)).to eq(0)
|
176
176
|
end
|
177
177
|
|
178
178
|
it 'is 8 for approximate year' do
|
179
|
-
Date.new.approximate!(:year).send(:ua_hash).
|
179
|
+
expect(Date.new.approximate!(:year).send(:ua_hash)).to eq(8)
|
180
180
|
end
|
181
181
|
|
182
182
|
end
|
@@ -188,15 +188,15 @@ describe 'Date/DateTime' do
|
|
188
188
|
describe '#values' do
|
189
189
|
|
190
190
|
it 'returns [2011,12,24] for christmas' do
|
191
|
-
xmas.values.
|
191
|
+
expect(xmas.values).to eq([2011,12,24])
|
192
192
|
end
|
193
193
|
|
194
194
|
it 'returns [2011,12] for christmas with month precision' do
|
195
|
-
xmas.month_precision!.values.
|
195
|
+
expect(xmas.month_precision!.values).to eq([2011,12])
|
196
196
|
end
|
197
197
|
|
198
198
|
it 'returns [2011] for christmas with year precision' do
|
199
|
-
xmas.year_precision!.values.
|
199
|
+
expect(xmas.year_precision!.values).to eq([2011])
|
200
200
|
end
|
201
201
|
|
202
202
|
end
|
@@ -204,15 +204,15 @@ describe 'Date/DateTime' do
|
|
204
204
|
describe '#<=>' do
|
205
205
|
|
206
206
|
it '2009-12-24 should be less than 2011-12-31' do
|
207
|
-
Date.new(2009,12,24).
|
207
|
+
expect(Date.new(2009,12,24)).to be < Date.new(2011,12,31)
|
208
208
|
end
|
209
209
|
|
210
210
|
it '2009-12-24 should be less than 2011-12' do
|
211
|
-
Date.new(2009,12,24).
|
211
|
+
expect(Date.new(2009,12,24)).to be < Date.edtf('2011-12')
|
212
212
|
end
|
213
213
|
|
214
214
|
it '2009-12-24 should be less than 2011' do
|
215
|
-
Date.new(2009,12,24).
|
215
|
+
expect(Date.new(2009,12,24)).to be < Date.edtf('2011')
|
216
216
|
end
|
217
217
|
|
218
218
|
|
@@ -221,20 +221,20 @@ describe 'Date/DateTime' do
|
|
221
221
|
describe '#==' do
|
222
222
|
|
223
223
|
it "xmas and new year's eve are not equal" do
|
224
|
-
xmas.
|
224
|
+
expect(xmas).not_to eq(new_years_eve)
|
225
225
|
end
|
226
226
|
|
227
227
|
it "xmas and new year's eve are equal using month percision" do
|
228
|
-
xmas.month_precision
|
228
|
+
expect(xmas.month_precision!).to eq(new_years_eve.month_precision!)
|
229
229
|
end
|
230
230
|
|
231
231
|
it "xmas and january 24 are not equal using month percision" do
|
232
|
-
xmas.month_precision
|
232
|
+
expect(xmas.month_precision!).not_to eq(xmas.month_precision!.next)
|
233
233
|
end
|
234
234
|
|
235
235
|
|
236
236
|
it "xmas and new year's eve are equal using year percision" do
|
237
|
-
xmas.year_precision
|
237
|
+
expect(xmas.year_precision!).to eq(new_years_eve.year_precision!)
|
238
238
|
end
|
239
239
|
|
240
240
|
end
|