Skip to content

timeframe

timeframe

Classes:

Name Description
TimeFrame

Pre-defined TimeFrame values

Functions:

Name Description
clean_timestamp

Removes Microseconds from the timestamp and returns it

on_timeframe

Checks if timestamp is on a timeframe value

round_down_timestamp

Find and round down timestamp to the nearest matching timeframe. E.G timeframe of 5 minute

within_timeframe

Checks if timestamp is within other timestamp and timeframe period

TimeFrame

Bases: Enum

Pre-defined TimeFrame values

clean_timestamp

clean_timestamp(timestamp: datetime) -> datetime

Removes Microseconds from the timestamp and returns it

Source code in hexital/utils/timeframe.py
def clean_timestamp(timestamp: datetime) -> datetime:
    """Removes Microseconds from the timestamp and returns it"""
    return timestamp.replace(microsecond=0)

on_timeframe

on_timeframe(timestamp: datetime, timeframe: timedelta) -> bool

Checks if timestamp is on a timeframe value

Source code in hexital/utils/timeframe.py
def on_timeframe(timestamp: datetime, timeframe: timedelta) -> bool:
    """Checks if timestamp is on a timeframe value"""
    return timestamp.timestamp() % timeframe.total_seconds() == 0

round_down_timestamp

round_down_timestamp(timestamp: datetime, timeframe: timedelta) -> datetime

Find and round down timestamp to the nearest matching timeframe. E.G timeframe of 5 minute E.G T5: 09:00:01 -> 9:00:00 E.G T5: 09:01:20 -> 9:00:00 E.G T5: 09:05:00 -> 9:05:00 Note: This method also calls clean_timestamp, removing microseconds

Source code in hexital/utils/timeframe.py
def round_down_timestamp(timestamp: datetime, timeframe: timedelta) -> datetime:
    """Find and round down timestamp to the nearest matching timeframe. E.G timeframe of 5 minute
    E.G T5: 09:00:01 -> 9:00:00
    E.G T5: 09:01:20 -> 9:00:00
    E.G T5: 09:05:00 -> 9:05:00
    Note: This method also calls clean_timestamp, removing microseconds
    """
    timestamp = clean_timestamp(timestamp)
    if timeframe < timedelta(days=1):
        return datetime.fromtimestamp(
            timestamp.timestamp() // timeframe.total_seconds() * timeframe.total_seconds()
        )
    elif timeframe < timedelta(days=7):
        return timestamp.replace(hour=0, minute=0, second=0)
    else:
        return timestamp.replace(day=0, hour=0, minute=0, second=0)

within_timeframe

within_timeframe(
    timestamp: datetime, within: datetime, timeframe: timedelta | None
) -> bool

Checks if timestamp is within other timestamp and timeframe period

Source code in hexital/utils/timeframe.py
def within_timeframe(timestamp: datetime, within: datetime, timeframe: timedelta | None) -> bool:
    """Checks if timestamp is within other timestamp and timeframe period"""
    if not timeframe:
        return False
    return within - timeframe < timestamp <= within