Skip to content

Iso date

DateTerm

Bases: BaseModel

Model for Date terms with format.

Parameters:

Name Type Description Default
input_term str

Term to run method on.

required
format str

Format of the date.

'%Y-%m-%dT%H:%M:%SZ'
output_key str

Key to output to.

'datetime'
Source code in extraction_methods/plugins/iso_date.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class DateTerm(BaseModel):
    """
    Model for Date terms with format.
    """

    input_term: str = Field(
        description="Term to run method on.",
    )
    format: str = Field(
        default="%Y-%m-%dT%H:%M:%SZ",
        description="Format of the date.",
    )
    output_key: str = Field(
        default="datetime",
        description="Key to output to.",
    )

ISODateExtract

Bases: ExtractionMethod

Takes the source dict and the key to access the date and converts the date to ISO 8601 Format.

e.g.

YYYY-MM-DDTHH:MM:SS.ffffff, if microsecond is not 0 YYYY-MM-DDTHH:MM:SS, if microsecond is 0

If the date format cannot be parsed, it is removed from the source dict with an error logged.

Method name: iso_date

Example Configuration

.. code-block:: yaml

- method: iso_date
  inputs:
    dates:
      - key: $datetime
        output_key: date
        format: "%Y-%m-%dT%H:%M:%S"
      - key: 2012-12-12
        format: "%Y-%m-%d"
Source code in extraction_methods/plugins/iso_date.py
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class ISODateExtract(ExtractionMethod):
    """
    Takes the source dict and the key to access the date and
    converts the date to ISO 8601 Format.

    e.g.

    ``YYYY-MM-DDTHH:MM:SS.ffffff``, if microsecond is not 0
    ``YYYY-MM-DDTHH:MM:SS``, if microsecond is 0

    If the date format cannot be parsed, it is removed from the source dict with
    an error logged.

    **Method name:** ``iso_date``

    Example Configuration:
        .. code-block:: yaml

            - method: iso_date
              inputs:
                dates:
                  - key: $datetime
                    output_key: date
                    format: "%Y-%m-%dT%H:%M:%S"
                  - key: 2012-12-12
                    format: "%Y-%m-%d"
    """

    input_class = ISODateInput

    @update_input
    def run(self, body: dict[str, Any]) -> dict[str, Any]:

        for date_term in self.input.date_terms:

            if not date_term:
                LOGGER.error("%s not present in %s", date_term.input_term, body)

            else:

                try:
                    date_iso = datetime.strptime(
                        date_term.input_term, date_term.format
                    ).isoformat()
                    body[date_term.output_key] = date_iso

                except ValueError:
                    LOGGER.error(
                        "date_term: %s doesn't match format: %s",
                        date_term.input_term,
                        date_term.format,
                    )

        return body

ISODateInput

Bases: Input

Model for ISO Date Input.

Parameters:

Name Type Description Default
date_terms list[DateTerm]

List of date terms.

[]
Source code in extraction_methods/plugins/iso_date.py
40
41
42
43
44
45
46
47
48
class ISODateInput(Input):
    """
    Model for ISO Date Input.
    """

    date_terms: list[DateTerm] = Field(
        default=[],
        description="List of date terms.",
    )