edtf 3.0.6 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/edtf/parser.y CHANGED
@@ -135,7 +135,6 @@ rule
135
135
  result = Date.new(val[0]).unspecified!([:day,:month])
136
136
  }
137
137
 
138
-
139
138
  level_1_interval : level_1_start '/' level_1_end {
140
139
  result = Interval.new(val[0], val[2])
141
140
  }
@@ -303,7 +302,6 @@ rule
303
302
  }
304
303
  ;
305
304
 
306
-
307
305
  partial_uncertain_or_approximate : pua_base
308
306
  | '(' pua_base ')' UA { result = uoa(val[1], val[3]) }
309
307
 
@@ -525,6 +523,8 @@ require 'strscan'
525
523
  [:Z, @src.matched]
526
524
  when @src.scan(/\?~/)
527
525
  [:UA, [:uncertain!, :approximate!]]
526
+ when @src.scan(/%/)
527
+ [:UA, [:uncertain!, :approximate!]]
528
528
  when @src.scan(/\?/)
529
529
  [:UA, [:uncertain!]]
530
530
  when @src.scan(/~/)
@@ -535,11 +535,13 @@ require 'strscan'
535
535
  [:UNKNOWN, :unknown]
536
536
  when @src.scan(/u/)
537
537
  [:U, @src.matched]
538
- when @src.scan(/x/i)
538
+ when @src.scan(/X/)
539
+ [:U, @src.matched]
540
+ when @src.scan(/x/)
539
541
  [:X, @src.matched]
540
- when @src.scan(/y/)
542
+ when @src.scan(/y/i)
541
543
  [:LONGYEAR, @src.matched]
542
- when @src.scan(/e/)
544
+ when @src.scan(/e/i)
543
545
  [:E, @src.matched]
544
546
  when @src.scan(/\+/)
545
547
  ['+', @src.matched]
@@ -5,17 +5,17 @@ module EDTF
5
5
  class Uncertainty < Struct.new(:year, :month, :day)
6
6
 
7
7
  attr_reader :hash_base
8
-
8
+
9
9
  def initialize(year = nil, month = nil, day = nil, hash_base = 1)
10
10
  @hash_base = hash_base
11
11
  super(year, month, day)
12
12
  end
13
-
13
+
14
14
  def hash_base=(base)
15
15
  @hash_map = false
16
16
  @hash_base = base
17
17
  end
18
-
18
+
19
19
  def uncertain?(parts = members)
20
20
  [*parts].any? { |p| !!send(p) }
21
21
  end
@@ -31,27 +31,27 @@ module EDTF
31
31
  [*parts].each { |p| send("#{p}=", false) }
32
32
  self
33
33
  end
34
-
34
+
35
35
  def eql?(other)
36
36
  hash == other.hash
37
37
  end
38
-
38
+
39
39
  def hash
40
40
  values.zip(hash_map).reduce(0) { |s, (v, h)| s + (v ? h : 0) }
41
41
  end
42
-
43
- private
44
-
42
+
43
+ private
44
+
45
45
  def hash_map
46
46
  @hash_map ||= (0...length).map { |i| hash_base << i }
47
47
  end
48
-
48
+
49
49
  end
50
50
 
51
51
 
52
52
  class Unspecified < Struct.new(:year, :month, :day)
53
53
 
54
- U = 'u'.freeze
54
+ X = 'X'.freeze
55
55
 
56
56
  def initialize
57
57
  super Array.new(4),Array.new(2), Array.new(2)
@@ -87,15 +87,15 @@ module EDTF
87
87
 
88
88
  def mask(values)
89
89
  if values[0] && values[0][0] == "-"
90
- values[0].delete!("-")
90
+ values[0].delete!("-")
91
91
  negative_year = true
92
92
  end
93
93
  results = values.zip(members.take(values.length)).map do |value, mask|
94
- value.split(//).zip(send(mask)).map { |v,m| m ? U : v }.join
94
+ value.split(//).zip(send(mask)).map { |v,m| m ? X : v }.join
95
95
  end
96
96
  results[0] = "-#{results[0]}" if negative_year
97
97
  results
98
98
  end
99
99
  end
100
100
 
101
- end
101
+ end
data/lib/edtf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module EDTF
2
- VERSION = '3.0.6'.freeze
2
+ VERSION = '3.1.0'.freeze
3
3
  end
data/lib/edtf.rb CHANGED
@@ -37,6 +37,7 @@ require 'forwardable'
37
37
  require 'enumerator'
38
38
  require 'set'
39
39
 
40
+ require 'active_support'
40
41
  require 'active_support/core_ext/date/calculations'
41
42
  require 'active_support/core_ext/date_time/calculations'
42
43
  require 'active_support/core_ext/time/calculations'
@@ -1,38 +1,38 @@
1
1
  module EDTF
2
2
  describe Parser do
3
3
  describe '#parse' do
4
-
4
+
5
5
  it 'parses simple dates "2001-02-03"' do
6
6
  expect(Parser.new.parse('2001-02-03').to_s).to eq('2001-02-03')
7
7
  end
8
-
8
+
9
9
  it 'parses negative years' do
10
10
  expect(Parser.new.parse('-2323').to_s).to eq('-2323-01-01')
11
11
  end
12
-
12
+
13
13
  it 'parses the negative year -2101 and sets the precision to :year' do
14
14
  expect(Parser.new.parse('-2101')).to be_year_precision
15
15
  end
16
-
16
+
17
17
  it 'parses year zero' do
18
18
  expect(Parser.new.parse('0000').to_s).to eq('0000-01-01')
19
19
  end
20
-
20
+
21
21
  it 'parses date/time with time zones' do
22
22
  expect(Parser.new.parse('2011-08-15T11:19:00+01:00').to_s).to eq('2011-08-15T11:19:00+01:00')
23
23
  end
24
-
24
+
25
25
  it 'parses simple intervals like "2007/2008"' do
26
26
  expect(Parser.new.parse('2007/2008')).to be_a(Interval)
27
27
  end
28
-
28
+
29
29
  it 'parses uncertain dates' do
30
- expect(Parser.new.parse('1984?')).to be_uncertain
30
+ expect(Parser.new.parse('1984?')).to be_uncertain
31
31
  expect(Parser.new.parse('1984')).to be_certain
32
32
  end
33
33
 
34
34
  it 'parses uncertain dates (day precision)' do
35
- expect(Parser.new.parse('1984-11-23?')).to be_uncertain
35
+ expect(Parser.new.parse('1984-11-23?')).to be_uncertain
36
36
  expect(Parser.new.parse('1984-11-23')).to be_certain
37
37
  end
38
38
 
@@ -41,22 +41,38 @@ module EDTF
41
41
  expect(Parser.new.parse('1984-01')).to be_precise
42
42
  end
43
43
 
44
- it 'parses uncertain approximate dates' do
44
+ it 'parses draft spec uncertain approximate dates' do
45
45
  expect(Parser.new.parse('1984?~')).to be_uncertain
46
46
  expect(Parser.new.parse('1984?~')).to be_approximate
47
47
  end
48
48
 
49
- it 'parses unspecified dates' do
49
+ it 'parses final spec uncertain approximate dates' do
50
+ expect(Parser.new.parse('1984%')).to be_uncertain
51
+ expect(Parser.new.parse('1984%')).to be_approximate
52
+ end
53
+
54
+ it 'parses draft spec unspecified dates' do
50
55
  expect(Parser.new.parse('199u')).to be_unspecified
51
56
  expect(Parser.new.parse('1999-uu-uu')).to be_unspecified
52
57
  expect(Parser.new.parse('199u-01')).to be_unspecified
53
58
  end
54
59
 
55
- it 'parses negative unspecified dates' do
60
+ it 'parses final spec unspecified dates' do
61
+ expect(Parser.new.parse('199X')).to be_unspecified
62
+ expect(Parser.new.parse('1999-XX-XX')).to be_unspecified
63
+ expect(Parser.new.parse('199X-01')).to be_unspecified
64
+ end
65
+
66
+ it 'parses draft spec negative unspecified dates' do
56
67
  expect(Parser.new.parse('-199u')).to be_unspecified
57
68
  expect(Parser.new.parse('-1999-uu-uu')).to be_unspecified
58
69
  end
59
70
 
71
+ it 'parses final spec negative unspecified dates' do
72
+ expect(Parser.new.parse('-199X')).to be_unspecified
73
+ expect(Parser.new.parse('-1999-XX-XX')).to be_unspecified
74
+ end
75
+
60
76
  it 'parses open intervals' do
61
77
  expect(Parser.new.parse('2004-01-01/open')).to be_open
62
78
  end
@@ -65,20 +81,20 @@ module EDTF
65
81
  expect(Parser.new.parse('2004-01-01/unknown')).to be_unknown_end
66
82
  expect(Parser.new.parse('unknown/2004-01-01')).to be_unknown_start
67
83
  end
68
-
84
+
69
85
  it 'parses intervals with uncertain or approximate dates' do
70
86
  expect(Parser.new.parse('1984-06-02?/2004-08-08~').from).to be_uncertain
71
87
  expect(Parser.new.parse('1984-06-02?/2004-08-08~').to).to be_approximate
72
88
  end
73
-
89
+
74
90
  it 'parses positive long years' do
75
91
  expect(Parser.new.parse('y170000002').year).to eq(170000002)
76
92
  end
77
-
93
+
78
94
  it 'parses negative long years' do
79
95
  expect(Parser.new.parse('y-170000002').year).to eq(-170000002)
80
96
  end
81
-
97
+
82
98
  it 'parses season codes' do
83
99
  expect(Parser.new.parse('2003-23')).to be_autumn
84
100
  end
@@ -86,14 +102,14 @@ module EDTF
86
102
  it 'parses calendar names' do
87
103
  expect(Parser.new.parse('2001-02-03^xyz').calendar).to eq('xyz')
88
104
  end
89
-
105
+
90
106
  it 'parses season qualifiers' do
91
107
  d = Parser.new.parse('2003-23^european')
92
108
  expect(d).to be_autumn
93
109
  expect(d).to be_qualified
94
110
  expect(d.qualifier).to eq('european')
95
111
  end
96
-
112
+
97
113
  it 'parses uncertain seasons' do
98
114
  expect(Parser.new.parse!('2003-23?')).to be_uncertain
99
115
  end
@@ -102,20 +118,26 @@ module EDTF
102
118
  expect(Parser.new.parse!('2003-23~')).to be_approximate
103
119
  end
104
120
 
105
- it 'parses uncertain and approximate seasons' do
121
+ it 'parses draft spec uncertain and approximate seasons' do
106
122
  expect(Parser.new.parse!('2003-23?~')).to be_uncertain
107
123
  expect(Parser.new.parse!('2003-23?~')).to be_approximate
108
124
  end
109
-
110
-
125
+
126
+ it 'parses final spec uncertain and approximate seasons' do
127
+ expect(Parser.new.parse!('2003-23%')).to be_uncertain
128
+ expect(Parser.new.parse!('2003-23%')).to be_approximate
129
+ end
130
+
111
131
  it 'parses positive scientific long years' do
112
132
  expect(Parser.new.parse('y17e7').year).to eq(170000000)
133
+ expect(Parser.new.parse('Y17E7').year).to eq(170000000)
113
134
  end
114
-
135
+
115
136
  it 'parses negative scientific long years' do
116
- expect(Parser.new.parse('y-17e7').year).to eq(-170000000)
137
+ expect(Parser.new.parse('y-17e7').year).to eq(-170000000)
138
+ expect(Parser.new.parse('Y-17E7').year).to eq(-170000000)
117
139
  end
118
-
140
+
119
141
  it 'parses masked precision date strings (decades)' do
120
142
  d = Parser.new.parse!('198x')
121
143
  expect(d).to be_cover(Date.new(1983,3,12))
@@ -127,17 +149,17 @@ module EDTF
127
149
  expect(d).to be_cover(Date.new(1848,1,14))
128
150
  expect(d).not_to be_cover(Date.new(1799,12,31))
129
151
  end
130
-
152
+
131
153
  it 'parses multiple dates (years)' do
132
154
  d = Parser.new.parse!('{1667,1668, 1670..1672}')
133
155
  expect(d.map(&:year)).to eq([1667,1668,1670,1671,1672])
134
156
  end
135
-
157
+
136
158
  it 'parses multiple dates (mixed years and months)' do
137
159
  d = Parser.new.parse!('{1960, 1961-12}')
138
160
  expect(d.map { |x| [x.year,x.month] }).to eq([[1960,1],[1961,12]])
139
161
  end
140
-
162
+
141
163
  it 'parses choice lists (One of the years 1667, 1668, 1670, 1671, 1672)' do
142
164
  d = Parser.new.parse!('[1667,1668, 1670..1672]')
143
165
  expect(d.map(&:year)).to eq([1667,1668,1670,1671,1672])
@@ -157,52 +179,95 @@ module EDTF
157
179
  d = Parser.new.parse!('[1760-01, 1760-02, 1760-12..]')
158
180
  expect(d.length).to eq(3)
159
181
  end
160
-
182
+
161
183
  it 'parses intern unspecified "199u-01-01"' do
162
- expect(Parser.new.parse!('199u-01-01').unspecified.to_s).to eq('sssu-ss-ss')
184
+ expect(Parser.new.parse!('199u-01-01').unspecified.to_s).to eq('sssX-ss-ss')
163
185
  end
164
-
186
+
165
187
  it 'parses intern unspecified "19uu-01-01"' do
166
- expect(Parser.new.parse!('19uu-01-01').unspecified.to_s).to eq('ssuu-ss-ss')
188
+ expect(Parser.new.parse!('19uu-01-01').unspecified.to_s).to eq('ssXX-ss-ss')
167
189
  end
168
190
 
169
191
  it 'parses intern unspecified "199u-uu-01"' do
170
- expect(Parser.new.parse!('199u-uu-01').unspecified.to_s).to eq('sssu-uu-ss')
192
+ expect(Parser.new.parse!('199u-uu-01').unspecified.to_s).to eq('sssX-XX-ss')
171
193
  end
172
-
194
+
173
195
  it 'parses intern unspecified "19uu-uu-01"' do
174
- expect(Parser.new.parse!('19uu-uu-01').unspecified.to_s).to eq('ssuu-uu-ss')
196
+ expect(Parser.new.parse!('19uu-uu-01').unspecified.to_s).to eq('ssXX-XX-ss')
175
197
  end
176
198
 
177
199
  it 'parses intern unspecified "199u-uu-uu"' do
178
- expect(Parser.new.parse!('199u-uu-uu').unspecified.to_s).to eq('sssu-uu-uu')
200
+ expect(Parser.new.parse!('199u-uu-uu').unspecified.to_s).to eq('sssX-XX-XX')
179
201
  end
180
-
202
+
181
203
  it 'parses intern unspecified "19uu-uu-uu"' do
182
- expect(Parser.new.parse!('19uu-uu-uu').unspecified.to_s).to eq('ssuu-uu-uu')
204
+ expect(Parser.new.parse!('19uu-uu-uu').unspecified.to_s).to eq('ssXX-XX-XX')
183
205
  end
184
206
 
185
207
  it 'parses intern unspecified "199u-01-uu"' do
186
- expect(Parser.new.parse!('199u-01-uu').unspecified.to_s).to eq('sssu-ss-uu')
208
+ expect(Parser.new.parse!('199u-01-uu').unspecified.to_s).to eq('sssX-ss-XX')
187
209
  end
188
-
210
+
189
211
  it 'parses intern unspecified "19uu-01-uu"' do
190
- expect(Parser.new.parse!('19uu-01-uu').unspecified.to_s).to eq('ssuu-ss-uu')
212
+ expect(Parser.new.parse!('19uu-01-uu').unspecified.to_s).to eq('ssXX-ss-XX')
191
213
  end
192
214
 
193
215
  it 'parses intern unspecified "1999-uu-01"' do
194
- expect(Parser.new.parse!('1999-uu-01').unspecified.to_s).to eq('ssss-uu-ss')
216
+ expect(Parser.new.parse!('1999-uu-01').unspecified.to_s).to eq('ssss-XX-ss')
195
217
  end
196
218
 
197
219
  it 'parses intern unspecified "2004-06-uu"' do
198
- expect(Parser.new.parse!('2004-06-uu').unspecified.to_s).to eq('ssss-ss-uu')
220
+ expect(Parser.new.parse!('2004-06-uu').unspecified.to_s).to eq('ssss-ss-XX')
199
221
  end
200
222
 
201
-
202
223
  it 'parses internal unspecified interval "2004-06-uu/2004-07-03"' do
203
224
  expect(Parser.new.parse!('2004-06-uu/2004-07-03').from).to eq(Date.new(2004,6,1))
204
225
  end
205
-
226
+
227
+ it 'parses intern unspecified "199X-01-01"' do
228
+ expect(Parser.new.parse!('199X-01-01').unspecified.to_s).to eq('sssX-ss-ss')
229
+ end
230
+
231
+ it 'parses intern unspecified "19XX-01-01"' do
232
+ expect(Parser.new.parse!('19XX-01-01').unspecified.to_s).to eq('ssXX-ss-ss')
233
+ end
234
+
235
+ it 'parses intern unspecified "199X-XX-01"' do
236
+ expect(Parser.new.parse!('199X-XX-01').unspecified.to_s).to eq('sssX-XX-ss')
237
+ end
238
+
239
+ it 'parses intern unspecified "19XX-XX-01"' do
240
+ expect(Parser.new.parse!('19XX-XX-01').unspecified.to_s).to eq('ssXX-XX-ss')
241
+ end
242
+
243
+ it 'parses intern unspecified "199X-XX-XX"' do
244
+ expect(Parser.new.parse!('199X-XX-XX').unspecified.to_s).to eq('sssX-XX-XX')
245
+ end
246
+
247
+ it 'parses intern unspecified "19XX-XX-XX"' do
248
+ expect(Parser.new.parse!('19XX-XX-XX').unspecified.to_s).to eq('ssXX-XX-XX')
249
+ end
250
+
251
+ it 'parses intern unspecified "199X-01-XX"' do
252
+ expect(Parser.new.parse!('199X-01-XX').unspecified.to_s).to eq('sssX-ss-XX')
253
+ end
254
+
255
+ it 'parses intern unspecified "19XX-01-XX"' do
256
+ expect(Parser.new.parse!('19XX-01-XX').unspecified.to_s).to eq('ssXX-ss-XX')
257
+ end
258
+
259
+ it 'parses intern unspecified "1999-XX-01"' do
260
+ expect(Parser.new.parse!('1999-XX-01').unspecified.to_s).to eq('ssss-XX-ss')
261
+ end
262
+
263
+ it 'parses intern unspecified "2004-06-XX"' do
264
+ expect(Parser.new.parse!('2004-06-XX').unspecified.to_s).to eq('ssss-ss-XX')
265
+ end
266
+
267
+ it 'parses internal unspecified interval "2004-06-XX/2004-07-03"' do
268
+ expect(Parser.new.parse!('2004-06-XX/2004-07-03').from).to eq(Date.new(2004,6,1))
269
+ end
270
+
206
271
  it 'parses "2004?-06-11": uncertain year; month, day known' do
207
272
  d = Parser.new.parse!('2004?-06-11')
208
273
  expect(d.uncertain?(:year)).to be true
@@ -215,7 +280,7 @@ module EDTF
215
280
  expect(d.approximate?(:month)).to be true
216
281
  expect(d.approximate?(:day)).to be false
217
282
  end
218
-
283
+
219
284
  it 'parses "2004-(06)?-11": uncertain month, year and day known' do
220
285
  d = Parser.new.parse!('2004-(06)?-11')
221
286
 
@@ -238,65 +303,65 @@ module EDTF
238
303
 
239
304
  it 'parses "2004-(06)?~": year known, month within year is approximate and uncertain' do
240
305
  d = Parser.new.parse!('2004-(06)?~')
241
-
306
+
242
307
  expect(d.approximate?(:year)).to be false
243
308
  expect(d.uncertain?(:year)).to be false
244
-
309
+
245
310
  expect(d.approximate?(:month)).to be true
246
311
  expect(d.uncertain?(:month)).to be true
247
-
312
+
248
313
  expect(d.approximate?(:day)).to be false
249
314
  expect(d.uncertain?(:day)).to be false
250
315
  end
251
316
 
252
317
  it 'parses "2004-(06-11)?": year known, month and day uncertain' do
253
318
  d = Parser.new.parse!('2004-(06-11)?')
254
-
319
+
255
320
  expect(d.approximate?(:year)).to be false
256
321
  expect(d.uncertain?(:year)).to be false
257
-
322
+
258
323
  expect(d.approximate?(:month)).to be false
259
324
  expect(d.uncertain?(:month)).to be true
260
-
325
+
261
326
  expect(d.approximate?(:day)).to be false
262
327
  expect(d.uncertain?(:day)).to be true
263
328
  end
264
329
 
265
330
  it 'parses "2004?-06-(11)~": year uncertain, month known, day approximate' do
266
331
  d = Parser.new.parse('2004?-06-(11)~')
267
-
332
+
268
333
  expect(d.approximate?(:year)).to be false
269
334
  expect(d.uncertain?(:year)).to be true
270
-
335
+
271
336
  expect(d.approximate?(:month)).to be false
272
337
  expect(d.uncertain?(:month)).to be false
273
-
338
+
274
339
  expect(d.approximate?(:day)).to be true
275
340
  expect(d.uncertain?(:day)).to be false
276
341
  end
277
-
342
+
278
343
  it 'parses "(2004-(06)~)?": year uncertain and month is both uncertain and approximate' do
279
344
  d = Parser.new.parse!('(2004-(06)~)?')
280
-
345
+
281
346
  expect(d.approximate?(:year)).to be false
282
347
  expect(d.uncertain?(:year)).to be true
283
-
348
+
284
349
  expect(d.approximate?(:month)).to be true
285
350
  expect(d.uncertain?(:month)).to be true
286
-
351
+
287
352
  expect(d.approximate?(:day)).to be false
288
353
  expect(d.uncertain?(:day)).to be false
289
354
  end
290
355
 
291
356
  it 'parses "2004~-(06)?-01~": year and day approximate, month uncertain' do
292
357
  d = Parser.new.parse!("2004~-(06)?-01~")
293
-
358
+
294
359
  expect(d.approximate?(:year)).to be true
295
360
  expect(d.uncertain?(:year)).to be false
296
-
361
+
297
362
  expect(d.approximate?(:month)).to be false
298
363
  expect(d.uncertain?(:month)).to be true
299
-
364
+
300
365
  expect(d.approximate?(:day)).to be true
301
366
  expect(d.uncertain?(:day)).to be false
302
367
  end
@@ -306,10 +371,10 @@ module EDTF
306
371
 
307
372
  expect(d.approximate?(:year)).to be true
308
373
  expect(d.uncertain?(:year)).to be false
309
-
374
+
310
375
  expect(d.approximate?(:month)).to be false
311
376
  expect(d.uncertain?(:month)).to be true
312
-
377
+
313
378
  expect(d.approximate?(:day)).to be true
314
379
  expect(d.uncertain?(:day)).to be true
315
380
  end
@@ -319,10 +384,10 @@ module EDTF
319
384
 
320
385
  expect(d.approximate?(:year)).to be true
321
386
  expect(d.uncertain?(:year)).to be false
322
-
387
+
323
388
  expect(d.approximate?(:month)).to be false
324
389
  expect(d.uncertain?(:month)).to be true
325
-
390
+
326
391
  expect(d.approximate?(:day)).to be true
327
392
  expect(d.uncertain?(:day)).to be true
328
393
  end
@@ -332,10 +397,10 @@ module EDTF
332
397
 
333
398
  expect(d.approximate?(:year)).to be true
334
399
  expect(d.uncertain?(:year)).to be false
335
-
400
+
336
401
  expect(d.approximate?(:month)).to be false
337
402
  expect(d.uncertain?(:month)).to be true
338
-
403
+
339
404
  expect(d.approximate?(:day)).to be false
340
405
  expect(d.uncertain?(:day)).to be false
341
406
  end
@@ -346,16 +411,16 @@ module EDTF
346
411
 
347
412
  expect(d.approximate?(:year)).to be true
348
413
  expect(d.uncertain?(:year)).to be true
349
-
414
+
350
415
  expect(d.approximate?(:month)).to be false
351
416
  expect(d.uncertain?(:month)).to be true
352
-
417
+
353
418
  expect(d.approximate?(:day)).to be false
354
419
  expect(d.uncertain?(:day)).to be false
355
420
 
356
421
  end
357
422
 
358
-
423
+
359
424
 
360
425
  end
361
426
  end