<?php

/*
   +----------------------------------------------------------------------+
   | Timer Class                                                     	  |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | This class times scripts execution time.  Include it at the very top |
   | and bottom of all scripts that output information.  		  |
   | Example: include 'Timer.inc.php';					  |
   |	      $timer = new Timer();					  |
   |	      $timer->start();						  |
   |	          <<CODE>>						  |
   |	      $timer->stop();						  |
   |	      $timer->showDuration();					  |
   +----------------------------------------------------------------------+
	function start() {
	function stop() {
	function getDuration() {
	function showDuration() {
        function commentDuration() {
   +----------------------------------------------------------------------+
*/

class Timer {

  var $starttime;
  var $stoptime;
  var $duration;

        /*****************************************************************
         * Start the timer
	 *****************************************************************/
	function start() {
		$this->starttime = microtime();
	}

        /*****************************************************************
         * Stop the timer
	 *****************************************************************/
	function stop() {
		$this->stoptime = microtime();

		if (!$this->starttime) die ("Timer not Started! Can't Stop!");

			$start_u = substr($this->starttime,0,10);
			$start_s = substr($this->starttime,11,10);
			$stop_u  = substr($this->stoptime,0,10);
			$stop_s  = substr($this->stoptime,11,10);
			$start_total = doubleval($start_u) + $start_s;
			$stop_total  = doubleval($stop_u) + $stop_s;
			$this->duration = $stop_total - $start_total;
		}

        /*****************************************************************
         * Compute the timer duration
	 *****************************************************************/
	function getDuration() {
		if (!$this->duration) die ("No start and stop have been called!");
			return round($this->duration,3);
	}

        /*****************************************************************
         * Display the duration
	 *****************************************************************/
	function showDuration() {
		if (!$this->duration) die ("No start and stop have been called!");
			echo "\n$this->duration\n";
	}

        /*****************************************************************
         * Comment the duration
         *****************************************************************/
        function commentDuration() {
                if (!$this->duration) die ("No start and stop have been called!");
                        echo "\n<!--Script Executed in $this->duration --!>\n";
        }

}
?>
