views:

315

answers:

1

I found leettime via twitter, and it's novel enough for me to want to mess around with it/share it with you guys. To my dismay this version doesn't do seconds. It also seems like there might be a more efficient way to generate the time, what do you think?

Leet Time

Now, how is this leettime calculated?

For a given humantime (e.g. 11:15 am) the value of the leettime corresponds to the number of times you have to add up 13 hours 37 minutes in order to exactly reach this time. Whenever the result overflows during the calculation (i.e. the obtained time exceeds 23:59 o'clock) you subtract 24 hours in order to stay on the clock.

Example:

The leettime of 00:00 is 0, the leettime of 13:37 is 1 and leettime 2 corresponds to 03:14 am (because 13:37 plus 13 hours 37 minutes is 03:14 o'clock).

Is there a unique leettime for every humantime during the day?

Yes! There are exactly 24*60=1440 different leettimes, each corresponds to exactly one minute in the day.

Add whatever else you think would be cool, I'm sure this guy would love it.

I put the PHP version up figuring it'd be the most accessible and portable. The other versions are available here.

<?php
/**
 * Converts standard time to leettime.
 * 
 * @param int h the hour in standard time
 * @param int m the minute in standard time
 * 
 * @return int the leettime or -1 if the input
 *         parameters are invalid
 */
function TimeToLeet($h, $m){
  if(!is_numeric($h) || !is_numeric($m) ||
    $h > 23 || $h < 0 || $m > 59 || $m < 0)
      return -1;

  $curm = 0;
  $curh = 0;
  $i = 0;
  while ($curm != $m || ($curh % 24) != $h){  
    if($curm < 23){
      $curh += 13;
      $curm += 37;      
    } else {
      $curh += 14;
      $curm -= 23;      
    }
    ++$i;
  }
  return $i;  
}

/**
 * Converts leettime to standard time.
 * 
 * @param int leet the time in leettime-format in the
 *        range from 0 - 1439
 * 
 * @return var an int-Array with the hours at position 0
 * and minutes at position 1 (-1 if the input parameter
 * is not in range)
 */
function LeetToTime($leet){
  if($leet > 1439 || $leet < 0) return array(-1, -1);
  $m = $leet * 37;
  $h = ($leet * 13) + ($m / 60);
  return array($h % 24, $m % 60);
}

// Demonstrate usage

$h = (int)date("G");
$m = (int)date("i");

echo date("H:i")." = ".TimeToLeet($h,$m);

echo "
";

$leettime = 999;
$result = LeetToTime($leettime);
if($result[0] == -1) echo "error";
else {
  $h = $result[0];
  $m = $result[1];
  echo $leettime." = ".($h>9?$h:"0".$h) . 
    ":".($m>9?$m:"0".$m);
}
?>
+14  A: 

For a faster conversion from HH:MM to Leet, try Modular Arithmetic:

First, convert 13:37 to a number mod 1440; specifically, 13*60+37 = 817

Find the inverse of 817 mod 1440. Result is 913 (stolen from Wolfram Alpha). 913 has the property that 913*817 == 1 mod 1440.

Alternatively, if you wanted to use 13 minutes, 37 seconds; you would work mod 86400, and 817^-1 == 67153.

Convert your target time to a number mod 1440; then multiply by 913 mod 1440 to get its corresponding leet time. To convert back, multiply by 817.

Example:

Going from leet time to normal time (accuracy to the minute):
someLeetTime = 1337
//where 817 = 13*60 + 37 = 13:37 in minutes
timeInMinutes = someLeetTime * 817 mod 1440 = 809
hours = timeInMinutes / 60 = 13
minutes = timeInMinutes % 60 = 29

Going from normal time to leet time (accuracy to the minute):
hours = 13
minutes = 29
timeInMinutes = hours * 60 + minutes = 809
//913 = 817^-1 mod 1440
someLeetTime = timeInMinutes * 913 mod 1440 = 1337 
13:29 is 1337 leet time

And, one possible implementation for seconds, where in 13:37 is treated as 13 minutes, 37 seconds. (Technically violates the standard, but we are l33t, and can do that)

Going from leet time to normal time (accuracy to the second):
someLeetTime = 1337
//where 817 = 13*60 + 37 = 13:37 in seconds
timeInSeconds = someLeetTime * 817 mod 86400 = 55529
hours = timeInSeconds / 3600 = 15
minutes = (timeInSeconds % 3600) / 60 = 25
seconds = timeInSeconds % 60 = 29

1337 in the new system is 15:25:29

Going from normal time to leet time (accuracy to the second):
hours = 15
minutes = 25
seconds = 29
timeInSeconds = hours * 3600 + minutes*60 + seconds = 55529
//67153 = 817^-1 mod 86400
someLeetTime = timeInSeconds * 67153 mod 86400 = 1337 
15:25:29 is 1337 leet time

Thank you Crypto, and Modular Arithmetic.

For the furtherance of this great cause, I have built a clock! Well... 6 actually.

CoderTao
A post like this is EXACTLY what I had hoped for by asking here. I officially respect this website so much for being representative of how awesome the internet is.
Sneakyness
Can you explain why it's faster?
Sneakyness
It's faster because it doesn't require a while loop; in the case of 8:51, the while loop in the original php would have required some few hundred executions; the listed algorithm, however, executes in relatively constant time, with no execution branches or other strange items.
CoderTao
/It's quite literally a small handful of additions, multiplications, and mod's.
CoderTao
Interesting. Times like these are when I can definitely feel that my lack of math experience (just never cared about it in high school past algebra) is holding me back a little bit, but I seem to be doing fine picking it up as I go.
Sneakyness
@Sneakyness: I don't think you're gonna get better answers than this one, you might as well accept it.
Alix Axel
I'm holding out to get the most out of my bounty. What I'm most interested in seeing is an implementation of the seconds to four places / converted to leet as well. I'm not sure how to go about doing this myself. I'll admit that my biggest weakness in programming is doing something like this, when I'm unsure how to obtain a (usually important) value and do what I want with it.
Sneakyness
I believe I mentioned that the same basic process works with 13 minutes, 37 seconds as well... could probably be a bit clearer of course
CoderTao
...actually, wait... what do you mean by seconds to four places? Seconds since the hour has started?
CoderTao
four places like 1.000 seconds. So 14:23:6403 == hh:mm:ssss, 6403 being 64.03 seconds. We're so leet we need to be that accurate. This would also enable the use of LeetTime in timed events, such as Rally Racing or Track.
Sneakyness
We are leet enough to have a 64 second minute? Awesome... (a link with a few example clocks has been added... there might be a more amazing way to combine them though).
CoderTao
I mixed that up because of the decimals afterwards. :X heh.
Sneakyness