parse_timedelta

safir.datetime.parse_timedelta(text)

Parse a string into a datetime.timedelta.

Expects a string consisting of one or more sequences of numbers and duration abbreviations, separated by optional whitespace. Whitespace at the beginning and end of the string is ignored. The supported abbreviations are:

  • Week: weeks, week, w

  • Day: days, day, d

  • Hour: hours, hour, hr, h

  • Minute: minutes, minute, mins, min, m

  • Second: seconds, second, secs, sec, s

If several are present, they must be given in the above order. Example valid strings are 8d (8 days), 4h 3minutes (four hours and three minutes), and 5w4d (five weeks and four days).

If you want to accept strings of this type as input to a timedelta field in a Pydantic model, use the HumanTimedelta type as the field type. It uses this function to parse input strings.

Parameters:

text (str) – Input string.

Returns:

Converted datetime.timedelta.

Return type:

datetime.timedelta

Raises:

ValueError – Raised if the string is not in a valid format.