dlpt.time module

Utility functions to convert time in/to/from various formats and track execution time of a code.

dlpt.time.timestamp_to_datetime(timestamp: float) datetime

Return a datetime object for a given timestamp (as returned by time.time()).

Parameters:

timestamp – timestamp as a number since the epoch (time.time()).

Returns:

Datetime object of a timestamp.

dlpt.time.timestamp_to_str(timestamp: float, fmt: str = '%H:%M:%S', msec_digits: int = 0) str

Return a string of converted timestamp (as returned by time.time()) by following the given format.

Parameters:
  • timestamp – timestamp as a number since the epoch (time.time()).

  • fmt – output string format.

  • msec_digits – See the docs check _format_msec()

Returns:

Timestamp as a string, based on a given format.

dlpt.time.sec_to_str(seconds: float, fmt: str = '%H h %M min %S sec', hide_zeroes: bool = True) str

Return a string of a converted time (in seconds) by following the given format.

Note: Only applicable for hours, minutes and seconds. Days and larger time

units are silently ignored (added to the hours).

Note: Seconds are always displayed as a 2 digit float, while hours and

numbers are integers. Example: 2 days and 4 hours -> 52 h 0 min 0.00 sec

Parameters:
  • seconds – time (duration) as a number of seconds.

  • fmt – output string format. This function does not support setting float number of digits for seconds. Output format can be changed if hide_zeroes arg is True.

  • hide_zeroes – if True, leading parts (hours, minutes) can be omitted (if zero), Otherwise, fmt is strictly respected. Note: this is applicable only in the most common use cases, where time is displayed in order <hours> <minutes> <seconds>. If hide_zeroes is True, leading zero-value parts are stripped to the first next time part: hours to minutes, minutes to seconds. 361.5 sec = 1 h 0 min 1.50 sec 360 sec = 1 h 0 min 0.00 sec 359 sec = 59 min 0.00 sec 59 sec = 59.00 sec Other special time formaters can be used by setting hide_zeroes to False.

Returns:

Formatted string of a given seconds number.

dlpt.time.time_to_seconds(d: int = 0, h: int = 0, m: int = 0, s: float = 0.0) float

Return ‘seconds’ representation of a given time as defined by days, hours, minutes and seconds.

Parameters:
  • d – number of days to add to returned seconds.

  • h – number of hours to add to returned seconds.

  • m – number of minutes to add to returned seconds.

  • s – number of seconds to add to returned seconds.

Returns:

‘Seconds’ representation of a given time duration.

dlpt.time.datetime_to_str(dt: datetime, fmt: str = '%H:%M:%S') str

Return a string representation of a given dt datetime.datetime` object.

Note: dt is datetime.datetime object, not datetime.timedelta -

check timedelta_to_str().

Parameters:
  • dt – datetime object to convert to string.

  • fmt – output string format.

Returns:

String representation of datetime.datetime object.

dlpt.time.timedelta_to_str(td: timedelta, fmt: str = '%M min %S sec') str

Return a string representation of a td datetime.timedelta object.

Note: receives datetime.timedelta object, not datetime.datetime - check

datetime_to_str().

Parameters:
  • td – datetime.timedelta object to convert to string.

  • fmt – output string format. Respect output format - does not hide zeroes.

Returns:

String representation of datetime.timedelta object.

dlpt.time.get_current_datetime_str(fmt: str = '%d-%b-%Y_%H:%M:%S', msec_digits: int = 0) str

Return a string of a current timestamp by following the given format.

Parameters:
  • fmt – output string format.

  • msec_digits – check _format_msec().

Returns:

Formatted current date and time string.

dlpt.time.print_exec_time(func: Callable[[...], T_EXEC_TIME]) Callable[[...], T_EXEC_TIME]

Decorator to get and print (to console) approximate execution time. Additionally, user can get execution time with get_last_measured_time_sec().

Parameters:

func – function reference to get execution time.

Example

>>> @dlpt.time.print_exec_time
    def my_function(*args, **kwargs):
        time.sleep(42)
>>> my_function()
"'my_function' execution time: 42.63 sec"
>>> dlpt.time.get_last_measured_time_sec()
42.63
dlpt.time.func_stopwatch(func: Callable[[...], T_EXEC_TIME]) Callable[[...], T_EXEC_TIME]

Call function and track its execution time. Similar to a print_exec_time() decorator, but can be used with function with arguments. Does not print time to console.

Parameters:

func – function ‘pointer’ to track execution time.

Example

>>> def my_function(*args, **kwargs):
        time.sleep(42)
>>> my_function_timed = dlpt.time.func_stopwatch(my_function)
>>> my_function_timed(arg1, arg2)
>>> dlpt.time.get_last_measured_time_sec()
42.63
Returns:

User function wrapped in func_stopwatch().

dlpt.time.get_last_measured_time_sec() float

Return execution time of the last function, that was timed by using print_exec_time() or func_stopwatch() function.

Note: only valid after function calls. Otherwise, return None or a

previous time.

Returns:

Last timed function or None (if no function was timed before).