hbutils.scale.time

Time duration conversion utilities.

This module provides helper functions for converting time durations into standardized numeric or string formats. It supports raw numeric values interpreted as seconds, as well as string duration formats such as "1h30m" or "0:03:53.540000" parsed by pytimeparse.

The main public utilities are:

Example:

>>> from hbutils.scale.time import time_to_duration, time_to_delta_str
>>> time_to_duration("1h343m67.4s")
24247.4
>>> time_to_delta_str(233.54)
'0:03:53.540000'

Note

The parsing of string durations is delegated to pytimeparse.parse(). Unsupported strings may result in a None return value from the parser.

__all__

hbutils.scale.time.__all__ = ['time_to_duration', 'time_to_delta_str']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

time_to_duration

hbutils.scale.time.time_to_duration(time_: float | int | str) float | int[source]

Turn any types of time duration into time value in seconds.

Parameters:

time (Union[int, float, str]) – Any types of time duration, can be numeric (seconds) or string format.

Returns:

Time duration value in seconds.

Return type:

Union[float, int]

Raises:
  • TypeError – If the input type is not int, float, or str.

  • TypeError – If the string input is not parsable by pytimeparse.

  • ValueError – If the string input is not parsable by pytimeparse.

Examples::
>>> from hbutils.scale import time_to_duration
>>> time_to_duration(23344)
23344
>>> time_to_duration(233.54)
233.54
>>> time_to_duration('1h343m67.4s')
24247.4
>>> time_to_duration('0:03:53.540000')
233.54

time_to_delta_str

hbutils.scale.time.time_to_delta_str(time_: float | int | str) str[source]

Turn any types of time duration into time value in formatted string.

This function converts various time duration formats into a standardized string representation using the format H:MM:SS or H:MM:SS.ffffff for durations with fractional seconds.

Parameters:

time (Union[int, float, str]) – Any types of time duration, can be numeric (seconds) or string format.

Returns:

Time duration value in formatted string (e.g., "6:29:04" or "0:03:53.540000").

Return type:

str

Raises:

TypeError – If the input type is not int, float, or str (raised by time_to_duration()).

Examples::
>>> from hbutils.scale import time_to_delta_str
>>> time_to_delta_str(23344)
'6:29:04'
>>> time_to_delta_str(233.54)
'0:03:53.540000'
>>> time_to_delta_str('1h343m67.4s')
'6:44:07.400000'