eZ Publish  [trunk]
eztime.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZTime class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZTime eztime.php
00013   \ingroup eZLocale
00014   \brief Locale aware time handler
00015 
00016   eZTime handles 24 hour time values in hours, minutes and seconds.
00017   The time stored as a timestamp clamped to a 24 hour day, ie 86400.
00018 
00019   A new instance of eZTime will automaticly use the current locale and current time,
00020   if you however want a different locale use the setLocale() function. The current locale can be
00021   fetched with locale().
00022 
00023   You can also change the time directly with setHour(), setMinute(),
00024   setSecond() and setHMS(). You can also adjust the time relative to it's
00025   current value by using adjustTime(). Use timeStamp() to get the current
00026   timestamp value or hour(), minute() and second() for the respective
00027   values.
00028 
00029   When creating new times you're advised to use the static create()
00030   function which returns a new eZTime object. You can also create a copy
00031   with the duplicate() function.
00032 
00033   Time checking is done with the isGreaterThan() and isEqualTo() functions.
00034 
00035   Text output is done with toString() which can return a long string (default) or
00036   short string representation according to the current locale.
00037 
00038 Example:
00039 \code
00040 
00041 $us_locale = eZLocale::instance( 'us' );
00042 
00043 $time1 = new eZTime();
00044 $time2 = eZTime::create();
00045 $time2->setLocale( $us_locale );
00046 $time2->adjustTime( -8 );
00047 $time3 = $time1->duplicate();
00048 
00049 print( $time1->toString() );
00050 print( $time2->toString( true ) );
00051 print( $time1->isEqualTo( $time3 ) ? 'true' : 'false' ); // Prints 'true'
00052 
00053 \endcode
00054 
00055   \sa eZDate, eZDateTime, eZLocale
00056 */
00057 
00058 class eZTime
00059 {
00060     /*!
00061      Number of seconds in a minute.
00062     */
00063     const SECONDS_A_MINUTE = 60;
00064 
00065     /*!
00066      Number of seconds in an hour.
00067     */
00068     const SECONDS_AN_HOUR = 3600;
00069 
00070     /*!
00071      Number of seconds in a day.
00072     */
00073     const SECONDS_A_DAY = 86400; // 24*60*60
00074 
00075     /*!
00076      Creates a new time object with default locale, if $time is not supplied
00077      the current time is used.
00078     */
00079     function eZTime( $timestamp = false )
00080     {
00081         if ( $timestamp === false )
00082         {
00083             $cur_date = getdate();
00084             $this->setHMS( $cur_date[ 'hours' ],
00085                            $cur_date[ 'minutes' ],
00086                            $cur_date[ 'seconds' ] );
00087         }
00088         else if ( $timestamp > self::SECONDS_A_DAY )
00089             $this->setTimeStamp( $timestamp );
00090         else
00091             $this->setTimeOfDay( $timestamp );
00092 
00093         $this->Locale = eZLocale::instance();
00094     }
00095 
00096     function attributes()
00097     {
00098         return array( 'timestamp',
00099                       'time_of_day',
00100                       'hour',
00101                       'minute',
00102                       'second',
00103                       'is_valid' );
00104     }
00105 
00106     function hasAttribute( $name )
00107     {
00108         return in_array( $name, $this->attributes() );
00109     }
00110 
00111     function attribute( $name )
00112     {
00113         if ( $name == 'timestamp' )
00114             return $this->timeStamp();
00115         else if ( $name == 'time_of_day' )
00116             return $this->timeOfDay();
00117         else if ( $name == 'hour' )
00118             return $this->hour();
00119         else if ( $name == 'minute' )
00120             return $this->minute();
00121         else if ( $name == 'second' )
00122             return $this->second();
00123         else if ( $name == 'is_valid'  )
00124             return $this->isValid();
00125 
00126         eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
00127         return false;
00128     }
00129 
00130     /*!
00131      \return true if the date has valid data.
00132     */
00133     function isValid()
00134     {
00135         return $this->IsValid;
00136     }
00137 
00138     /*!
00139      Sets the locale to $locale which is used in text output.
00140     */
00141     function setLocale( &$locale )
00142     {
00143         $this->Locale =& $locale;
00144     }
00145 
00146     /*!
00147      Returns a reference to the current locale.
00148     */
00149     function &locale()
00150     {
00151         return $this->Locale;
00152     }
00153 
00154     /*!
00155      Sets the hour leaving the other elements untouched.
00156     */
00157     function setHour( $hour )
00158     {
00159         $this->Time = ($hour % 24) * self::SECONDS_AN_HOUR + $this->minute() * self::SECONDS_A_MINUTE + $this->second();
00160         $this->IsValid = $this->Time >= 0;
00161     }
00162 
00163     /*!
00164      Sets the minute leaving the other elements untouched.
00165     */
00166     function setMinute( $min )
00167     {
00168         $this->Time = $this->hour() * self::SECONDS_AN_HOUR + ($min % 60) * self::SECONDS_A_MINUTE + $this->second();
00169         $this->IsValid = $this->Time >= 0;
00170     }
00171 
00172     /*!
00173      Sets the second leaving the other elements untouched.
00174     */
00175     function setSecond( $sec )
00176     {
00177         $this->Time = $this->hour() * self::SECONDS_AN_HOUR + $this->minute() * self::SECONDS_A_MINUTE + ($sec % 60);
00178         $this->IsValid = $this->Time >= 0;
00179     }
00180 
00181     /*!
00182      Sets all hour, minute and second elements.
00183     */
00184     function setHMS( $hour, $min = 0, $sec = 0 )
00185     {
00186         $this->Time = ($hour % 24 ) * self::SECONDS_AN_HOUR + ($min % 60) * self::SECONDS_A_MINUTE + ($sec % 60);
00187         $this->IsValid = $this->Time >= 0;
00188     }
00189 
00190     /*!
00191      Returns the hour element.
00192     */
00193     function hour()
00194     {
00195         return (int)($this->Time / self::SECONDS_AN_HOUR);
00196     }
00197 
00198     /*!
00199      Returns the minute element.
00200     */
00201     function minute()
00202     {
00203         return (int)(($this->Time % self::SECONDS_AN_HOUR) / self::SECONDS_A_MINUTE);
00204     }
00205 
00206     /*!
00207      Returns the second element.
00208     */
00209     function second()
00210     {
00211         return (int)($this->Time % self::SECONDS_A_MINUTE);
00212     }
00213 
00214     /*
00215      Return long timestamp ($this->Time + current day midnight timestamp)
00216     */
00217     function timeStamp()
00218     {
00219         $cur_date = getdate();
00220         $longtimestamp = mktime( $this->hour(),
00221                                  $this->minute(),
00222                                  $this->second(),
00223                                  $cur_date[ 'mon' ],
00224                                  $cur_date[ 'mday' ],
00225                                  $cur_date[ 'year' ] );
00226         return $longtimestamp;
00227     }
00228 
00229     /*
00230      Sets timestamp (time of the das) by clamping off time from the last midnight.
00231     */
00232     function setTimeStamp( $timestamp )
00233     {
00234         $date = getdate( $timestamp );
00235         $this->Time = $date[ 'hours' ] * self::SECONDS_AN_HOUR +
00236                       $date[ 'minutes' ] * self::SECONDS_A_MINUTE +
00237                       $date[ 'seconds' ];
00238     }
00239 
00240     /*!
00241      Returns the timestamp value, this is not the number of seconds since the epoch but
00242      a clamped value to the number of seconds in a day.
00243     */
00244     function timeOfDay()
00245     {
00246         return $this->Time;
00247     }
00248 
00249     /*
00250      Sets time of day (the number of seconds since begining of day)
00251     */
00252     function setTimeOfDay( $timestamp )
00253     {
00254         $this->Time = $timestamp % self::SECONDS_A_DAY;
00255         if ( $this->Time < 0 )
00256             $this->Time += self::SECONDS_A_DAY;
00257         $this->IsValid = $this->Time >= 0;
00258     }
00259 
00260 
00261     /*!
00262      Adjusts the time relative to it's current value. This is useful for adding/subtracting
00263      hours, minutes or seconds to an existing time.
00264     */
00265     function adjustTime( $hour, $minute = 0, $second = 0 )
00266     {
00267         $this->setTimeOfDay( $hour * self::SECONDS_AN_HOUR +
00268                              $minute * self::SECONDS_A_MINUTE +
00269                              $second + $this->Time );
00270     }
00271 
00272     /*!
00273      Creates a new eZTime object with the time values $hour, $min and $sec and returns a reference to it.
00274      Any value can be ommitted or set to -1 to use the current time value.
00275     */
00276     static function create( $hour = -1, $minute = -1, $second = -1 )
00277     {
00278         $cur_date = getdate();
00279 
00280         $time = new eZTime();
00281         $time->setHMS( $hour < 0 ? $cur_date[ 'hours' ] : $hour,
00282                        $minute < 0 ? $cur_date[ 'minutes' ] : $minute,
00283                        $second < 0 ? $cur_date[ 'seconds' ] : $second );
00284         return $time;
00285     }
00286 
00287     /*!
00288      \deprecated This function is deprecated in PHP5, use the PHP5 clone keyword instead
00289      Creates an exact copy of this object and returns it.
00290     */
00291     function duplicate()
00292     {
00293         $copy = clone $this;
00294         return $copy;
00295     }
00296 
00297     /*!
00298      Returns true if this object has a time greater than $time. $time can be specified as
00299      a timestamp value or as an eZTime object. If $equal is true it returns true if
00300      they are equal as well.
00301     */
00302     function isGreaterThan( &$time, $equal = false )
00303     {
00304         $t1 =& $this->Time;
00305         if ( $time instanceof eZTime )
00306             $t2 = $time->timeOfDay();
00307         else
00308             $t2 = ( $time % self::SECONDS_A_DAY );
00309         if ( $t1 > $t2 )
00310             return true;
00311         else if ( $equal and $t1 == $t2 )
00312             return true;
00313         else
00314             return false;
00315     }
00316 
00317     /*!
00318      Returns true if this object is equal to $time. $time can be specified as
00319      a timestamp value or as an eZTime object.
00320     */
00321     function isEqualTo( &$time )
00322     {
00323         $t1 =& $this->Time;
00324         if ( $time instanceof eZTime )
00325             $t2 = $time->timeOfDay();
00326         else
00327             $t2 = ( $time % self::SECONDS_A_DAY );
00328         return $t1 == $t2;
00329     }
00330 
00331     /*!
00332      Creates a string representation of the time using the current locale and returns it.
00333      If $short is true a short representation is used.
00334     */
00335     function toString( $short = false )
00336     {
00337         if ( $short )
00338             $str = $this->Locale->formatShortTime( $this->timeStamp() );
00339         else
00340             $str = $this->Locale->formatTime( $this->timeStamp() );
00341         return $str;
00342     }
00343 
00344     /*!
00345      \static
00346      \deprecated Use eZTime::SECONDS_A_DAY instead
00347      Get number of seconds per day
00348     */
00349     static function secondsPerDay()
00350     {
00351         return self::SECONDS_A_DAY;
00352     }
00353 
00354     /// Locale object, is just a reference to minimize memory usage.
00355     public $Locale;
00356     /// The current time as a clamped timestamp
00357     public $Time;
00358 }
00359 
00360 ?>