Skip to content

capturegraph.scheduling.forecast.time #

Time Forecasting - Generate Future Time Slots#

Creates a list of candidate timestamps for the scheduler to choose from.

times(resolution=timedelta(minutes=5), span=timedelta(hours=24), start=None) #

Generate candidate timestamps over a time span.

Parameters:

Name Type Description Default
resolution timedelta

Time between candidate slots. Defaults to 5 minutes.

timedelta(minutes=5)
span timedelta

Total time span to cover. Defaults to 24 hours.

timedelta(hours=24)
start datetime | None

Starting time. Defaults to now.

None

Returns:

Type Description
List[datetime]

List of datetime objects at the given resolution.

Source code in capturegraph-lib/capturegraph/scheduling/forecast/time.py
def times(
    resolution: timedelta = timedelta(minutes=5),
    span: timedelta = timedelta(hours=24),
    start: datetime | None = None,
) -> cg.List[datetime]:
    """Generate candidate timestamps over a time span.

    Args:
        resolution: Time between candidate slots. Defaults to 5 minutes.
        span: Total time span to cover. Defaults to 24 hours.
        start: Starting time. Defaults to now.

    Returns:
        List of datetime objects at the given resolution.
    """
    if start is None:
        start = datetime.now()

    times = []
    end = start + span
    current = start

    while current < end:
        times.append(current)
        current += resolution

    return cg.List(times)

times_of_day(hours, start=None, span=timedelta(hours=24)) #

Generate candidate time slots at specific hours of the day.

Creates slots for today and tomorrow at the specified hours, returning only those that are in the future.

Parameters:

Name Type Description Default
hours list[int]

List of hours (0-23) to generate slots for.

required
start datetime | None

Starting time. Defaults to now.

None
span timedelta

Total time span to cover. Defaults to 24 hours.

timedelta(hours=24)

Returns:

Type Description
List[datetime]

List of datetime objects.

Example
slots = cgsh.forecast.times_of_day(
    hours=[10, 12, 14, 16, 18, 20, 22],
    start=datetime.now(),
    span=timedelta(hours=24),
)
next_slot = store.assign_next(user_id, slots)
Source code in capturegraph-lib/capturegraph/scheduling/forecast/time.py
def times_of_day(
    hours: list[int],
    start: datetime | None = None,
    span: timedelta = timedelta(hours=24),
) -> cg.List[datetime]:
    """Generate candidate time slots at specific hours of the day.

    Creates slots for today and tomorrow at the specified hours,
    returning only those that are in the future.

    Args:
        hours: List of hours (0-23) to generate slots for.
        start: Starting time. Defaults to now.
        span: Total time span to cover. Defaults to 24 hours.

    Returns:
        List of datetime objects.

    Example:
        ```python
        slots = cgsh.forecast.times_of_day(
            hours=[10, 12, 14, 16, 18, 20, 22],
            start=datetime.now(),
            span=timedelta(hours=24),
        )
        next_slot = store.assign_next(user_id, slots)
        ```
    """
    if start is None:
        start = datetime.now()

    slots = []
    today = start.date()

    for hour in hours:
        slot_time = datetime.combine(today, datetime.min.time()).replace(hour=hour)

        if slot_time > start:
            slots.append(slot_time)

    # Sort by date
    slots.sort(key=lambda s: s)
    return cg.List(slots)