Skip to content

capturegraph.data.typed_json.handlers.datetime #

DateTimeHandler #

Bases: TypeHandler

TypeHandler for Python datetime objects.

Serializes datetime to Unix timestamp format compatible with iOS PTime.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/datetime.py
class DateTimeHandler(TypeHandler):
    """TypeHandler for Python datetime objects.

    Serializes datetime to Unix timestamp format compatible with iOS PTime.
    """

    def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
        """Return schema: expects 'time_since_1970' key with numeric value."""
        return {"time_since_1970": (int, float)}

    def decode(self, obj: Dict[str, Any]) -> Any | None:
        """Decode JSON dict to datetime.

        Args:
            obj: Dict with 'time_since_1970' key containing seconds since epoch.

        Returns:
            datetime instance, or None if schema doesn't match.
        """
        try:
            return datetime.fromtimestamp(obj["time_since_1970"])
        except (KeyError, TypeError, ValueError):
            return None

    def encodable_types(self) -> Tuple[type, ...]:
        """Return tuple of types this handler can encode."""
        return (datetime,)

    def encode(self, obj: Any) -> Dict[str, Any] | None:
        """Encode datetime to JSON dict.

        Args:
            obj: datetime instance to encode.

        Returns:
            Dict with 'time_since_1970' key, or None if not a datetime.
        """
        return {"time_since_1970": obj.timestamp()}

decodable_schema() #

Return schema: expects 'time_since_1970' key with numeric value.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/datetime.py
def decodable_schema(self) -> Dict[str, Tuple[type, ...]]:
    """Return schema: expects 'time_since_1970' key with numeric value."""
    return {"time_since_1970": (int, float)}

decode(obj) #

Decode JSON dict to datetime.

Parameters:

Name Type Description Default
obj Dict[str, Any]

Dict with 'time_since_1970' key containing seconds since epoch.

required

Returns:

Type Description
Any | None

datetime instance, or None if schema doesn't match.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/datetime.py
def decode(self, obj: Dict[str, Any]) -> Any | None:
    """Decode JSON dict to datetime.

    Args:
        obj: Dict with 'time_since_1970' key containing seconds since epoch.

    Returns:
        datetime instance, or None if schema doesn't match.
    """
    try:
        return datetime.fromtimestamp(obj["time_since_1970"])
    except (KeyError, TypeError, ValueError):
        return None

encodable_types() #

Return tuple of types this handler can encode.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/datetime.py
def encodable_types(self) -> Tuple[type, ...]:
    """Return tuple of types this handler can encode."""
    return (datetime,)

encode(obj) #

Encode datetime to JSON dict.

Parameters:

Name Type Description Default
obj Any

datetime instance to encode.

required

Returns:

Type Description
Dict[str, Any] | None

Dict with 'time_since_1970' key, or None if not a datetime.

Source code in capturegraph-lib/capturegraph/data/typed_json/handlers/datetime.py
def encode(self, obj: Any) -> Dict[str, Any] | None:
    """Encode datetime to JSON dict.

    Args:
        obj: datetime instance to encode.

    Returns:
        Dict with 'time_since_1970' key, or None if not a datetime.
    """
    return {"time_since_1970": obj.timestamp()}