hyperion.dateutils¶
hyperion.dateutils
¶
truncate_datetime
¶
Truncate datetime to the specified unit (set all smaller units to zero).
Source code in hyperion/dateutils.py
iter_dates_between
¶
Iterate over datetimes between start_date and end_date with steps based on the given granularity. Includes the start_date and may include end_date (if end date is reachable from start date with given granularity).
:param start_date: The starting datetime. :param end_date: The ending datetime. :param granularity: The granularity for steps (e.g., "d" for days, "M" for months). :return: An iterator of datetime objects.
Source code in hyperion/dateutils.py
quantize_datetime
¶
Quantize a datetime to the next interval based on the specified resolution.
This function aligns a given datetime to the next moment in time defined by the resolution. The resolution is expressed as a unit (seconds, minutes, hours, or days) and a value (e.g., 5 seconds, 15 minutes).
Important Notes: - The resolution is always calculated relative to the higher unit, which may lead to overlapping intervals for non-standard values. For example: - A 7-second resolution could result in intervals ending at 12:50:56 and 12:51:03, with another interval starting at 12:51:00, causing overlaps. - To avoid such overlaps, it is recommended to use resolutions that are divisors of the higher unit (e.g., 60 for seconds and minutes).
Parameters: - base (datetime.datetime): The datetime to quantize. - resolution (TimeResolution | str): The resolution for quantization. If a string is provided, it should follow the format "{value}{unit}" (e.g., "5s", "15m", "2h").
Returns: - datetime.datetime: The quantized datetime.
Raises: - ValueError: If the resolution unit is unsupported.
Source code in hyperion/dateutils.py
assure_timezone
¶
Assure datetime has a datetime and return it timezone-aware if not.
Source code in hyperion/dateutils.py
get_date_pattern
¶
Get a date pattern string up to the specified unit level.
Examples:
>>> get_date_pattern(datetime.datetime(2025, 1, 12, 12), "h")
"2025-01-12T12"
>>> get_date_pattern(datetime.datetime(2025, 1, 12, 12), "d")
"2025-01-12"
>>> get_date_pattern(datetime.datetime(2025, 1, 12, 12), "M")
"2025-01"
>>> get_date_pattern(datetime.datetime(2025, 1, 12, 12), "y")
"2025"
Source code in hyperion/dateutils.py
iter_intervals
¶
Iter tuples of (interval start, interval end).
:param start_date: The starting datetime. :param end_date: The ending datetime. :param granularity: The granularity for steps (e.g., "d" for days, "M" for months). :return: An iterator of intervals (tuples of (start, end)).