29
3
Write a short program which takes in a positive number of seconds representing an age, and outputs an estimate of that time in English.
Your program must output the least precise amount of time which has passed, among the following metrics and their lengths in seconds:
second = 1
minute = 60
hour = 60 * 60
day = 60 * 60 * 24
week = 60 * 60 * 24 * 7
month = 60 * 60 * 24 * 31
year = 60 * 60 * 24 * 365
Examples
input : output
1 : 1 second
59 : 59 seconds
60 : 1 minute
119 : 1 minute
120 : 2 minutes
43200 : 12 hours
86401 : 1 day
1815603 : 3 weeks
1426636800 : 45 years
As you can see above, after the time of say, 1 day (60 * 60 * 24 = 86400 seconds), we no longer output minute(s) or hour(s), but only days until we surpass the time of one week, and so on.
Consider the given length of time to be an age. For example, after 119 seconds, 1 minute has passed, not 2.
Rules
- No specification for 0 or negative inputs.
- Follow proper pluralization. Every measure greater than 1 must include an
s
following the word. - You may not use a pre-existing library which serves the function of the entire program.
- This is a code golf, shortest program wins the internet points.
- Have fun!
3I don't understand how we choose a unit or amount. Do we round? – xnor – 2018-03-18T02:35:00.207
1@xnor we integer divide and use the smallest non-zero value along with its unit (possibly pluralised). Hence 59 -> "59 seconds" and 86401 -> "1 day". – Jonathan Allan – 2018-03-18T03:21:09.000
5
Welcome to PPCG! Nice first challenge. For future reference there is a sandbox which is useful for getting feedback before posting to main.
– Jonathan Allan – 2018-03-18T03:24:10.000It's too bad that weeks are included 'cause if they weren't there would be a 33 byte Mathematica solution:
Floor[DateObject@#-DateObject@0]&
. – DanTheMan – 2018-03-18T03:49:27.847@DanTheMan That's neat! Just tried it out and it doesn't appear to count for no pluralization though. – bitconfused – 2018-03-18T03:54:48.160
4
Note that Do X without Y is discouraged, as well as Non-observable program requirement.
– user202729 – 2018-03-18T14:02:53.7301How should we round the numbers? Should 119 seconds be 1 minute or 2 minutes? What about 90? – user202729 – 2018-03-18T14:19:27.287
@user202729 Is that all in regard to my rule against using a library which "serves the function of the entire program"? I see that on quite a few other codegolfs, it seemed sensible. Also, as Johnathan Allan said, always round down to the nearest increment of the unit. 119 and 90 both round down to 60 = 1 minute. – bitconfused – 2018-03-18T18:24:42.553
1Postgres has a near built-in for this (
justify_interval
). – Denis de Bernardy – 2018-03-18T21:07:03.5631
Rules should be put in the challenge, not comment. See Changing the challenge in the comments.
– user202729 – 2018-03-19T01:13:49.123@user202729 what change? Could you be more specific? – bitconfused – 2018-03-19T05:10:50.790
The "integer-divide" (round down) part. – user202729 – 2018-03-19T05:11:36.133
Whether one uses "integer division" is implementation specific. Since Jonathan's comment and your question I've made clarifications within the challenge. Thanks! – bitconfused – 2018-03-19T05:16:26.917
But then, you should specify that the time should be rounded down. – user202729 – 2018-03-19T05:17:33.150
1My initial assumption was that people would understand that after 90 seconds, 1 minute has passed but 2 minutes haven't. It's one minute old, not two. Similar to how someone's age counts only how many years have passed since their birth. I have now edited the challenge, thanks again. – bitconfused – 2018-03-19T05:27:35.613