Page 1 of 1

TimePoint Pattern

Posted: Wed Jun 09, 2004 7:37 pm
by jason
Based on Martin Fowler's TimePoint pattern. It was implemented because I work a lot with dates, and I work alot with Date Ranges, and I will be implementing a DateRange (and will post it). Therefore, I needed a good TimePoint class.

Basically, does it work? Thoughts, ideas, or comments welcome.

Code: Select all

<?php

if ( !defined('ECLIPSE_TIMEPOINT') ) {
	
	define('ECLIPSE_TIMEPOINT', '');
	
	/**
	* TimePoint
	* Handles time points
	*
	* @package EclipseValues
	*/
	class TimePoint
	{
		var $seconds;
		var $minutes;
		var $hours;
		var $month_day;
		var $weekday_number;
		var $month_number;
		var $year;
		var $year_day;
		var $weekday_name;
		var $month_name;
		var $timestamp;

		/**
		* @description Handles time points
		*/
		function TimePoint ( $timestamp=false )
		{
			$this->_now($timestamp);
		}
		
		/**
		* addYears
		* @desc Add a number of years to the time
		* @access private
		* @param int $years
		* @return TimePoint
		*/
		function addYears ( $years )
		{
			return new TimePoint(mktime($this->hours, 
				$this->minutes, 
				$this->seconds, 
				$this->month_number, 
				$this->month_day, 
				$this->year + $years));
		}
		
		/**
		* minusYears
		* @desc Subtract a number of years from the time
		* @access private
		* @param int $years
		* @return TimePoint
		*/
		function minusYears ( $years )
		{
			return $this->addYears(-$years);
		}
		
		/**
		* addMonths
		* @desc Add a number of months to the time
		* @access public
		* @param int $months
		* @return TimePoint
		*/
		function addMonths ( $months )
		{
			return new TimePoint(mktime($this->hours, 
				$this->minutes, 
				$this->seconds, 
				$this->month_number + $months, 
				$this->month_day, 
				$this->year));
		}
		
		/**
		* minusMonths
		* @desc Subtract a number of months from the time
		* @access public
		* @param int $months
		* @return TimePoint
		*/
		function minusMonths ( $months )
		{
			return $this->addMonths(-$months);
		}
		
		/**
		* addDays
		* @desc Add a number of days to the time
		* @access public
		* @param int $days
		* @return TimePoint
		*/
		function addDays ( $days )
		{
			return new TimePoint(mktime($this->hours, 
				$this->minutes, 
				$this->seconds, 
				$this->month_number, 
				$this->month_day + $days, 
				$this->year));
		}
		
		/**
		* minusDays
		* @desc Subtract a number of days from the time
		* @access public
		* @param int $days
		* @return TimePoint
		*/
		function minusDays ( $days )
		{
			return $this->addDays(-$days);
		}
		
		/**
		* isAfter
		* @desc Whether the object is after the passed TimePoint
		* @access public
		* @param TimePoint $TimePoint
		* @return bool
		*/
		function isAfter ( $TimePoint )
		{
			return ($this->timestamp > $TimePoint->get_timestamp());
		}
		
		/**
		* isBefore
		* @desc Whether the object is before the passed TimePoint
		* @access public
		* @param TimePoint $TimePoint
		* @return bool
		*/
		function isBefore ( $TimePoint )
		{
			return ($this->timestamp < $TimePoint->get_timestamp());
		}
		
		/**
		* isEqualTime
		* @desc Whether the two TimePoints are equal to the second
		* @access public
		* @param TimePoint $TimePoint
		* @return bool
		*/
		function isEqualTime ( $TimePoint )
		{
			return ($this->timestamp == $TimePoint->get_timestamp());
		}
		
		/**
		* isSameDay
		* @desc Whether the two TimePoints occured on the same day
		* @access public
		* @param TimePoint $TimePoint
		* @return bool
		*/
		function isSameDay ( &$TimePoint )
		{
			$Date1 =& $this->trimToDays();
			$Date2 =& $TimePoint->trimToDays();
			
			return ($Date1->get_timestamp() == $Date2->get_timestamp());
		}
		
		/**
		* trimToDays
		* @desc Set's the precision to day
		* @access public
		* @return void
		*/
		function &trimToDays ()
		{
			return new TimePoint(mktime(0, 0, 0, 
				$this->month_number, 
				$this->month_day, 
				$this->year));
		}
		
		/**
		* _resetTime
		* @desc Whenever something is set, we need to reset the time
		* @access private
		* @return void
		*/
		function _resetTime ()
		{
			$this->_now(mktime($this->hours, 
				$this->minutes, 
				$this->seconds, 
				$this->month_number, 
				$this->month_day, 
				$this->year));
		}

		/**
		* now
		* @desc Returns a Date for the current date
		* @access public
		* @return TimePoint
		*/
		function now ()
		{
			return new TimePoint();
		}

		/**
		* _now
		* @desc Set's the value to the current date
		* @access private
		* @return void
		*/
		function _now ( $timestamp=false)
		{
			$stamp = $timestamp ? $timestamp : time();
			$date = getdate($stamp);
			$this->seconds = $date['seconds'];
			$this->minutes = $date['minutes'];
			$this->hours = $date['hours'];
			$this->month_day = $date['mday'];
			$this->weekday_number = $date['wday'];
			$this->month_number = $date['mon'];
			$this->year = $date['year'];
			$this->year_day = $date['yday'];
			$this->weekday_name = $date['weekday'];
			$this->month_name = $date['month'];
			$this->timestamp = $date['0'];
		}

		/**
		* @desc Setter for seconds
		* @access public
		* @return void
		* @param int seconds
		*/
		function set_seconds ( $seconds ) {
			$this->seconds = $seconds;
			$this->_resetTime();
		}

		/**
		* @desc Setter for minutes
		* @access public
		* @return void
		* @param int minutes
		*/
		function set_minutes ( $minutes ) {
			$this->minutes = $minutes;
			$this->_resetTime();
		}

		/**
		* @desc Setter for hours
		* @access public
		* @return void
		* @param int hours
		*/
		function set_hours ( $hours ) {
			$this->hours = $hours;
			$this->_resetTime();
		}

		/**
		* @desc Setter for month_day
		* @access public
		* @return void
		* @param int month_day
		*/
		function set_month_day ( $month_day ) {
			$this->month_day = $month_day;
			$this->_resetTime();
		}

		/**
		* @desc Setter for weekday_number
		* @access public
		* @return void
		* @param int weekday_number
		*/
		function set_weekday_number ( $weekday_number ) {
			$this->weekday_number = $weekday_number;
			$this->_resetTime();
		}

		/**
		* @desc Setter for month_number
		* @access public
		* @return void
		* @param int month_number
		*/
		function set_month_number ( $month_number ) {
			$this->month_number = $month_number;
			$this->_resetTime();
		}

		/**
		* @desc Setter for year
		* @access public
		* @return void
		* @param int year
		*/
		function set_year ( $year ) {
			$this->year = $year;
			$this->_resetTime();
		}

		/**
		* @desc Setter for year_day
		* @access public
		* @return void
		* @param int year_day
		*/
		function set_year_day ( $year_day ) {
			$this->year_day = $year_day;
			$this->_resetTime();
		}

		/**
		* @desc Setter for timestamp
		* @access public
		* @return void
		* @param int timestamp
		*/
		function set_timestamp ( $timestamp ) {
			$this->timestamp = $timestamp;
			$this->_resetTime();
		}

		/**
		* @desc Getter for seconds
		* @access public
		* @return int
		*/
		function get_seconds () { return $this->seconds; }

		/**
		* @desc Getter for minutes
		* @access public
		* @return int
		*/
		function get_minutes () { return $this->minutes; }

		/**
		* @desc Getter for hours
		* @access public
		* @return int
		*/
		function get_hours () { return $this->hours; }

		/**
		* @desc Getter for month_day
		* @access public
		* @return int
		*/
		function get_month_day () { return $this->month_day; }

		/**
		* @desc Getter for weekday_number
		* @access public
		* @return int
		*/
		function get_weekday_number () { return $this->weekday_number; }

		/**
		* @desc Getter for month_number
		* @access public
		* @return int
		*/
		function get_month_number () { return $this->month_number; }

		/**
		* @desc Getter for year
		* @access public
		* @return int
		*/
		function get_year () { return $this->year; }

		/**
		* @desc Getter for year_day
		* @access public
		* @return int
		*/
		function get_year_day () { return $this->year_day; }

		/**
		* @desc Getter for weekday_name
		* @access public
		* @return string
		*/
		function get_weekday_name () { return $this->weekday_name; }

		/**
		* @desc Getter for month_name
		* @access public
		* @return string
		*/
		function get_month_name () { return $this->month_name; }

		/**
		* @desc Getter for timestamp
		* @access public
		* @return int
		*/
		function get_timestamp () { return $this->timestamp; }
	}
}

?>

Posted: Wed Jun 09, 2004 8:07 pm
by feyd
slight nit-pick here: wouldn't it be better to use $this->get_timestamp() instead of $this->timestamp for OOP conformance and modularity in the isAfter() type functions?

Without running it, looks good to me.

Posted: Wed Jun 09, 2004 8:15 pm
by jason
I could, but I don't follow that rule of doing it ALL the time. It's something that's easily refactored, if needed. But if it's not needed, why do it that way? For me, if it's part of the class, and I don't forsee any major problems, I will usually make the call directly.

Posted: Wed Jun 09, 2004 8:42 pm
by feyd
same here.. I get bitched at for it sometimes.. :D