All challenges
Easy 0 solved 0% acceptance

Hours, Minutes, and Seconds

Convert a duration in seconds to a compact readable string.

Problem

Split a duration into hours, minutes, and seconds. Write units as h, m, and s, in that order, with one space between units. Omit any zero-valued unit, except that zero seconds is written as 0s.

Implement format_duration(seconds).

Input

seconds: The total duration in seconds.

Output

Return the formatted duration, for example 2h 2m 5s.

Constraints

0 ≤ seconds ≤ 1,000,000,000. Hours may exceed 23; this is a duration, not a time of day.

Examples

Example 1
Input:
seconds = 7325

Expected output:
expected = "2h 2m 5s"
Input:
long long seconds = 7325;

Expected output:
std::string expected = "2h 2m 5s";
Input:
long seconds = 7325;

Expected output:
string expected = "2h 2m 5s";

Explanation

7325 = 2 × 3600 + 2 × 60 + 5.

Example 2
Input:
seconds = 120

Expected output:
expected = "2m"
Input:
long long seconds = 120;

Expected output:
std::string expected = "2m";
Input:
long seconds = 120;

Expected output:
string expected = "2m";

Explanation

120 seconds is exactly 2 minutes. Omit the zero hours and seconds.

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.