Skip to content

ontocast.onto.xsd_lexical

Lexical forms of the XSD date, time and duration datatypes.

rdflib reads these datatypes with Python's ISO parsers, which also accept forms XSD does not (2021-06-29 or 20210629 as xsd:dateTime, week dates, a space for T, a date-time as xsd:date). A triple store treats such a literal as ill-typed, so validity is decided here by the XSD 1.1 lexical spaces instead, with the day checked against its month.

Attributes

CALENDAR_DATATYPES = frozenset({XSD.date, XSD.dateTime, XSD.dateTimeStamp, XSD.gYearMonth, XSD.gYear}) module-attribute

TEMPORAL_DATATYPES = frozenset(_PATTERNS) module-attribute

__all__ = ['CALENDAR_DATATYPES', 'TEMPORAL_DATATYPES', 'is_valid_temporal_lexical'] module-attribute

Functions:

is_valid_temporal_lexical(lexical, datatype)

Whether lexical is in the XSD lexical space of datatype.

A day must exist in its month (2021-02-30 is not a date; --02-29 is a month-day).

Returns:

Type Description
bool | None

None when datatype is not a date, time or duration datatype.

Source code in ontocast/onto/xsd_lexical.py
def is_valid_temporal_lexical(lexical: str, datatype: URIRef) -> bool | None:
    """Whether ``lexical`` is in the XSD lexical space of ``datatype``.

    A day must exist in its month (``2021-02-30`` is not a date; ``--02-29``
    is a month-day).

    Returns:
        ``None`` when ``datatype`` is not a date, time or duration datatype.
    """
    pattern = _PATTERNS.get(datatype)
    if pattern is None:
        return None
    # These datatypes collapse whitespace before their lexical space applies.
    match = pattern.fullmatch(" ".join(lexical.split()))
    if match is None:
        return False
    groups = match.groupdict()
    if groups.get("day") is None or groups.get("month") is None:
        return True
    year = int(groups["year"]) if groups.get("year") is not None else None
    return int(groups["day"]) <= _days_in_month(year, int(groups["month"]))