15

1

# Introduction

Time is confusing. Sixty seconds to a minute, sixty minutes to an hour, twenty-four hours to a day (and not to mention that pesky am/pm!).

There's no room for such silliness nowadays, so we've decided to adopt the only sensible alternative: decimal days! That is to say, each day is considered 1 whole unit, and anything shorter is written as a decimal fraction of that day. So, for example: "12:00:00" would be written as "0.5", and "01:23:45" might be written as "0.058159".

Because it will take time to get used to the new system, you are tasked with writing a program that can convert between them in both directions.

# Challenge

Write a program in the language of your choice, that given a modern time in the ISO-8601 format of "hh:mm:ss", will return the equivalent decimal fraction unit. Likewise, given a decimal fraction, the program should return the time in the modern format initially specified.

You can make the following assumptions:

- The modern time input and output can range from "00:00:00" to "24:00:00"
- The decimal time input and output can range from "0" to "1", and should be able to accept/output up to at least 5 decimal places (such as "0.12345"). More precision is acceptable
- The program should be able to know which conversion direction to perform based on input
- You cannot use time related functions/libraries

The winner will be determined by the shortest code that accomplishes the criteria. They will be selected in at least 7 decimal day units, or if/when there have been enough submissions.

# Examples

Here's a(n intentionally) poorly written piece of JavaScript code to be used as an example:

```
function decimalDay(hms) {
var x, h, m, s;
if (typeof hms === 'string' && hms.indexOf(':') > -1) {
x = hms.split(':');
return (x[0] * 3600 + x[1] * 60 + x[2] * 1) / 86400;
}
h = Math.floor(hms * 24) % 24;
m = Math.floor(hms * 1440) % 60;
s = Math.floor(hms * 86400) % 60;
return (h > 9 ? '' : '0') + h + ':' + (m > 9 ? '' : '0') + m + ':' + (s > 9 ? '' : '0') + s;
}
decimalDay('02:57:46'); // 0.12344907407407407
decimalDay('23:42:12'); // 0.9876388888888888
decimalDay(0.5); // 12:00:00
decimalDay(0.05816); // 01:23:45
```

Hmm... 60 is

almost64. I wonder what time would be like if there were 64 seconds in a minute and 64 minutes in an hour (and 16 or 32 hours in a day). – None – 2015-07-09T16:53:06.2931Do we have to handle leap seconds? so 23:59:60 is 1 second from the end of a 86401 second day? – Sparr – 2015-07-10T03:52:30.147

1@Sparr No need to worry about leap seconds. This is the future, where we have decided that it's silly for a second to be considered an absolute value while also tying it to the relative speed of the rotation of the earth ;) – Mwr247 – 2015-07-13T14:24:29.123

1@MichaelT It'd be a programmers dream world =P – Mwr247 – 2015-07-13T14:39:58.420

1@Mwr247 yep. DNS TTL has (had?) a field that is

`n`

where`n`

is 2^n seconds. So a value of '6' had a TTL of about 1 minute. A value of '12' had a TTL of about 1 hour. '15' was about 8 hours and so on. It allowed one byte to define the timeout and give you enough control for short or long times. – None – 2015-07-13T14:52:22.267For those looking for loopholes, note that MicroSoft Excel and LibreOffice Calc already use floating point as their internal representations of dates and times! They both have scripting languages built-in. – None – 2015-07-13T16:01:33.580