Právě prohlížíš: Timer.class.php
Zpět do složkyStáhnout
  1. <?php
  2. class Timer implements ArrayAccess/* Iterator */ {
  3. private $start = 0;
  4. private $startm = 0;
  5. private $last = 0;
  6. private $lastm = 0;
  7. public $laps = array(); //name => array(t,tm)
  8. public $floats = true;
  9. public function __construct() {
  10. $start = $this->getTime();
  11. $this->last=$this->start = $start[0];
  12. $this->lastm=$this->startm = $start[1];
  13. //$start[2] = "START";
  14. $laps["START"] = $start;
  15. //var_dump($start,$this->start, $this->startm);
  16. }
  17. public function lap($lapname=NULL) {
  18. if(is_string($lapname)&&strpos($lapname,"-")!==false)
  19. return trigger_error("Timer::lap() -> Lap name must not contain \"-\" /minus/ character!",E_USER_WARNING)&&false;
  20. $now = $this->getTime();
  21. $this->last = $now[0];
  22. $this->lastm = $now[1];
  23.  
  24. if(is_string($lapname)||is_int($lapname))
  25. $this->laps[$lapname]=$now;
  26. else
  27. $this->laps[]=$now;
  28. }
  29. public function __invoke($lapname=NULL) {
  30. $this->lap($lapname);
  31. }
  32. private function getTime() {
  33. $start = explode(" ",microtime());
  34. $r = array();
  35. $r[] = (int)$start[1];
  36. $r[] = (int)substr($start[0],2,9);
  37. return $r;
  38. }
  39. private function diffTime($time,$time2=null,$micro=true) {
  40. if(!is_array($time))
  41. $time = array((int)$time,0);
  42. if(!is_array($time2))
  43. $time2 = array($this->start,$this->startm);
  44. /*if($micro) {
  45.   $dm = $time2[1]-$time[1];
  46.   if($dm<0&&$time2[0]>=$time[0]) { //Pokud je mikro rozdil mensi nez 0
  47.   $time2[0]--;
  48.   $dm=1-$dm;
  49.   }
  50.   elseif($dm>0&&$time2[0]<=$time[0]) {
  51.   $time2[0]++;
  52.   $dm=$dm-1;
  53.   }
  54.   }
  55.   else $dm=0;
  56.   $d = abs($time2[0]-$time[0]); */
  57. $time = $time[0]+$time[1]/1000000000;
  58. $time2 = $time2[0]+$time2[1]/1000000000;
  59. $d = abs($time-$time2);
  60. //$dm = round(($d-floor($d))*1000000000);
  61. //$d = floor($d);
  62. return $this->toArray($d);
  63. }
  64. private function toFloat(array $time) {
  65. return $time[0]+$time[1]/1000000000;
  66. }
  67. private function toArray($time) {
  68. $dm = round(($time-floor($time))*1000000000);
  69. $d = floor($time);
  70. return array((int)$d,(int)$dm);
  71. }
  72. public function range($lap,$lap2,$float=true) {
  73. if(!isset($this->laps[$lap]))
  74. return trigger_error("Timer::range -> Lap \"$lap\" not found.",E_USER_WARNING)&&false;
  75. if(!isset($this->laps[$lap2]))
  76. return trigger_error("Timer::range -> Lap \"$lap2\" not found.",E_USER_WARNING)&&false;
  77. $diff = $this->diffTime($this->laps[$lap],$this->laps[$lap2]);
  78. if($float)
  79. return $diff[0]+$diff[1]/1000000000;
  80. return $diff;
  81. }
  82. public function started() {
  83. return array($this->start,$this->startm);
  84. }
  85. public function offsetSet($offset, $value) {
  86. if (is_null($offset)) $this->lap();
  87. else $this->lap($offset);
  88. }
  89. public function offsetExists($offset) {
  90. return isset($this->laps[$offset]);
  91. }
  92. public function offsetUnset($offset) {
  93. unset($this->laps[$offset]);
  94. }
  95. public function offsetGet($offset) {
  96. if(strpos($offset,"-")!==false) {
  97. $offset = explode("-",$offset);
  98. if($offset[0]=="")
  99. $offset[0] = $this->started();
  100. if($offset[count($offset)-1]=="")
  101. $offset[count($offset)-1] = $this->getTime();
  102. foreach($offset as &$val) {
  103. if(is_array($val))
  104. continue;
  105. if(!isset($this->laps[$val])) {
  106. trigger_error("Timer::[] -> offset \"$val\" does not exist!",E_USER_NOTICE);
  107. unset($val);
  108. continue;
  109. }
  110. $val = $this->laps[$val];
  111. }
  112. $diff = array();
  113. //$names = array_keys($offset);
  114. for($i=0; $i<count($offset)-1; $i++) {
  115. $diff[] = $this->floats?$this->toFloat($this->diffTime($offset[$i],$offset[$i+1])):$this->diffTime($offset[$i],$offset[$i+1]);
  116. }
  117. if(count($diff)==1)
  118. return $diff[0];
  119. //var_dump($offset);
  120. return $diff;
  121. }
  122. if($this->floats)
  123. return $this->toFloat($this->laps[$offset]);
  124. return $this->laps[$offset];
  125. }
  126. }
  127. ?>
  128.  
Parsed using GeSHi 1.0.8.11 at 0.068