Advertisement
rfv123

ElapasedTiming - MicroTiming

Dec 9th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.76 KB | None | 0 0
  1. <?php
  2. namespace app\system;
  3.  
  4. class MicroTime {
  5.  
  6.     private $when = null;
  7.  
  8.     private $float = null;
  9.     private $seconds = null;
  10.     private $milliseconds = null;
  11.  
  12.     // Constructor for class
  13.     public function __construct($when = null) {
  14.         if (is_null($when)) {
  15.             $when = microtime();
  16.         }
  17.  
  18.         $this->when = $when;
  19.         $this->setSecMSec($when);
  20.     }
  21.  
  22.     public function elapsedTime()
  23.     {
  24.         return round((float) $this->seconds + $this->milliseconds / 1000.0, 3);
  25.     }
  26.  
  27.     public function seconds()
  28.     {
  29.         return $this->seconds;
  30.     }
  31.  
  32.     public function milliseconds()
  33.     {
  34.         return $this->milliseconds;
  35.     }
  36.  
  37.  
  38.     public function asfloat()
  39.     {
  40.         return $this->float;
  41.     }
  42.  
  43.     public function when()
  44.     {
  45.         return $this->when;
  46.     }
  47.  
  48.     public function asDateTime() {
  49.         return \DateTime::createFromFormat('U,u', $this->seconds .','. $this->milliseconds * 1000);
  50.     }
  51.  
  52.     // Format time to date and time
  53.     public function asDateTimeStr() {
  54.         $when = date_create_from_format('U', $this->seconds);
  55.         return sprintf("%s.%03u", $when->format('Y-m-d H:i:s'), $this->milliseconds);                  ;
  56.     }
  57.  
  58.     // always subtract the later from the earlier and change signs if required
  59.     public function diff(MicroTime $then)
  60.     {
  61.         // which is earlier?
  62.         $swapped = false;
  63.         $mtL = $this;
  64.         $mtE = $then;
  65.  
  66.         if ($mtL->float < $mtE->float) {
  67.             $swapped = true;
  68.             $mtE = $this;
  69.             $mtL = $then;
  70.         }
  71.         $diff = new self();
  72.         $diff->when = $mtL->when;
  73.         $diff->float = $mtL->float - $mtE->float;
  74.         $diff->seconds = (int) floor($diff->float);
  75.         $diff->milliseconds =  (int) round(($diff->float - $diff->seconds) * 1000.0, 0);
  76.  
  77.         if ($swapped) {
  78.             $diff->float = $diff->float * -1.0;
  79.             $diff->seconds = $diff->seconds * -1.0;
  80.             $diff->milliseconds = $diff->milliseconds * -1.0;
  81.         }
  82.         return $diff;
  83.     }
  84.  
  85.     /**
  86.      *
  87.      * @param type $when
  88.      * @return array [seconds, milliseconds]
  89.      */
  90.     private function setSecMSec($when)
  91.     {
  92.         if (is_float($when)) {
  93.             $this->float = $when;
  94.             $this->seconds = (int) floor($when);
  95.             $this->milliseconds =  (int) round(($when - $this->seconds) * 1000.0, 0);
  96.         }
  97.         else {
  98.             list($usec, $sec) = explode(' ', $when);
  99.             $this->seconds = (int) $sec;
  100.             $this->milliseconds =  (int) round($usec * 1000.0, 0);
  101.             $this->float = (float) $sec + (float) $usec;
  102.         }
  103.     }
  104.  
  105.     public function __get($name)
  106.     {
  107.         return $this->{$name};
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement