@@ -37,11 +37,11 @@ To use
37
37
38
38
# Derive Python date objects
39
39
# lower and upper bounds that strictly adhere to the given range
40
- >>> e.lower_strict(), e.upper_strict()
41
- (datetime.date (1979, 8, 1), datetime.date (1979, 8, 31))
40
+ >>> e.lower_strict()[:3] , e.upper_strict()[:3]
41
+ ((1979, 8, 1), (1979, 8, 31))
42
42
# lower and upper bounds that are padded if there's indicated uncertainty
43
- >>> e.lower_fuzzy(), e.upper_fuzzy()
44
- (datetime.date (1979, 7, 1), datetime.date (1979, 9, 30))
43
+ >>> e.lower_fuzzy()[:3] , e.upper_fuzzy()[:3]
44
+ ((1979, 7, 1), (1979, 9, 30))
45
45
46
46
# Date intervals
47
47
>>> interval = parse_edtf("1979-08~/open")
50
50
# Intervals have lower and upper EDTF objects.
51
51
>>> interval.lower, interval.upper
52
52
(UncertainOrApproximate: '1979-08~', UncertainOrApproximate: 'open')
53
- >>> interval.lower.upper_strict()
54
- datetime.date (1979, 8, 31)
55
- >>> interval.upper.lower_strict() #'open' is interpreted to mean 'still happening'.
53
+ >>> interval.lower.upper_strict()[:3]
54
+ (1979, 8, 31)
55
+ >>> interval.upper.lower_strict() # 'open' is interpreted to mean 'still happening'.
56
56
[Today's date]
57
57
58
58
# Date collections
@@ -296,6 +296,31 @@ few different Python dates, depending on the circumstance. Generally, Python
296
296
dates are used for sorting and filtering, and are not displayed directly to
297
297
users.
298
298
299
+
300
+ ``struct_time `` date representation
301
+ -----------------------------------
302
+
303
+ Because Python's ``datetime `` module does not support dates out side the range
304
+ 1 AD to 9999 AD we return dates as `time.struct_time ` objects by default
305
+ instead of the ``datetime.date `` or ``datetime.datetime `` objects you might
306
+ expect.
307
+
308
+ The ``struct_time `` representation is more difficult to work with, but can be
309
+ sorted as-is which is the primary use-case, and can be converted relatively
310
+ easily to ``date `` or ``datetime `` objects (provided the year is within 1 to
311
+ 9999 AD) or to date objects in more flexible libraries like
312
+ `astropy.time <http://docs.astropy.org/en/stable/time/index.html >`_
313
+ for years outside these bounds.
314
+
315
+ If you are sure you are working with dates within the range supported by
316
+ Python's ``datetime `` module, you can get these more convenient objects using
317
+ the ``edtf.struct_time_to_date `` and ``edtf.struct_time_to_datetime ``
318
+ functions.
319
+
320
+ NOTE: This library previously did return ``date `` and ``datetime `` objects
321
+ from methods by default before we switched to ``struct_time ``. See ticket
322
+ `<https://github.com/ixc/python-edtf/issues/26 >`_.
323
+
299
324
``lower_strict `` and ``upper_strict ``
300
325
-------------------------------------
301
326
@@ -308,9 +333,21 @@ natural sort order. In a descending sort (most recent first), sort by
308
333
``upper_strict ``::
309
334
310
335
>>> e = parse_edtf('1912-04~')
311
- >>> e.lower_strict()
336
+
337
+ >>> e.lower_strict() # Returns struct_time
338
+ >>> time.struct_time(tm_year=1912, tm_mon=4, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=0, tm_isdst=-1)
339
+
340
+ >>> e.lower_strict()[:3] # Show only interesting parts of struct_time
341
+ (1912, 4, 01)
342
+
343
+ >>> from edtf import struct_time_to_date
344
+ >>> struct_time_to_date(e.lower_strict()) # Convert to date
312
345
datetime.date(1912, 4, 01)
313
- >>> e.upper_strict()
346
+
347
+ >>> e.upper_strict()[:3]
348
+ (1912, 4, 30)
349
+
350
+ >>> struct_time_to_date(e.upper_strict())
314
351
datetime.date(1912, 4, 30)
315
352
316
353
``lower_fuzzy `` and ``upper_fuzzy ``
@@ -330,33 +367,23 @@ is, if a date is approximate at the month scale, it is padded by a month. If
330
367
it is approximate at the year scale, it is padded by a year::
331
368
332
369
>>> e = parse_edtf('1912-04~')
333
- >>> e.lower_fuzzy() # padding is 100% of a month
334
- datetime.date (1912, 3, 1)
335
- >>> e.upper_fuzzy()
336
- datetime.date (1912, 5, 30)
370
+ >>> e.lower_fuzzy()[:3] # padding is 100% of a month
371
+ (1912, 3, 1)
372
+ >>> e.upper_fuzzy()[:3]
373
+ (1912, 5, 30)
337
374
338
375
>>> e = parse_edtf('1912~')
339
- >>> e.lower_fuzzy() # padding is 100% of a year
340
- datetime.date (1911, 1, 1)
341
- >>> e.upper_fuzzy()
342
- datetime.date (1913, 12, 31)
376
+ >>> e.lower_fuzzy()[:3] # padding is 100% of a year
377
+ (1911, 1, 1)
378
+ >>> e.upper_fuzzy()[:3]
379
+ (1913, 12, 31)
343
380
344
381
One can interpret uncertain or approximate dates as 'plus or minus a
345
382
[level of precision]'.
346
383
347
384
If a date is both uncertain __and__ approximate, the padding is applied twice,
348
385
i.e. it gets 100% * 2 padding, or 'plus or minus two [levels of precision]'.
349
386
350
- Long years
351
- ----------
352
-
353
- Since EDTF covers a much greater timespan than Python ``date `` objects, it is
354
- easy to exceed the bounds of valid Python ``date``s. In this case, the returned
355
- dates are clamped to ``date.MIN `` and ``date.MAX ``.
356
-
357
- Future revisions will include numerical interpretations of dates for better
358
- sortability.
359
-
360
387
Seasons
361
388
-------
362
389
0 commit comments