Skip to content

Instantly share code, notes, and snippets.

@davidjmemmett
Created April 18, 2011 12:21
Show Gist options
  • Save davidjmemmett/925223 to your computer and use it in GitHub Desktop.
Save davidjmemmett/925223 to your computer and use it in GitHub Desktop.
<?php
class SlowClockCalculator {
private $target_hour = 0;
private $target_minute = 0;
private $slow = 0;
private $difference = 0;
private $result = 0;
public function __construct($target, $slow, $difference = 0) {
list($this->target_hour, $this->target_minute) = explode(':', $target);
$this->slow = $slow;
$this->difference = $difference;
}
private function getHour($i) {
return floor($i / 60);
}
private function getMinutes($i) {
return $i % 60;
}
private function targetIsMet($i) {
return $this->getHour($i) == $this->target_hour && $this->getMinutes($i) == $this->target_minute;
}
public function calculate() {
$a = 0; $b = 0;
for ($hour = 0; $hour < 24; $hour++) {
for ($minute = 0; $minute < 60; $minute++) {
if ($this->targetIsMet($b)) { return $this->result = $a + $this->difference; }
$a++; $b++;
if ($minute == 0) { $b -= $this->slow; }
}
}
}
public function getResult() {
return sprintf("%'02u:%'02u", $this->getHour($this->result), $this->getMinutes($this->result));
}
}
class SlowClockCalculatorTest extends PHPUnit_Framework_TestCase {
public static function data() {
return array (
array ('0:00', '0', '0', '00:00'),
array ('00:00', '0', '0', '00:00'),
array ('19:57', '3', '90', '22:30'),
array ('19:57', '3', '80', '22:20'),
array ('19:50', '3', '80', '22:13'),
);
}
/**
* @dataProvider data
*/
public function testCalculate($target, $slow, $difference, $result) {
$c = new SlowClockCalculator($target, $slow, $difference);
$c->calculate();
$this->assertEquals($result, $c->getResult());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment