edtf 4.0.0 → 4.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68cf5a59b655ee503dfda487dd719422299c40ee1744baeb93605b5b08bd0c9f
4
- data.tar.gz: 2b8e4a4e41c46f9607057c2b61720d3fce35acae2afa73ea112ff3bfdc03dc5e
3
+ metadata.gz: da8f3ed7204b150a35b07df4507742e265a9e78684956a824a1803da966c8fc8
4
+ data.tar.gz: 612e35c7e583ca68a51cc81f22314c60ac7aaefbffde79e243b2de428ded3589
5
5
  SHA512:
6
- metadata.gz: 34e2bf30004ba2eb3389d69fd9244cff29ddcf915e477e894b513116365efb0862585d2fa078786d5f40c22cfbddfec23d26a361886be0a78013f1b4457e0b93
7
- data.tar.gz: fa871f182f1ec791e36946c0a8a5127323c6b4da1aa6915b625f180a2e0698d586f6452d4d969f2d9f4345195838a92a4ee6414068354334f79a27ceb6fc0000
6
+ metadata.gz: a71e612f246046dc6d6706834b5fbb0a57b68bcb248eaa2e2a761af6b07c6df169492ba607c82a3df2c2187be6896fa239437b70c34a009e8b7e3324f8501520
7
+ data.tar.gz: b8caf26e31f0b665e2e4b1ffd7ac464ef8339c879a4b363a06b89f38b1823586e7bb2797ded205cb721a31dd4bbe5e7c3ac16e1e749c45a9a39e6d516ae884f5
data/README.md CHANGED
@@ -29,17 +29,52 @@ because of many functional overlaps
29
29
  ### ISO 8601-2
30
30
 
31
31
  A variation of EDTF has been adopted by the ISO 8601-2 standard.
32
- EDTF-Ruby does not support this new version of EDTF yet,
33
- but if you are curious,
34
- [EDTF.js](https://github.com/inukshuk/edtf.js), an ES6 implementation
35
- is already available.
32
+ EDTF-Ruby 5.0 will switch to this version of EDTF.
33
+
34
+ Preparing for EDTF-Ruby 5.0
35
+ ---------------------------
36
+ EDTF-Ruby 5.0 will accept the older draft syntax
37
+ only in an opt-in legacy mode, and print ISO 8601-2 syntax only.
38
+ Version 4.1 helps you prepare.
39
+
40
+ Enable deprecation warnings
41
+ to see which input uses legacy syntax
42
+ and which methods will go away:
43
+
44
+ $ RUBYOPT=-W:deprecated bundle exec rake
45
+ EDTF "2004/open": `open` is legacy syntax, use `..`
46
+
47
+ In code, use `Warning[:deprecated] = true` instead.
48
+ To audit stored dates without warnings, use `EDTF.legacy?`,
49
+ e.g. to print each line of a file that uses legacy syntax:
50
+
51
+ $ ruby -redtf -ne 'print if EDTF.legacy?($_.chomp)' dates.txt
52
+
53
+ You can switch most legacy syntax to ISO 8601-2 right away.
54
+ Parentheses and masked precision (`198x`) have no replacement in 4.1;
55
+ EDTF-Ruby 5.0 converts them in legacy mode.
56
+ Calendars and season qualifiers (`^…`) are not part of ISO 8601-2;
57
+ EDTF-Ruby 5.0 converts hemispheres to seasons (`2001-21^southern` to `2001-29`).
58
+
59
+ In your code:
60
+ * Replace `Date.edtf` and `Date.edtf!`
61
+ with `EDTF.parse`, `EDTF.parse!`, or `EDTF()`.
62
+ `EDTF()` converts strings, times, and dates to EDTF objects,
63
+ raising an error if it cannot.
64
+ * EDTF objects will be immutable.
65
+ Instead of the bang! methods, use `with` for a modified copy:
66
+
67
+ > EDTF('2004-06-11').with(precision: :year, approximate: :year).edtf
68
+ => "2004~"
69
+
70
+ * `XXXX` will be a date with an unspecified year instead of an `EDTF::Unknown`.
36
71
 
37
72
  Quickstart
38
73
  ----------
39
74
  EDTF Ruby is implemented as an extension to the regular Ruby date/time classes.
40
- You can parse EDTF strings either using `Date.edtf` or `EDTF.parse`
41
- (both methods come with an alternative bang! version,
42
- that will raise an error if the string cannot be parsed
75
+ You can parse EDTF strings using `EDTF.parse`
76
+ (or its bang! version `EDTF.parse!`, or `EDTF()`,
77
+ which will raise an error if the string cannot be parsed
43
78
  instead of silently returning nil);
44
79
  if given a valid EDTF string
45
80
  the return value will either be an (extended) `Date`,
@@ -130,7 +165,7 @@ whilst uncertain could be something completely different
130
165
  Here are a few examples of how you can access
131
166
  the uncertain/approximate state of dates in Ruby:
132
167
 
133
- > d = Date.edtf('1984?')
168
+ > d = EDTF('1984?')
134
169
  > d.uncertain?
135
170
  => true
136
171
  > d.uncertain? :year
@@ -151,7 +186,7 @@ In addition, EDTF supports *unspecified* date parts.
151
186
  EDTF-Ruby provides parsing for both the draft and finalized EDTF specifications,
152
187
  with output meeting the requirements of the final specification:
153
188
 
154
- > d = Date.edtf('1999-03-XX')
189
+ > d = EDTF('1999-03-XX')
155
190
  > d.unspecified?
156
191
  => true
157
192
  > d.unspecified? :year
@@ -162,7 +197,7 @@ with output meeting the requirements of the final specification:
162
197
  > d.edtf
163
198
  => "1999-XX-XX"
164
199
 
165
- > draft_d = Date.edtf('1999-03-uu')
200
+ > draft_d = EDTF('1999-03-uu') # legacy syntax
166
201
  > draft_d.edtf
167
202
  => "1999-03-XX"
168
203
 
@@ -172,12 +207,12 @@ do not factor into date calculations
172
207
 
173
208
  EDTF long or scientific years are mapped to normal date instances.
174
209
 
175
- > Date.edtf('y-17e7').year
210
+ > EDTF('Y-17E7').year
176
211
  => -170000000
177
212
 
178
213
  When printing date strings, EDTF-Ruby will try to avoid nested parentheses:
179
214
 
180
- > Date.edtf("(1999-(02)~-23)?").edtf
215
+ > EDTF("(1999-(02)~-23)?").edtf
181
216
  => "1999?-(02)?~-23?"
182
217
 
183
218
 
@@ -188,7 +223,7 @@ the EDTF-Ruby parser will return an instance of `EDTF::Interval`;
188
223
  intervals mimic regular Ruby ranges,
189
224
  but offer additional functionality.
190
225
 
191
- > d = Date.edtf('1984-06?/2004-08?')
226
+ > d = EDTF('1984-06?/2004-08?')
192
227
  > d.from.uncertain?
193
228
  => true
194
229
  > d.include?(Date.new(1987,04,13))
@@ -215,7 +250,7 @@ There are 243 months between 1984-06-01 and 2004-08-31.
215
250
  Here we iterate through the interval in 36-month steps
216
251
  and map each date to the year.
217
252
 
218
- > Date.edtf('1582-10-01/1582-10-31').length
253
+ > EDTF('1582-10-01/1582-10-31').length
219
254
  => 31
220
255
 
221
256
  This interval has day precision,
@@ -225,9 +260,9 @@ so the ten days skipped by the Gregorian calendar reform are included.
225
260
 
226
261
  Intervals can be open or have unknown start or end dates.
227
262
 
228
- > Date.edtf('2004/open').open?
263
+ > EDTF('2004/..').open?
229
264
  => true
230
- > Date.edtf('2004/open').cover?(Date.today)
265
+ > EDTF('2004/..').cover?(Date.today)
231
266
  => true
232
267
 
233
268
  ### Sets
@@ -238,7 +273,7 @@ or inclusive lists.
238
273
  In EDTF-Ruby, these are covered by the class `EDTF::Set`
239
274
  and the `choice` attribute.
240
275
 
241
- > s = Date.edtf('{1667,1668, 1670..1672}')
276
+ > s = EDTF('{1667,1668, 1670..1672}')
242
277
  > s.choice?
243
278
  => false
244
279
  > s.choice!
@@ -249,9 +284,9 @@ As you can see above,
249
284
  EDTF-Ruby remembers which parts of the set were specified as a range;
250
285
  ranges are however enumerated for membership tests:
251
286
 
252
- > s.include?(Date.edtf('1669'))
287
+ > s.include?(EDTF('1669'))
253
288
  => false
254
- > s.include?(Date.edtf('1671'))
289
+ > s.include?(EDTF('1671'))
255
290
  => true
256
291
 
257
292
  Even though we're still aware
@@ -282,7 +317,7 @@ to define decades or centuries.
282
317
  EDTF-Ruby maps these to dedicated intervals
283
318
  which always cover 10 or 100 years, respectively.
284
319
 
285
- > d = Date.edtf!('196x')
320
+ > d = EDTF('196x')
286
321
  > d.edtf
287
322
  => "196x"
288
323
  > d.class
@@ -306,7 +341,7 @@ which allow you to access the seasons by the names
306
341
  spring, summer, autumn (or fall), and winter, respectively.
307
342
  You can also use the more neutral taxonomy of first, second, third, fourth.
308
343
 
309
- > w = Date.edtf!('2003-24')
344
+ > w = EDTF('2003-24')
310
345
  > w.winter?
311
346
  => true
312
347
  > s = w.succ
data/lib/edtf/date.rb CHANGED
@@ -24,13 +24,13 @@ class Date
24
24
  end
25
25
 
26
26
  def edtf(input, options = {})
27
- edtf!(input, options)
28
- rescue
29
- nil
27
+ warn '`Date.edtf` is deprecated, use `EDTF.parse`', category: :deprecated, uplevel: 1
28
+ ::EDTF.parse(input, options)
30
29
  end
31
30
 
32
31
  def edtf!(input, options = {})
33
- ::EDTF::Parser.new(options).parse!(input)
32
+ warn '`Date.edtf!` is deprecated, use `EDTF.parse!` or `EDTF()`', category: :deprecated, uplevel: 1
33
+ ::EDTF.parse!(input, options)
34
34
  end
35
35
  end
36
36
 
@@ -80,6 +80,17 @@ class Date
80
80
  end
81
81
 
82
82
 
83
+ # Returns a copy with the given EDTF attributes replaced.
84
+ def with(precision: self.precision,
85
+ uncertain: PRECISION.select { |p| uncertain?(p) },
86
+ approximate: PRECISION.select { |p| approximate?(p) })
87
+ date = dup
88
+ date.precision = precision
89
+ date.uncertain.certain!.uncertain!(uncertain)
90
+ date.approximate.certain!.uncertain!(approximate)
91
+ date
92
+ end
93
+
83
94
  # Returns this Date's precision.
84
95
  def precision
85
96
  @precision ||= :day
data/lib/edtf/interval.rb CHANGED
@@ -11,12 +11,12 @@ module EDTF
11
11
  # containing exactly four dates with year precision. This is also reflected
12
12
  # in membership tests.
13
13
  #
14
- # Date.edtf('2003/2006').length -> 4
14
+ # EDTF('2003/2006').length -> 4
15
15
  #
16
- # Date.edtf('2003/2006').include? Date.edtf('2004') -> true
17
- # Date.edtf('2003/2006').include? Date.edtf('2004-03') -> false
16
+ # EDTF('2003/2006').include? EDTF('2004') -> true
17
+ # EDTF('2003/2006').include? EDTF('2004-03') -> false
18
18
  #
19
- # Date.edtf('2003/2006').cover? Date.edtf('2004-03') -> true
19
+ # EDTF('2003/2006').cover? EDTF('2004-03') -> true
20
20
  #
21
21
  class Interval
22
22