<?php
/**
* This file is part of the easy-CMS project.
* easy-CMS 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 2 of the License, or
* (at your option) any later version.
* 
* easy-CMS 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 easy-CMS; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/**
* Allows time operations and outputs.
*
* @access   private
* @author	Cedric Francoys <cedric.francoys@easy-cms.org>
* @version	Time.class, Sun Nov 28 13:28:08 CET 2004
* @package	Types
*/
class Time {

	var $timeStamp;

	function Time($time, $format='H:i:s'){
    $this->setValue($time, $format);
 	}   

  function setValue($time, $format='H:i:s') {
    list($hour, $minute, $second) = sscanf($time,"%02d:%02d:%02d");
    $this->timeStamp = mktime($hour, $minute, $second, 0, 0, 0);
  }

  function getValue() {
    return sprintf("%02d:%02d:%02d", 	$this->getHour(), $this->getMinute(), $this->getSecond());
  }

  function compareTo(&$timeObj) {
		if ($timeObj->timeStamp == $this->timeStamp) {
			return 0;
    }
		if ($timeObj->timeStamp > $this->timeStamp) {
			return -1;
    }
		return 1;
  }

  function getType() {
    return EASY_CMS_TYPE_TIME;
  }
		
	function getHour(){
			return date("H", $this->timeStamp);
	}
	
	function getMinute(){
			return date("i", $this->timeStamp);	
	}

	function getSecond(){
			return date("s", $this->timeStamp);	
	}

	function getTime(){
			return sprintf("%02d:%02d:%02d", $this->getHour(), $this->getMinute(), $this->getSecond());
	}

	function addHours($hours){
		$this->timeStamp = mktime($this->getHour()+$hours, $this->getMinute(), $this->getSecond(), 0, 0, 0);
	}

	function addMinutes($minutes){
		$this->timeStamp = mktime($this->getHour(), $this->getMinute()+$minutes, $this->getSecond(), 0, 0, 0);
	}
	
	function addSeconds($seconds){
		$this->timeStamp = mktime($this->getHour(), $this->getMinute(), $this->getSecond()+$seconds, 0, 0, 0);
	}

	function toInt(){
    return $this->getHour().$this->getMinute().$this->getSecond();
	}

  function toXML() {
    return '<![CDATA['.$this->getValue().']]>';
  }

  function toSQL() {
		return $this->html_encode($this->getValue());
  }

	function toString(){
    return $this->getValue();
	}    
}	
?>