Location: PHPKode > projects > PhpiCalLib > dateordatetimeproperties.php
<?php
/**
 * dateordatetimeproperties.php: iCalendar date-time / date property classes
 * 
 * This program 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.
 * 
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @copyright Copyright (C) 2008 Nigel Swinson, hide@address.com 
 * @author  Nigel Swinson
 * @package PhpiCalLib
 * @version 1.0
 */

require_once 'unencodedproperties.php';

/**
 * Many properties specify a value of type date-time / date.  This base class can be used for those.
 *
 */
class PhpiCalLib_Properties_DateOrDateTimeProperty extends PhpiCalLib_Properties_UnencodedProperty {
	/**
	 * The current type of this property, either PHPICALLIB_DATATYPE_DATE or PHPICALLIB_DATATYPE_DATE_TIME
	 * @var integer
	 */
	private $DataType = 0;
	/**
	 * If {@link DataType} is PHPICALLIB_DATATYPE_DATE_TIME, this will be a {@link PhpiCalLib_DataType_DateTime} object.
	 *
	 * @var PhpiCalLib_DataType_DateTime
	 */
	private $DateTime = null;
	/**
	 * If {@link DataType} is PHPICALLIB_DATATYPE_DATE, this will be a {@link PhpiCalLib_DataType_Date} object.
	 *
	 * @var PhpiCalLib_DataType_Date
	 */
	private $Date = null;
	
	/**
	 * Access the Date of the value.
	 *
	 * @return PhpiCalLib_DateType_Date
	 */
	public function GetDateValue() {
		if ($this->DataType == PHPICALLIB_DATATYPE_DATETIME) {
			return $this->DateTime->GetDate();
		} else {
			return $this->Date;
		}
	}

	/**
	 * Access the Time of the value.  This will return null if the value is a date not a date-time
	 *
	 * @return PhpiCalLib_DateType_Time or null
	 */
	public function GetTimeValue() {
		if ($this->DataType != PHPICALLIB_DATATYPE_DATETIME) {
			return null;
		}
		return $this->DateTime->GetTime();
	}

	/**
	 * Access the date-time of the value.  This will return null if the value is a date not a date-time
	 *
	 * @return PhpiCalLib_DateType_DateTime or null
	 */
	public function GetDateTimeValue() {
		if ($this->DataType != PHPICALLIB_DATATYPE_DATETIME) {
			return null;
		}
		return $this->DateTime;
	}
	
	/**
	 * Return a timestamp WRT to UTC.
	 * 
	 * If the value is a Date, then it will be set to midnight.
	 *
	 * @return integer Number of seconds since 1970 UTC
	 */
	public function ToTimeStamp() {
		if ($this->Date) {
			return $this->Date->ToTimeStamp();
		} else {
			return $this->DateTime->ToTimeStamp();
		}
	}
	
	/**
	 * Set to a date-time property as represented by the given timestamp
	 *
	 * @param number $Time Unix timestamp
	 * @param bool $Utc true to store in utc, false to store in "floating time", TZ specified by the TZID parameter
	 */
	public function SetDateTimeFromTimeStamp($Time, $Utc) {
		// Set the type to be a datetime
		$DateTime = new PhpiCalLib_DataTypes_DateTime();
		$DateTime->FromTimeStamp($Time, $Utc);
		$this->SetDateTimeValue($DateTime);
	}

	/**
	 * Set the value as a DateTime
	 *
	 * @param PhpiCalLib_DateType_DateTime $DateTime
	 */
	public function SetDateTimeValue($DateTime) {
		// Call the parent directly to avoid recursion
		parent::SetEncodedValue($DateTime->ToString());
		$this->DataType = PHPICALLIB_DATATYPE_DATE_TIME;
		$this->DateTime = $DateTime;
		$this->Date = null;
		$this->RemoveParameter(PHPICALLIB_PARAMETER_VALUE);
	}
	
	/**
	 * Set to a date-time property as represented by the given timestamp
	 *
	 * @param number $Time Unix timestamp
	 */
	public function SetDateFromTimeStamp($Time) {
		$Date = new PhpiCalLib_DataTypes_Date();
		$Date->FromTimeStamp($Time);
		$this->SetDateValue($Date);
	}
	
	/**
	 * Set the value as a Date
	 *
	 * @param PhpiCalLib_DateType_Date $Date
	 */
	public function SetDateValue($Date) {
		// Call the parent directly to avoid recursion
		parent::SetEncodedValue($Date->ToString());
		$this->DataType = PHPICALLIB_DATATYPE_DATE;
		$this->DateTime = null;
		$this->Date = $Date;
		// Set the parameter to indicate it's a date
		$Value = new PhpiCalLib_Parameters_Value();
		$Value->SetType(PHPICALLIB_PARAMETER_VALUE);
		$Value->FromDataType(PHPICALLIB_DATATYPE_DATE);
		$this->SetParameter($Value);
	}
	
	/**
	 * Overridden to do extra parsing to extract the $TextValue
	 */
	public function SetEncodedValue($Text) {
		// Find out what value type we are expecting and call the relevant SetValue method.
		$Value = $this->GetParameter(PHPICALLIB_PARAMETER_VALUE);
		if (!$Value || ($Value->GetValue() == 'DATE-TIME')) {
			$DateTime = new PhpiCalLib_DataTypes_DateTime();
			$DateTime->Parse($Text);
			$this->SetDateTimeValue($DateTime);
		} else {
			$Date = new PhpiCalLib_DataTypes_Date();
			$Date->Parse($Text);
			$this->SetDateValue($Date);
		}
	}
}


/**
 * The DTEND property.
 * <pre>
 *        dtend      = "DTEND" dtendparam ":" dtendval CRLF
 *
 *        dtendparam = *(
 *                   ; the following are OPTIONAL,
 *                   ; but MUST NOT occur more than once
 *                   (";" "VALUE" "=" ("DATE-TIME" / "DATE")) /
 *                   (";" tzidparam) /
 *
 *                   ; the following is OPTIONAL,
 *                   ; and MAY occur more than once
 *                   (";" other-param)
 *
 *                   )
 *
 *        dtendval   = date-time / date
 *        ;Value MUST match value type
 * </pre>
 * @link http://tools.ietf.org/html/draft-ietf-calsify-rfc2445bis-08#section-3.8.2.2
 */
class PhpiCalLib_Properties_DtEndProperty extends PhpiCalLib_Properties_DateOrDateTimeProperty {
	public function __construct() {
		parent::__construct();
		$this->SetType(PHPICALLIB_PROPERTY_DTEND);
	}
}

/**
 * The DUE property.
 * <pre>
 *        due        = "DUE" dueparam ":" dueval CRLF
 *
 *        dueparam   = *(
 *
 *                   ; the following are OPTIONAL,
 *                   ; but MUST NOT occur more than once
 *                   (";" "VALUE" "=" ("DATE-TIME" / "DATE")) /
 *                   (";" tzidparam) /
 *
 *                   ; the following is OPTIONAL,
 *                   ; and MAY occur more than once
 *                   (";" other-param)
 *                   )
 *
 *        dueval     = date-time / date
 *        ;Value MUST match value type
 * </pre>
 * @link http://tools.ietf.org/html/draft-ietf-calsify-rfc2445bis-08#section-3.8.2.3
 */
class PhpiCalLib_Properties_DueProperty extends PhpiCalLib_Properties_DateOrDateTimeProperty {
	public function __construct() {
		parent::__construct();
		$this->SetType(PHPICALLIB_PROPERTY_DUE);
	}
}

/**
 * The DTSTART property.
 * <pre>
 *        dtstart    = "DTSTART" dtstparam ":" dtstval CRLF
 *
 *        dtstparam  = *(
 *                   ; the following are OPTIONAL,
 *                   ; but MUST NOT occur more than once
 *                   (";" "VALUE" "=" ("DATE-TIME" / "DATE")) /
 *                   (";" tzidparam) /
 *
 *                   ; the following is OPTIONAL,
 *                   ; and MAY occur more than once
 *                   (";" other-param)
 *
 *                   )
 *
 *        dtstval    = date-time / date
 *        ;Value MUST match value type
 * </pre>
 * @link http://tools.ietf.org/html/draft-ietf-calsify-rfc2445bis-08#section-3.8.2.4
 */
class PhpiCalLib_Properties_DtStartProperty extends PhpiCalLib_Properties_DateOrDateTimeProperty {
	public function __construct() {
		parent::__construct();
		$this->SetType(PHPICALLIB_PROPERTY_DTSTART);
	}
}

/**
 * The RECURRENCE-ID property.
 * <pre>
 *          recurid    = "RECURRENCE-ID" ridparam ":" ridval CRLF
 *
 *        ridparam   = *(
 *                   ; the following are OPTIONAL,
 *                   ; but MUST NOT occur more than once
 *                   (";" "VALUE" "=" ("DATE-TIME" / "DATE")) /
 *                   (";" tzidparam) / (";" rangeparam) /
 *
 *                   ; the following is OPTIONAL,
 *                   ; and MAY occur more than once
 *                   (";" other-param)
 *
 *                   )
 *
 *        ridval     = date-time / date
 * </pre>
 * @link http://tools.ietf.org/html/draft-ietf-calsify-rfc2445bis-08#section-3.8.4.4
 */
class PhpiCalLib_Properties_RecurrenceIdProperty extends PhpiCalLib_Properties_DateOrDateTimeProperty {
	public function __construct() {
		parent::__construct();
		$this->SetType(PHPICALLIB_PROPERTY_RECURRENCE_ID);
	}
}

/**
 * The EXDATE property.
 * <pre>
 *        exdate     = "EXDATE" exdtparam ":" exdtval *("," exdtval) CRLF
 *
 *        exdtparam  = *(
 *                   ; the following are OPTIONAL,
 *                   ; but MUST NOT occur more than once
 *                   (";" "VALUE" "=" ("DATE-TIME" / "DATE")) /
 *                   (";" tzidparam) /
 *
 *                   ; the following is OPTIONAL,
 *                   ; and MAY occur more than once
 *                   (";" other-param)
 *
 *                   )
 *
 *        exdtval    = date-time / date
 *        ;Value MUST match value type
 * </pre>
 * @link http://tools.ietf.org/html/draft-ietf-calsify-rfc2445bis-08#section-3.8.5.1
 */
class PhpiCalLib_Properties_ExDateProperty extends PhpiCalLib_Properties_DateOrDateTimeProperty {
	public function __construct() {
		parent::__construct();
		$this->SetType(PHPICALLIB_PROPERTY_EXDATE);
	}
}

?>
Return current item: PhpiCalLib