Incremental Game Time Format
Goal
Incremental games often have a countdown timer expressing the days, hours, minutes and seconds until a task is complete. Depending on the space available, they can be formatted as:
2d 13h
23h 59m 48s
14m
3h 0m 0s
The goal of this code golf is to write a function or program that performs this formatting.
Inputs
- The total number of seconds.
- The maximum number of segments to output.
Output
- Segments include:
- 0w weeks
- 0d days
- 0h hours
- 0m minutes
- 0s seconds
- Each segment is separated by a single space.
- Displayed segments must be contiguous. For example, you will not show hours and seconds without showing minutes, even if there are zero minutes.
- Single-digit values do not have leading zeros, though a value of zero must be shown as
0
. - Values are rounded down.
- The first segment displayed is the first non-zero value.
Test Cases
seconds segments output
0 1 0s
123 1 2m
123 2 2m 3s
123 3 2m 3s
82815 3 23h 0m 15s
307891 2 3d 13h
307891 4 3d 13h 31m 31s
604800 1 1w
604800 6 1w 0d 0h 0m 0s
Winning
The lowest byte-count solution in one week will win "acceptance".
Edits
- Clarified which segment is first, as shown in examples.
- Added test case 4 as per request.
307891 1
?0w
or1w
. \$\endgroup\$3d
? "The first segment displayed is the first non-zero value" \$\endgroup\$