Skip to content

type_bridge.attribute.date

date

Date attribute type for TypeDB.

Date

Date(value)

Bases: Attribute

Date attribute type that accepts date values (date only, no time).

This maps to TypeDB's 'date' type, which is an ISO 8601 compliant date without time information.

Range: January 1, 262144 BCE to December 31, 262142 CE

Example

from datetime import date

class PublishDate(Date): pass

class BirthDate(Date): pass

Usage with date values

published = PublishDate(date(2024, 3, 30)) birthday = BirthDate(date(1990, 5, 15))

Initialize Date attribute with a date value.

Parameters:

Name Type Description Default
value date | str

The date value to store. Can be: - datetime.date instance - str in ISO 8601 format (YYYY-MM-DD)

required
Example

from datetime import date

From date instance

publish_date = PublishDate(date(2024, 3, 30))

From ISO string

publish_date = PublishDate("2024-03-30")

Source code in type_bridge/attribute/date.py
def __init__(self, value: date_type | str):
    """Initialize Date attribute with a date value.

    Args:
        value: The date value to store. Can be:
            - datetime.date instance
            - str in ISO 8601 format (YYYY-MM-DD)

    Example:
        from datetime import date

        # From date instance
        publish_date = PublishDate(date(2024, 3, 30))

        # From ISO string
        publish_date = PublishDate("2024-03-30")
    """
    if isinstance(value, str):
        value = date_type.fromisoformat(value)
    elif isinstance(value, datetime_type):
        # If passed a datetime, extract just the date part
        value = value.date()
    super().__init__(value)

value property

value

Get the stored date value.

__add__

__add__(other)

Add a Duration to this Date.

Parameters:

Name Type Description Default
other Any

A Duration to add to this date

required

Returns:

Type Description
Date

New Date with the duration added

Example

from type_bridge import Duration d = Date(date(2024, 1, 31)) duration = Duration("P1M") result = d + duration # Date(2024-02-29)

Source code in type_bridge/attribute/date.py
def __add__(self, other: Any) -> "Date":
    """Add a Duration to this Date.

    Args:
        other: A Duration to add to this date

    Returns:
        New Date with the duration added

    Example:
        from type_bridge import Duration
        d = Date(date(2024, 1, 31))
        duration = Duration("P1M")
        result = d + duration  # Date(2024-02-29)
    """
    from type_bridge.attribute.duration import Duration

    if isinstance(other, Duration):
        new_date = self.value + other.value
        # isodate returns datetime when adding Duration to date, extract .date()
        if isinstance(new_date, datetime_type):
            new_date = new_date.date()
        return Date(new_date)
    return NotImplemented

__radd__

__radd__(other)

Reverse addition for Duration + Date.

Source code in type_bridge/attribute/date.py
def __radd__(self, other: Any) -> "Date":
    """Reverse addition for Duration + Date."""
    return self.__add__(other)

__sub__

__sub__(other)

Subtract a Duration from this Date.

Parameters:

Name Type Description Default
other Any

A Duration to subtract from this date

required

Returns:

Type Description
Date

New Date with the duration subtracted

Example

from type_bridge import Duration d = Date(date(2024, 3, 31)) duration = Duration("P1M") result = d - duration # Date(2024-02-29)

Source code in type_bridge/attribute/date.py
def __sub__(self, other: Any) -> "Date":
    """Subtract a Duration from this Date.

    Args:
        other: A Duration to subtract from this date

    Returns:
        New Date with the duration subtracted

    Example:
        from type_bridge import Duration
        d = Date(date(2024, 3, 31))
        duration = Duration("P1M")
        result = d - duration  # Date(2024-02-29)
    """
    from type_bridge.attribute.duration import Duration

    if isinstance(other, Duration):
        new_date = self.value - other.value
        if isinstance(new_date, datetime_type):
            new_date = new_date.date()
        return Date(new_date)
    return NotImplemented