auditok.util.make_duration_formatter

auditok.util.make_duration_formatter(fmt)[source]

Make and return a function used to format durations in seconds. Accepted format directives are:

  • %S : absolute number of seconds with 3 decimals. This direction should be used alone.
  • %i : milliseconds
  • %s : seconds
  • %m : minutes
  • %h : hours

These last 4 directives should all be specified. They can be placed anywhere in the input string.

Parameters:fmt (str) – duration format.
Returns:formatter – a function that takes a duration in seconds (float) and returns a string that corresponds to that duration.
Return type:callable
Raises:TimeFormatError – if the format contains an unknown directive.

Examples

Using %S:

formatter = make_duration_formatter("%S")
formatter(123.589)
'123.589'
formatter(123)
'123.000'

Using the other directives:

formatter = make_duration_formatter("%h:%m:%s.%i")
formatter(3600+120+3.25)
'01:02:03.250'

formatter = make_duration_formatter("%h hrs, %m min, %s sec and %i ms")
formatter(3600+120+3.25)
'01 hrs, 02 min, 03 sec and 250 ms'

# omitting one of the 4 directives might result in a wrong duration
formatter = make_duration_formatter("%m min, %s sec and %i ms")
formatter(3600+120+3.25)
'02 min, 03 sec and 250 ms'