fn format_duration(duration: Duration) -> StringExpand description
⚙️ Function: Formats a Duration into a human-readable string.
This function takes a Duration and returns a string representing the remaining time in a human-readable format.
The function distinguishes between days, hours, and minutes, with specific rules for singular and plural terms.
If the remaining time is less than a minute, it returns “less than a minute”.
§Parameters:
duration: ADurationobject representing the time span to format. The function will extract the number of days, hours, and minutes from this duration to create a user-friendly time description.
§Returns:
String: A human-readable string indicating how much time is left, formatted as:- “in 1 day”, “in 1 day and X hours”, “in X hours”, “in X minutes”, or “less than a minute”. The string changes based on the length of the duration.
§⚠️ Notes:
- If the duration is greater than a day, the function formats the result as “in X days and Y hours”, or “in X days” if there are no remaining hours.
- If the duration is less than a day but more than an hour, the result is formatted as “in X hours”.
- For durations less than an hour but more than a minute, it returns “in X minutes”.
- If the duration is less than a minute, the function returns “less than a minute”.
§Example:
let duration = Duration::hours(5);
let formatted = format_duration(duration);
assert_eq!(formatted, "in 5 hours");
let short_duration = Duration::minutes(1);
let formatted_short = format_duration(short_duration);
assert_eq!(formatted_short, "in 1 minute");The function will return the appropriate formatted string based on the duration passed in.