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