edtf2


Nameedtf2 JSON
Version 5.0.0 PyPI version JSON
download
home_pagehttps://github.com/saw-leipzig/python-edtf
SummaryPython implementation of Library of Congress EDTF (Extended Date Time Format) specification
upload_time2023-10-04 06:32:19
maintainer
docs_urlNone
authorSabine Müller
requires_python
licenseMIT
keywords edtf
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            edtf2
=====

An implementation of EDTF format in Python, together with utility functions for parsing natural language date texts, and converting EDTF dates to related Python `date` or `struct_time` objects.

See http://www.loc.gov/standards/datetime/ for the current draft specification.

This project is based on python-edtf and was developed to include the newest specification

## To install

    pip install edtf2

## To use

    >>> from edtf2 import parse_edtf
    # Parse an EDTF string to an EDTFObject
    >>> e = parse_edtf("1979-08~") # approx August 1979
    >>> e
    UncertainOrApproximate: '1979-08~'
    # normalised string representation (some different EDTF strings have identical meanings)
    >>> unicode(e)
    u'1979-08~'

    # Derive Python date objects
    # lower and upper bounds that strictly adhere to the given range
    >>> e.lower_strict()[:3], e.upper_strict()[:3]
    ((1979, 8, 1), (1979, 8, 31))
    # lower and upper bounds that are padded if there's indicated uncertainty
    >>> e.lower_fuzzy()[:3], e.upper_fuzzy()[:3]
    ((1979, 7, 1), (1979, 9, 30))

    # Date intervals
    >>> interval = parse_edtf("1979-08~/..")
    >>> interval
    Level1Interval: '1979-08~/..'
    # Intervals have lower and upper EDTF objects.
    >>> interval.lower, interval.upper
    (UncertainOrApproximate: '1979-08~', UnspecifiedIntervalSection: '..')
    >>> interval.lower.lower_strict()[:3], interval.lower.upper_strict()[:3]
    ((1979, 8, 1), (1979, 8, 31))
    >>> interval.upper.upper_strict() # '..' is interpreted to mean open interval and is returning -/+ math.inf
    math.inf

    # Date collections
    >>> coll = parse_edtf('{1667,1668, 1670..1672}')
    >>> coll
    MultipleDates: '{1667, 1668, 1670..1672}'
    >>> coll.objects
    (Date: '1667', Date: '1668', Consecutives: '1670..1672')

The object returned by `parse_edtf()` is an instance of an `edtf2.parser.parser_classes.EDTFObject` subclass, depending on the type of date that was parsed. These classes are:

    # Level 0
    Date
    DateAndTime
    Interval

    # Level 1
    UncertainOrApproximate
    Unspecified
    Level1Interval
    UnspecifiedIntervalSection
    LongYear
    Season

    # Level 2
    PartialUncertainOrApproximate
    PartialUnspecified
    OneOfASet
    MultipleDates
    MaskedPrecision
    Level2Interval
    Level2Season
    ExponentialYear

All of these implement `upper/lower_strict/fuzzy()` methods to derive `struct_time` objects, except of UnspecifiedIntervalSection, that can also return math.inf value

The `*Interval` instances have `upper` and `lower` properties that are themselves `EDTFObject` instances.

`OneOfASet` and `MultipleDates` instances have an `objects` property that is a list of all of the EDTF dates parsed in the set or list.

## EDTF Specification Inclusions

The library includes implementation of levels 0, 1 and 2 of the EDTF spec.

Test coverage includes every example given in the spec table of features.

### Level 0 ISO 8601 Features

* Date:

        >>> parse_edtf('1979-08') # August 1979
        Date: '1979-08'

* Date and Time:

        >>> parse_edtf('2004-01-01T10:10:10+05:00')
        DateAndTime: '2004-01-01T10:10:10+05:00'

* Interval (start/end):

        >>> parse_edtf('1979-08-28/1979-09-25') # From August 28 to September 25 1979
        Interval: '1979-08-28/1979-09-25'

### Level 1 Extensions

* Uncertain/Approximate dates:
      
        >>> parse_edtf('1979-08-28~') # Approximately August 28th 1979
        UncertainOrApproximate: '1979-08-28~'

* Unspecified dates:

        >>> parse_edtf('1979-08-XX') # An unknown day in August 1979
        Unspecified: '1979-08-XX'
        >>> parse_edtf('1979-XX') # Some month in 1979
        Unspecified: '1979-XX'

* Extended intervals:

        >>> parse_edtf('1984-06-02?/2004-08-08~')
        Level1Interval: '1984-06-02?/2004-08-08~'

* Years exceeding four digits:

        >>> parse_edtf('y-12000') # 12000 years BCE
        LongYear: 'y-12000'

* Season:

        >>> parse_edtf('1979-22') # Summer 1979
        Season: '1979-22'

### Level 2 Extensions

* Partial uncertain/approximate:

        >>> parse_edtf('(2011)-06-04~') # year certain, month/day approximate.
        # Note that the result text is normalized
        PartialUncertainOrApproximate: '2011-(06-04)~'

* Partial unspecified:

        >>> parse_edtf('1979-XX-28') # The 28th day of an uncertain month in 1979
        PartialUnspecified: '1979-XX-28'

* One of a set:

        >>> parse_edtf("[..1760-12-03,1762]")
        OneOfASet: '[..1760-12-03, 1762]'

* Multiple dates:

        >>> parse_edtf('{1667,1668, 1670..1672}')
        MultipleDates: '{1667, 1668, 1670..1672}'

* Masked precision:

        >>> parse_edtf('197x') # A date in the 1970s.
        MaskedPrecision: '197x'

* Level 2 Extended intervals:

        >>> parse_edtf('2004-06-(01)~/2004-06-(20)~')
        Level2Interval: '2004-06-(01)~/2004-06-(20)~'

* Year requiring more than 4 digits - exponential form:

        >>> parse_edtf('y-17e7')
        ExponentialYear: 'y-17e7'

### Natural language representation


The library includes a basic English natural language parser (it's not yet smart enough to work with occasions such as 'Easter', or in other languages):

    >>> from edtf2 import text_to_edtf
    >>> text_to_edtf("circa August 1979")
    '1979-08~'

Note that the result is a string, not an `ETDFObject`.

The parser can parse strings such as:

    'January 12, 1940' => '1940-01-12'
    '90' => '1990' #implied century
    'January 2008' => '2008-01'
    'the year 1800' => '1800'
    '10/7/2008' => '2008-10-07' # in a full-specced date, assume US ordering

    # uncertain/approximate
    '1860?' => '1860?'
    '1862 (uncertain)' => '1862?'
    'circa Feb 1812' => '1812-02~'
    'c.1860' => '1860~' #with or without .
    'ca1860' => '1860~'
    'approx 1860' => '1860~'

    # masked precision
    '1860s' => '186x' #186x has decade precision, 186u has year precision.
    '1800s' => '18xx' # without uncertainty indicators, assume century

    # masked precision + uncertainty
    'ca. 1860s' => '186x~'
    'circa 1840s' => '184x~'
    'ca. 1860s?' => '186x?~'
    'c1800s?' => '180x?~' # with uncertainty indicators, use the decade

    # unspecified parts
    'January 12' => 'XXXX-01-12'
    'January' => 'XXXX-01'
    '7/2008' => '2008-07'

    #seasons
    'Autumn 1872' => '1872-23'
    'Fall 1872' => '1872-23'

    # before/after
    'earlier than 1928' => 'unknown/1928'
    'later than 1928' => '1928/unknown'
    'before January 1928' => 'unknown/1928-01'
    'after about the 1920s' => '192x~/unknown'

    # unspecified
    'year in the 1860s' => '186u' #186x has decade precision, 186u has year precision.
    ('year in the 1800s', '18xu')
    'month in 1872' => '1872-XX'
    'day in January 1872' => '1872-01-XX'
    'day in 1872' => '1872-XX-XX'

    #centuries
    '1st century' => '00xx'
    '10c' => '09xx'
    '19th century?' => '18xx?'

    # just showing off now...
    'a day in about Spring 1849?' => '1849-21-XX?~'

    # simple ranges, which aren't as accurate as they could be. The parser is
    limited to only picking the first year range it finds.
    '1851-1852' => '1851/1852'
    '1851-1852; printed 1853-1854' => '1851/1852'
    '1851-52' => '1851/1852'
    '1856-ca. 1865' => '1856/1865~'
    '1860s-1870s' => '186x/187x'
    '1920s -early 1930s' => '192x/193x'
    '1938, printed 1940s-1950s' => '1938'


Generating natural text from an EDTF representation is a future goal.

### What assumptions does the natural text parser make when interpreting an ambiguous date?

* "1800s" is ambiguously a century or decade. If the given date is either uncertain or approximate, the decade interpretation is used. If the date is certain and precise, the century interpretation is used.

* If the century isn't specified (`EDTF(natural_text="the '70s")`), we imply the century to be "19" if the year is greater than the current year, otherwise we imply the century to be the current century.

* US-ordered dates (mm/dd/yyyy) are assumed by default in natural language.  To change this, set `DAY_FIRST` to True in settings.

* If a natural language groups dates with a '/', it's interpreted as "or" rather than "and". The resulting EDTF text is a list bracketed by `[]` ("one of these dates") rather than `{}` (all of these dates).


## Converting to and from Python dates


Since EDTF dates are often regions, and often imprecise, we need to use a few different Python dates, depending on the circumstance. Generally, Python dates are used for sorting and filtering, and are not displayed directly to users.


### `struct_time` date representation

Because Python's `datetime` module does not support dates out side the range 1 AD to 9999 AD we return dates as `time.struct_time` objects by default instead of the `datetime.date` or `datetime.datetime` objects you might expect.

The `struct_time` representation is more difficult to work with, but can be sorted as-is which is the primary use-case, and can be converted relatively easily to `date` or `datetime` objects (provided the year is within 1 to 9999 AD) or to date objects in more flexible libraries like [astropy.time](http://docs.astropy.org/en/stable/time/index.html) for years outside these bounds.

If you are sure you are working with dates within the range supported by Python's `datetime` module, you can get these more convenient objects using the `edtf2.struct_time_to_date` and `edtf2.struct_time_to_datetime` functions.

NOTE: This library previously did return `date` and `datetime` objects from methods by default before we switched to `struct_time`. See ticket https://github.com/ixc/python-edtf/issues/26.

### `lower_strict` and `upper_strict`

These dates indicate the earliest and latest dates that are __strictly__ in the date range, ignoring uncertainty or approximation. One way to think about this is 'if you had to pick a single date to sort by, what would it be?'.

In an ascending sort (most recent last), sort by `lower_strict` to get a natural sort order. In a descending sort (most recent first), sort by `upper_strict`:

    >>> e = parse_edtf('1912-04~')

    >>> e.lower_strict()  # Returns struct_time
    >>> 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)

    >>> e.lower_strict()[:3]  # Show only interesting parts of struct_time
    (1912, 4, 01)

    >>> from edtf2 import struct_time_to_date
    >>> struct_time_to_date(e.lower_strict())  # Convert to date
    datetime.date(1912, 4, 01)

    >>> e.upper_strict()[:3]
    (1912, 4, 30)

    >>> struct_time_to_date(e.upper_strict())
    datetime.date(1912, 4, 30)

### `lower_fuzzy` and `upper_fuzzy`
-----------------------------------

These dates indicate the earliest and latest dates that are __possible__ in the date range, for a fairly arbitrary definition of 'possibly'.

These values are useful for filtering results - i.e. testing which EDTF dates might conceivably fall into, or overlap, a desired date range.

The fuzzy dates are derived from the strict dates, plus or minus a level of padding that depends on how precise the date specfication is. For the case of approximate or uncertain dates, we (arbitrarily) pad the ostensible range by 100% of the uncertain timescale, or by a 12 weeks in the case of seasons. That is, if a date is approximate at the month scale, it is padded by a month. If it is approximate at the year scale, it is padded by a year:

    >>> e = parse_edtf('1912-04~')
    >>> e.lower_fuzzy()[:3]  # padding is 100% of a month
    (1912, 3, 1)
    >>> e.upper_fuzzy()[:3]
    (1912, 5, 30)

    >>> e = parse_edtf('1912~')
    >>> e.lower_fuzzy()[:3]  # padding is 100% of a year
    (1911, 1, 1)
    >>> e.upper_fuzzy()[:3]
    (1913, 12, 31)

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

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

### Seasons

Seasons are interpreted as Northern Hemisphere by default. To change this, override the month mapping in `appsettings.py`.

### Comparisons

Two EDTF dates are considered equal if their unicode() representations are the same. An EDTF date is considered greater than another if its `lower_strict` value is later.

## Django ORM field

The `edtf2.fields.EDTFField` implements a simple Django field that stores an EDTF object in the database.

To store a natural language value on your model, define another field, and set the `natural_text_field` parameter of your `EDTFField`.

When your model is saved, the `natural_text_field` value will be parsed to set the `date_edtf` value, and the underlying EDTF object will set the `_earliest` and `_latest` fields on the model to a float value representing the Julian Date.


**WARNING**: The conversion to and from Julian Date numerical values can be inaccurate, especially for ancient dates back to thousands of years BC. Ideally Julian Date values should be used for range and ordering operations only where complete accuracy is not required. They should **not** be used for definitive storage or for display after roundtrip conversions.

Example usage:

    from django.db import models
    from edtf2.fields import EDTFField

    class MyModel(models.Model):
         date_display = models.CharField(
            "Date of creation (display)",
            blank=True,
            max_length=255,
         )
         date_edtf = EDTFField(
             "Date of creation (EDTF)",
             natural_text_field='date_display',
             lower_fuzzy_field='date_earliest',
             upper_fuzzy_field='date_latest',
             lower_strict_field='date_sort_ascending',
             upper_strict_field='date_sort_descending',
             blank=True,
             null=True,
         )
         # use for filtering
         date_earliest = models.FloatField(blank=True, null=True)
         date_latest = models.FloatField(blank=True, null=True)
         # use for sorting
         date_sort_ascending = models.FloatField(blank=True, null=True)
         date_sort_descending = models.FloatField(blank=True, null=True)


Since the `EDTFField` and the `_earliest` and `_latest` field values are set automatically, you may want to make them readonly, or not visible in your model admin.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/saw-leipzig/python-edtf",
    "name": "edtf2",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "edtf",
    "author": "Sabine M\u00fcller",
    "author_email": "muellers@saw-leipzig.de",
    "download_url": "https://files.pythonhosted.org/packages/d7/50/aa2ed46d9834cd1c14cad9873705b4954e822621da48e80439d6a1eaae8b/edtf2-5.0.0.tar.gz",
    "platform": null,
    "description": "edtf2\n=====\n\nAn implementation of EDTF format in Python, together with utility functions for parsing natural language date texts, and converting EDTF dates to related Python `date` or `struct_time` objects.\n\nSee http://www.loc.gov/standards/datetime/ for the current draft specification.\n\nThis project is based on python-edtf and was developed to include the newest specification\n\n## To install\n\n    pip install edtf2\n\n## To use\n\n    >>> from edtf2 import parse_edtf\n    # Parse an EDTF string to an EDTFObject\n    >>> e = parse_edtf(\"1979-08~\") # approx August 1979\n    >>> e\n    UncertainOrApproximate: '1979-08~'\n    # normalised string representation (some different EDTF strings have identical meanings)\n    >>> unicode(e)\n    u'1979-08~'\n\n    # Derive Python date objects\n    # lower and upper bounds that strictly adhere to the given range\n    >>> e.lower_strict()[:3], e.upper_strict()[:3]\n    ((1979, 8, 1), (1979, 8, 31))\n    # lower and upper bounds that are padded if there's indicated uncertainty\n    >>> e.lower_fuzzy()[:3], e.upper_fuzzy()[:3]\n    ((1979, 7, 1), (1979, 9, 30))\n\n    # Date intervals\n    >>> interval = parse_edtf(\"1979-08~/..\")\n    >>> interval\n    Level1Interval: '1979-08~/..'\n    # Intervals have lower and upper EDTF objects.\n    >>> interval.lower, interval.upper\n    (UncertainOrApproximate: '1979-08~', UnspecifiedIntervalSection: '..')\n    >>> interval.lower.lower_strict()[:3], interval.lower.upper_strict()[:3]\n    ((1979, 8, 1), (1979, 8, 31))\n    >>> interval.upper.upper_strict() # '..' is interpreted to mean open interval and is returning -/+ math.inf\n    math.inf\n\n    # Date collections\n    >>> coll = parse_edtf('{1667,1668, 1670..1672}')\n    >>> coll\n    MultipleDates: '{1667, 1668, 1670..1672}'\n    >>> coll.objects\n    (Date: '1667', Date: '1668', Consecutives: '1670..1672')\n\nThe object returned by `parse_edtf()` is an instance of an `edtf2.parser.parser_classes.EDTFObject` subclass, depending on the type of date that was parsed. These classes are:\n\n    # Level 0\n    Date\n    DateAndTime\n    Interval\n\n    # Level 1\n    UncertainOrApproximate\n    Unspecified\n    Level1Interval\n    UnspecifiedIntervalSection\n    LongYear\n    Season\n\n    # Level 2\n    PartialUncertainOrApproximate\n    PartialUnspecified\n    OneOfASet\n    MultipleDates\n    MaskedPrecision\n    Level2Interval\n    Level2Season\n    ExponentialYear\n\nAll of these implement `upper/lower_strict/fuzzy()` methods to derive `struct_time` objects, except of UnspecifiedIntervalSection, that can also return math.inf value\n\nThe `*Interval` instances have `upper` and `lower` properties that are themselves `EDTFObject` instances.\n\n`OneOfASet` and `MultipleDates` instances have an `objects` property that is a list of all of the EDTF dates parsed in the set or list.\n\n## EDTF Specification Inclusions\n\nThe library includes implementation of levels 0, 1 and 2 of the EDTF spec.\n\nTest coverage includes every example given in the spec table of features.\n\n### Level 0 ISO 8601 Features\n\n* Date:\n\n        >>> parse_edtf('1979-08') # August 1979\n        Date: '1979-08'\n\n* Date and Time:\n\n        >>> parse_edtf('2004-01-01T10:10:10+05:00')\n        DateAndTime: '2004-01-01T10:10:10+05:00'\n\n* Interval (start/end):\n\n        >>> parse_edtf('1979-08-28/1979-09-25') # From August 28 to September 25 1979\n        Interval: '1979-08-28/1979-09-25'\n\n### Level 1 Extensions\n\n* Uncertain/Approximate dates:\n      \n        >>> parse_edtf('1979-08-28~') # Approximately August 28th 1979\n        UncertainOrApproximate: '1979-08-28~'\n\n* Unspecified dates:\n\n        >>> parse_edtf('1979-08-XX') # An unknown day in August 1979\n        Unspecified: '1979-08-XX'\n        >>> parse_edtf('1979-XX') # Some month in 1979\n        Unspecified: '1979-XX'\n\n* Extended intervals:\n\n        >>> parse_edtf('1984-06-02?/2004-08-08~')\n        Level1Interval: '1984-06-02?/2004-08-08~'\n\n* Years exceeding four digits:\n\n        >>> parse_edtf('y-12000') # 12000 years BCE\n        LongYear: 'y-12000'\n\n* Season:\n\n        >>> parse_edtf('1979-22') # Summer 1979\n        Season: '1979-22'\n\n### Level 2 Extensions\n\n* Partial uncertain/approximate:\n\n        >>> parse_edtf('(2011)-06-04~') # year certain, month/day approximate.\n        # Note that the result text is normalized\n        PartialUncertainOrApproximate: '2011-(06-04)~'\n\n* Partial unspecified:\n\n        >>> parse_edtf('1979-XX-28') # The 28th day of an uncertain month in 1979\n        PartialUnspecified: '1979-XX-28'\n\n* One of a set:\n\n        >>> parse_edtf(\"[..1760-12-03,1762]\")\n        OneOfASet: '[..1760-12-03, 1762]'\n\n* Multiple dates:\n\n        >>> parse_edtf('{1667,1668, 1670..1672}')\n        MultipleDates: '{1667, 1668, 1670..1672}'\n\n* Masked precision:\n\n        >>> parse_edtf('197x') # A date in the 1970s.\n        MaskedPrecision: '197x'\n\n* Level 2 Extended intervals:\n\n        >>> parse_edtf('2004-06-(01)~/2004-06-(20)~')\n        Level2Interval: '2004-06-(01)~/2004-06-(20)~'\n\n* Year requiring more than 4 digits - exponential form:\n\n        >>> parse_edtf('y-17e7')\n        ExponentialYear: 'y-17e7'\n\n### Natural language representation\n\n\nThe library includes a basic English natural language parser (it's not yet smart enough to work with occasions such as 'Easter', or in other languages):\n\n    >>> from edtf2 import text_to_edtf\n    >>> text_to_edtf(\"circa August 1979\")\n    '1979-08~'\n\nNote that the result is a string, not an `ETDFObject`.\n\nThe parser can parse strings such as:\n\n    'January 12, 1940' => '1940-01-12'\n    '90' => '1990' #implied century\n    'January 2008' => '2008-01'\n    'the year 1800' => '1800'\n    '10/7/2008' => '2008-10-07' # in a full-specced date, assume US ordering\n\n    # uncertain/approximate\n    '1860?' => '1860?'\n    '1862 (uncertain)' => '1862?'\n    'circa Feb 1812' => '1812-02~'\n    'c.1860' => '1860~' #with or without .\n    'ca1860' => '1860~'\n    'approx 1860' => '1860~'\n\n    # masked precision\n    '1860s' => '186x' #186x has decade precision, 186u has year precision.\n    '1800s' => '18xx' # without uncertainty indicators, assume century\n\n    # masked precision + uncertainty\n    'ca. 1860s' => '186x~'\n    'circa 1840s' => '184x~'\n    'ca. 1860s?' => '186x?~'\n    'c1800s?' => '180x?~' # with uncertainty indicators, use the decade\n\n    # unspecified parts\n    'January 12' => 'XXXX-01-12'\n    'January' => 'XXXX-01'\n    '7/2008' => '2008-07'\n\n    #seasons\n    'Autumn 1872' => '1872-23'\n    'Fall 1872' => '1872-23'\n\n    # before/after\n    'earlier than 1928' => 'unknown/1928'\n    'later than 1928' => '1928/unknown'\n    'before January 1928' => 'unknown/1928-01'\n    'after about the 1920s' => '192x~/unknown'\n\n    # unspecified\n    'year in the 1860s' => '186u' #186x has decade precision, 186u has year precision.\n    ('year in the 1800s', '18xu')\n    'month in 1872' => '1872-XX'\n    'day in January 1872' => '1872-01-XX'\n    'day in 1872' => '1872-XX-XX'\n\n    #centuries\n    '1st century' => '00xx'\n    '10c' => '09xx'\n    '19th century?' => '18xx?'\n\n    # just showing off now...\n    'a day in about Spring 1849?' => '1849-21-XX?~'\n\n    # simple ranges, which aren't as accurate as they could be. The parser is\n    limited to only picking the first year range it finds.\n    '1851-1852' => '1851/1852'\n    '1851-1852; printed 1853-1854' => '1851/1852'\n    '1851-52' => '1851/1852'\n    '1856-ca. 1865' => '1856/1865~'\n    '1860s-1870s' => '186x/187x'\n    '1920s -early 1930s' => '192x/193x'\n    '1938, printed 1940s-1950s' => '1938'\n\n\nGenerating natural text from an EDTF representation is a future goal.\n\n### What assumptions does the natural text parser make when interpreting an ambiguous date?\n\n* \"1800s\" is ambiguously a century or decade. If the given date is either uncertain or approximate, the decade interpretation is used. If the date is certain and precise, the century interpretation is used.\n\n* If the century isn't specified (`EDTF(natural_text=\"the '70s\")`), we imply the century to be \"19\" if the year is greater than the current year, otherwise we imply the century to be the current century.\n\n* US-ordered dates (mm/dd/yyyy) are assumed by default in natural language.  To change this, set `DAY_FIRST` to True in settings.\n\n* If a natural language groups dates with a '/', it's interpreted as \"or\" rather than \"and\". The resulting EDTF text is a list bracketed by `[]` (\"one of these dates\") rather than `{}` (all of these dates).\n\n\n## Converting to and from Python dates\n\n\nSince EDTF dates are often regions, and often imprecise, we need to use a few different Python dates, depending on the circumstance. Generally, Python dates are used for sorting and filtering, and are not displayed directly to users.\n\n\n### `struct_time` date representation\n\nBecause Python's `datetime` module does not support dates out side the range 1 AD to 9999 AD we return dates as `time.struct_time` objects by default instead of the `datetime.date` or `datetime.datetime` objects you might expect.\n\nThe `struct_time` representation is more difficult to work with, but can be sorted as-is which is the primary use-case, and can be converted relatively easily to `date` or `datetime` objects (provided the year is within 1 to 9999 AD) or to date objects in more flexible libraries like [astropy.time](http://docs.astropy.org/en/stable/time/index.html) for years outside these bounds.\n\nIf you are sure you are working with dates within the range supported by Python's `datetime` module, you can get these more convenient objects using the `edtf2.struct_time_to_date` and `edtf2.struct_time_to_datetime` functions.\n\nNOTE: This library previously did return `date` and `datetime` objects from methods by default before we switched to `struct_time`. See ticket https://github.com/ixc/python-edtf/issues/26.\n\n### `lower_strict` and `upper_strict`\n\nThese dates indicate the earliest and latest dates that are __strictly__ in the date range, ignoring uncertainty or approximation. One way to think about this is 'if you had to pick a single date to sort by, what would it be?'.\n\nIn an ascending sort (most recent last), sort by `lower_strict` to get a natural sort order. In a descending sort (most recent first), sort by `upper_strict`:\n\n    >>> e = parse_edtf('1912-04~')\n\n    >>> e.lower_strict()  # Returns struct_time\n    >>> 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)\n\n    >>> e.lower_strict()[:3]  # Show only interesting parts of struct_time\n    (1912, 4, 01)\n\n    >>> from edtf2 import struct_time_to_date\n    >>> struct_time_to_date(e.lower_strict())  # Convert to date\n    datetime.date(1912, 4, 01)\n\n    >>> e.upper_strict()[:3]\n    (1912, 4, 30)\n\n    >>> struct_time_to_date(e.upper_strict())\n    datetime.date(1912, 4, 30)\n\n### `lower_fuzzy` and `upper_fuzzy`\n-----------------------------------\n\nThese dates indicate the earliest and latest dates that are __possible__ in the date range, for a fairly arbitrary definition of 'possibly'.\n\nThese values are useful for filtering results - i.e. testing which EDTF dates might conceivably fall into, or overlap, a desired date range.\n\nThe fuzzy dates are derived from the strict dates, plus or minus a level of padding that depends on how precise the date specfication is. For the case of approximate or uncertain dates, we (arbitrarily) pad the ostensible range by 100% of the uncertain timescale, or by a 12 weeks in the case of seasons. That is, if a date is approximate at the month scale, it is padded by a month. If it is approximate at the year scale, it is padded by a year:\n\n    >>> e = parse_edtf('1912-04~')\n    >>> e.lower_fuzzy()[:3]  # padding is 100% of a month\n    (1912, 3, 1)\n    >>> e.upper_fuzzy()[:3]\n    (1912, 5, 30)\n\n    >>> e = parse_edtf('1912~')\n    >>> e.lower_fuzzy()[:3]  # padding is 100% of a year\n    (1911, 1, 1)\n    >>> e.upper_fuzzy()[:3]\n    (1913, 12, 31)\n\nOne can interpret uncertain or approximate dates as 'plus or minus a [level of precision]'.\n\nIf a date is both uncertain __and__ approximate, the padding is applied twice, i.e. it gets 100% * 2 padding, or 'plus or minus two [levels of precision]'.\n\n### Seasons\n\nSeasons are interpreted as Northern Hemisphere by default. To change this, override the month mapping in `appsettings.py`.\n\n### Comparisons\n\nTwo EDTF dates are considered equal if their unicode() representations are the same. An EDTF date is considered greater than another if its `lower_strict` value is later.\n\n## Django ORM field\n\nThe `edtf2.fields.EDTFField` implements a simple Django field that stores an EDTF object in the database.\n\nTo store a natural language value on your model, define another field, and set the `natural_text_field` parameter of your `EDTFField`.\n\nWhen your model is saved, the `natural_text_field` value will be parsed to set the `date_edtf` value, and the underlying EDTF object will set the `_earliest` and `_latest` fields on the model to a float value representing the Julian Date.\n\n\n**WARNING**: The conversion to and from Julian Date numerical values can be inaccurate, especially for ancient dates back to thousands of years BC. Ideally Julian Date values should be used for range and ordering operations only where complete accuracy is not required. They should **not** be used for definitive storage or for display after roundtrip conversions.\n\nExample usage:\n\n    from django.db import models\n    from edtf2.fields import EDTFField\n\n    class MyModel(models.Model):\n         date_display = models.CharField(\n            \"Date of creation (display)\",\n            blank=True,\n            max_length=255,\n         )\n         date_edtf = EDTFField(\n             \"Date of creation (EDTF)\",\n             natural_text_field='date_display',\n             lower_fuzzy_field='date_earliest',\n             upper_fuzzy_field='date_latest',\n             lower_strict_field='date_sort_ascending',\n             upper_strict_field='date_sort_descending',\n             blank=True,\n             null=True,\n         )\n         # use for filtering\n         date_earliest = models.FloatField(blank=True, null=True)\n         date_latest = models.FloatField(blank=True, null=True)\n         # use for sorting\n         date_sort_ascending = models.FloatField(blank=True, null=True)\n         date_sort_descending = models.FloatField(blank=True, null=True)\n\n\nSince the `EDTFField` and the `_earliest` and `_latest` field values are set automatically, you may want to make them readonly, or not visible in your model admin.",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python implementation of Library of Congress EDTF (Extended Date Time Format) specification",
    "version": "5.0.0",
    "project_urls": {
        "Homepage": "https://github.com/saw-leipzig/python-edtf"
    },
    "split_keywords": [
        "edtf"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d750aa2ed46d9834cd1c14cad9873705b4954e822621da48e80439d6a1eaae8b",
                "md5": "255381fcbf3ff453c3c6d363655eb88d",
                "sha256": "1b9f51d3c080fee828d61f71ec0d503d7bad6c59285e618cf9bc6b42f079780f"
            },
            "downloads": -1,
            "filename": "edtf2-5.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "255381fcbf3ff453c3c6d363655eb88d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 41789,
            "upload_time": "2023-10-04T06:32:19",
            "upload_time_iso_8601": "2023-10-04T06:32:19.354076Z",
            "url": "https://files.pythonhosted.org/packages/d7/50/aa2ed46d9834cd1c14cad9873705b4954e822621da48e80439d6a1eaae8b/edtf2-5.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-04 06:32:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "saw-leipzig",
    "github_project": "python-edtf",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "tox": true,
    "lcname": "edtf2"
}
        
Elapsed time: 0.11770s