haive.core.common.mixins.general.timestamp¶

Timestamp mixin for tracking creation and modification times.

This module provides a mixin for adding timestamp tracking to Pydantic models. It automatically records creation time and provides methods for updating and querying timestamps, which is useful for auditing, caching, and expiration logic.

Usage:

from pydantic import BaseModel from haive.core.common.mixins.general import TimestampMixin

class Document(TimestampMixin, BaseModel):

title: str content: str

# Create a document (timestamps automatically set) doc = Document(title=”Example”, content=”Content”)

# Check how old the document is age = doc.age_in_seconds()

# Update document and its timestamp doc.content = “Updated content” doc.update_timestamp()

# Check time since last update time_since_update = doc.time_since_update()

Classes¶

TimestampMixin

Mixin for adding timestamp tracking to Pydantic models.

Module Contents¶

class haive.core.common.mixins.general.timestamp.TimestampMixin(/, **data)[source]¶

Bases: pydantic.BaseModel

Mixin for adding timestamp tracking to Pydantic models.

This mixin adds creation and update timestamps to any model, with methods for updating timestamps and calculating time intervals. It’s useful for tracking when objects were created and modified, which helps with auditing, caching strategies, and expiration logic.

Parameters:

data (Any)

created_at¶

When this object was created (auto-set on instantiation).

updated_at¶

When this object was last updated (initially same as created_at).

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

age_in_seconds()[source]¶

Get age of this object in seconds.

This method calculates how much time has passed since the object was created.

Returns:

Number of seconds since creation.

Return type:

float

time_since_update()[source]¶

Get time since last update in seconds.

This method calculates how much time has passed since the object was last updated.

Returns:

Number of seconds since last update.

Return type:

float

update_timestamp()[source]¶

Update the updated_at timestamp to the current time.

This method should be called whenever the object is modified to track the time of the latest change.

Return type:

None