Location: PHPKode > projects > Boardnukers VKP Aka TIS > htdocs/includes/pstimer.php
<?php
/**
 * PHP Snippets - A Library For PHP
 *
 * LIZENZ
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author Hinrich Donner
 * @copyright Hinrich Donner 2003
 * @license LGPL
 * @version $Revision: 1.2 $
 * @since 5.5.1
 * @package PHPSnippets
 * @subpackage Utilities
 * @category Time
 * @link http://sourceforge.net/projects/phpsnippets/
 */

/**
 * psTimer
 *
 * A simple timer class.
 */
class psTimer
{
    /**
     * _running
     *
     * Trigger the current state.
     *
     * @var bool
     * @access protected
     * @see phpsTimer::IsRunning()
     */
    var $_running = false;

    /**
     * start
     *
     * The start time or FALSE if not started.
     *
     * @var float
     * @see phpsTimer::Start(), phpsTimer::Stop()
     */
    var $start = false;

    /**
     * stop
     *
     * The stop time if started and stopped again or FALSE.
     *
     * @var float
     * @see phpsTimer::Start(), phpsTimer::Stop()
     */
    var $stop = false;

    /**
     * psTimer
     *
     * Constructor.
     */
    function psTimer()
    {
    }

    /**
     * GetMicroTime
     *
     * Returns the microtime as a string.  Taken from the PHP manual
     * example on microtime.  This method can be called without an
     * instance.
     *
     * @return float
     */
    function GetMicroTime()
    {
        list($usec, $sec) = explode(" ",microtime());
        return ((float)$usec + (float)$sec);
    }

    /**
     * Start
     *
     * Starts the timer.
     *
     * @return float The start time or FALSE if allready started
     * @see phpsTimer::Stop(), phpsTimer::Restart()
     */
    function Start()
    {
        if (!$this->IsRunning())
        {
            $this->start = $this->GetMicroTime();
            $this->stop = false;
            $this->_running = true;
        }
        return $this->start;
    }

    /**
     * Restart
     *
     * Restarts the timer.
     *
     * @return float The current time
     * @see phpsTimer::Start(), phpsTimer::Stop()
     */
    function Restart()
    {
        $this->start = $this->GetMicroTime();
        $this->_running = true;
        return $this->start;
    }

    /**
     * Stop
     *
     * Stop the timer.
     *
     * @return float The stop time or FALSE on error
     * @see phpsTimer::Start(), phpsTimer::Restart(), phpsTimer::Ellapsed()
     */
    function Stop()
    {
        if (!$this->IsRunning())
            return false;
        $this->stop = $this->GetMicroTime();
        $this->_running = false;
        return $this->stop;
    }

    /**
     * Ellapsed
     *
     * Returns the ellapsed time.
     *
     * @param int $round Maximum number of digits or -1 to get all
     * @return float The ellapsed time or FALSE on error
     * @see phpsTimer::Start(), phpsTimer::Stop()
     */
    function Ellapsed($round = -1)
    {
        if ($this->stop === false)
        {
            if ($this->start === false)
                return false;
            $result = $this->GetMicroTime() - $this->start;
        }
        else
            $result = $this->stop - $this->start;

        if ($round != -1)
            $result = round($result, (int) $round);
        return $result;
    }

    /**
     * IsRunning
     *
     * Returns the current state of the timer instance.
     *
     * @return bool TRUE or FALSE
     */
    function IsRunning()
    {
        return $this->_running;
    }

}

// $Log: pstimer.php,v $
// Revision 1.2  2004/07/02 08:10:37  hdonner
// - PHPDocu
//
// Revision 1.1  2004/04/25 10:14:25  hdonner
// - Small changes
//
// Revision 1.1  2004/04/12 14:42:46  hdonner
// - Move from BerliOS
//
// Revision 1.1  2003/11/10 13:10:44  hdonner
// - Zeitmessung fuer den Seitenaufbau als Makro in der Fusszeile
//
//
?>
Return current item: Boardnukers VKP Aka TIS