PHP - 115 102 bytes
A solution in 155 bytes (wrapped here on 3 lines for readability):
$a=$argv[1];
$h=($a-($m=($a=($a-($s=($a=($a-($t=($a=($a-($u=$a%1000))/1000)%1000))/1000)%60))/60)%60))/60;
printf("%02d:%02d:%02d:%03d:%03d",$h,$m,$s,$t,$u);
The second line computes (from inside to outside) the exact values of the components starting with the microseconds.
The shorter version (115 bytes, wrapped on two lines for readability):
$u=$argv[1];$h=($m=($s=($t=$u/1000)/1000)/60)/60;
printf("%02d:%02d:%02d:%03d:%03d",$h,$m%60,$s%60,$t%1000,$u%1000);
It also uses embedded assignments to compute the convert the input number of microseconds in milliseconds, seconds, minutes and hours using floating point numbers. The modulus operator (%
) and the decimal number format (%d
) of printf()
is then used to force them to integer numbers (the fractional part is ignored).
Another solution that uses the date functions (102 bytes)
$u=$argv[1];
echo gmdate("H:i:s",strtotime("@".substr($u,0,-6))),":",substr($u,-6,3),":",substr($u,-3);
The hours:minutes:seconds part is handled by the PHP date functions gmdate()
and strtotime()
, the milli- and micro-seconds are extracted as strings from the input value.
Usage:
$ php -r '$u=$argv[1];echo gmdate("H:i:s",strtotime("@".substr($u,0,-6))),":",substr($u,-6,3),":",substr($u,-3);' 7198898787; echo
01:59:58:898:787
1I realise now that this isn't really a standard format for writing times, and
hh:mm:ss.000000
would probably have been better (and easier). Still, can't go changing it now. – Sam – 2015-07-06T21:43:11.9971Out of curiosity, what was the SO post? – Digital Trauma – 2015-07-06T23:25:52.937
@DigitalTrauma http://stackoverflow.com/questions/31251377 by a relatively new user. A correct answer had already been chosen, I was just playing around in IDLE and came up with a grotesque-looking dictionary comprehension that wasn't a particularly good answer to the question. Someone saw it and pointed out this site in a comment. I came here, wrote a question (slightly different to the SO post), and also wrote a much improved version of my answer (which I haven't posted, and which is now redundant to all the much more compact and imaginative answers below).
– Sam – 2015-07-07T07:19:19.980Is there a limit on the number of hours in the input? – FUZxxl – 2015-07-07T10:19:51.020
Yes, arbitrarily I made it <= 86400000000 microsec, so <= 24 hours. – Sam – 2015-07-07T10:25:59.503
Sure would have saved me quite a few characters in my JavaScript answer if ISO format with the dot was allowed :/ Interesting challenge nonetheless :) – rink.attendant.6 – 2015-07-07T21:10:16.490
Welcome to PPCG! :) – undergroundmonorail – 2015-07-07T23:39:43.180