16
4
Challenge
Write the shortest program that converts a human readable time interval to date components of the form:
{±YEARS|±MONTHS|±DAYS|±HOURS|±MINUTES|±SECONDS}
Sample cases
Each test case is two lines, input followed by output:
1 year 2 months 3 seconds
{1|2|0|0|0|3}
-2 day 5 year 8months
{5|8|-2|0|0|0}
3day 9 years 4 seconds -5 minute 4 years 4 years -3seconds
{17|0|3|0|-5|1}
Rules
- You can not use
strtotime
or any built-in function that does the whole job. - Shortest code wins (bytes)
- You can print your output to
stdout
or a file, the result can also be returned by a function, it's up to you - The token can be in singular or plural form.
- The components may be in a random order
- There may be no white space between the number and the token
- Sign is optional when the time interval is positive (input and output)
- If a component appears more than once the values should be added
- Each component has its own sign
- The components should be handled separately (e.g.
80 minutes
remains as 80 in the output) - The input is guaranteed to be lower case
Happy Golfing!
2I like this challenge but I'm having a hard time coming up with anything that isn't long and messy in languages which are ill-suited for code golf. :/ – Alex A. – 2015-02-09T22:36:30.453
Does output format matter? – Titus – 2017-01-07T12:14:37.563
Sign is optional when the time interval is positive
Does that mean that input may contain+
signs? – Titus – 2017-01-07T12:23:02.483