Skip to content

Commit 709e998

Browse files
committed
#26 Update README.md for removal of datetime limits
- explain the switch from `datetime` module objects to using `time.struct_time` as responses to upper/lower strict/fuzzy methods - update example code to reflect the change - remove reference to `date.MIN` and `date.MAX` restrictions.
1 parent 742dad8 commit 709e998

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

README.rst

+54-27
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ To use
3737

3838
# Derive Python date objects
3939
# 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))
4242
# 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))
4545

4646
# Date intervals
4747
>>> interval = parse_edtf("1979-08~/open")
@@ -50,9 +50,9 @@ To use
5050
# Intervals have lower and upper EDTF objects.
5151
>>> interval.lower, interval.upper
5252
(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'.
5656
[Today's date]
5757

5858
# Date collections
@@ -296,6 +296,31 @@ few different Python dates, depending on the circumstance. Generally, Python
296296
dates are used for sorting and filtering, and are not displayed directly to
297297
users.
298298

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+
299324
``lower_strict`` and ``upper_strict``
300325
-------------------------------------
301326

@@ -308,9 +333,21 @@ natural sort order. In a descending sort (most recent first), sort by
308333
``upper_strict``::
309334

310335
>>> 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
312345
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())
314351
datetime.date(1912, 4, 30)
315352

316353
``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
330367
it is approximate at the year scale, it is padded by a year::
331368

332369
>>> 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)
337374

338375
>>> 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)
343380

344381
One can interpret uncertain or approximate dates as 'plus or minus a
345382
[level of precision]'.
346383

347384
If a date is both uncertain __and__ approximate, the padding is applied twice,
348385
i.e. it gets 100% * 2 padding, or 'plus or minus two [levels of precision]'.
349386

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-
360387
Seasons
361388
-------
362389

0 commit comments

Comments
 (0)