18
1
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.
What is expected output for
307891 1
?0w
or1w
. – jnovacho – 2015-07-08T13:06:44.6331@jnovacho Wouldn't it be
3d
? "The first segment displayed is the first non-zero value" – Luigi – 2015-07-08T13:57:31.147@Luigi True. I've missed that. – jnovacho – 2015-07-08T14:03:53.200
Am I the only one thinking that this is a "could some one please write this code for me" question? – fho – 2015-07-09T09:50:09.653
It's not every day a code golf task might actually be useful. I say go with it :D – Geobits – 2015-07-09T20:13:16.307
@fho, if I wanted to write this function for production code, I would be writing it maintainable, not golfing it. ;-P – Hand-E-Food – 2015-07-10T04:09:40.140
@Hand-E-Food point taken ;) – fho – 2015-07-10T09:40:09.343