[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezlocale/classes/ -> eztime.php (source)

   1  <?php
   2  //
   3  // Definition of eZTime class
   4  //
   5  // Created on: <01-Mar-2002 13:48:40 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZTime eztime.php
  31    \ingroup eZLocale
  32    \brief Locale aware time handler
  33  
  34    eZTime handles 24 hour time values in hours, minutes and seconds.
  35    The time stored as a timestamp clamped to a 24 hour day, ie 86400.
  36  
  37    A new instance of eZTime will automaticly use the current locale and current time,
  38    if you however want a different locale use the setLocale() function. The current locale can be
  39    fetched with locale().
  40  
  41    You can also change the time directly with setHour(), setMinute(),
  42    setSecond() and setHMS(). You can also adjust the time relative to it's
  43    current value by using adjustTime(). Use timeStamp() to get the current
  44    timestamp value or hour(), minute() and second() for the respective
  45    values.
  46  
  47    When creating new times you're advised to use the static create()
  48    function which returns a new eZTime object. You can also create a copy
  49    with the duplicate() function.
  50  
  51    Time checking is done with the isGreaterThan() and isEqualTo() functions.
  52  
  53    Text output is done with toString() which can return a long string (default) or
  54    short string representation according to the current locale.
  55  
  56  Example:
  57  \code
  58  include_once( 'lib/ezlocale/classes/ezlocale.php' );
  59  include_once( 'lib/ezlocale/classes/eztime.php' );
  60  
  61  $us_locale =& eZLocale::instance( 'us' );
  62  
  63  $time1 = new eZTime();
  64  $time2 = eZTime::create();
  65  $time2->setLocale( $us_locale );
  66  $time2->adjustTime( -8 );
  67  $time3 = $time1->duplicate();
  68  
  69  print( $time1->toString() );
  70  print( $time2->toString( true ) );
  71  print( $time1->isEqualTo( $time3 ) ? 'true' : 'false' ); // Prints 'true'
  72  
  73  \endcode
  74  
  75    \sa eZDate, eZDateTime, eZLocale
  76  */
  77  
  78  include_once ( 'lib/ezlocale/classes/ezlocale.php' );
  79  
  80  /*!
  81   Number of seconds in a minute.
  82  */
  83  define( 'EZTIME_SECONDS_A_MINUTE', 60 );
  84  /*!
  85   Number of seconds in an hour.
  86  */
  87  define( 'EZTIME_SECONDS_AN_HOUR', 3600 );
  88  /*!
  89   Number of seconds in a day.
  90  */
  91  define( 'EZTIME_SECONDS_A_DAY', 86400 ); // 24*60*60
  92  
  93  class eZTime
  94  {
  95      /*!
  96       Creates a new time object with default locale, if $time is not supplied
  97       the current time is used.
  98      */
  99      function eZTime( $timestamp = false )
 100      {
 101          if ( $timestamp === false )
 102          {
 103              $cur_date = getdate();
 104              $this->setHMS( $cur_date[ 'hours' ],
 105                             $cur_date[ 'minutes' ],
 106                             $cur_date[ 'seconds' ] );
 107          }
 108          else if ( $timestamp > EZTIME_SECONDS_A_DAY )
 109              $this->setTimeStamp( $timestamp );
 110          else
 111              $this->setTimeOfDay( $timestamp );
 112  
 113          $this->Locale =& eZLocale::instance();
 114      }
 115  
 116      function attributes()
 117      {
 118          return array( 'timestamp',
 119                        'time_of_day',
 120                        'hour',
 121                        'minute',
 122                        'is_valid' );
 123      }
 124  
 125      function hasAttribute( $name )
 126      {
 127          return in_array( $name, $this->attributes() );
 128      }
 129  
 130      function &attribute( $name )
 131      {
 132          if ( $name == 'timestamp' )
 133              $retValue = $this->timeStamp();
 134          else if ( $name == 'time_of_day' )
 135              $retValue = $this->timeOfDay();
 136          else if ( $name == 'hour' )
 137              $retValue = $this->hour();
 138          else if ( $name == 'minute' )
 139              $retValue = $this->minute();
 140          else if ( $name == 'is_valid'  )
 141              $retValue = $this->isValid();
 142          else
 143          {
 144              eZDebug::writeError( "Attribute '$name' does not exist", 'eZTime::attribute' );
 145              $retValue = false;
 146          }
 147          return $retValue;
 148      }
 149  
 150      /*!
 151       \return true if the date has valid data.
 152      */
 153      function isValid()
 154      {
 155          return $this->IsValid;
 156      }
 157  
 158      /*!
 159       Sets the locale to $locale which is used in text output.
 160      */
 161      function setLocale( &$locale )
 162      {
 163          $this->Locale =& $locale;
 164      }
 165  
 166      /*!
 167       Returns a reference to the current locale.
 168      */
 169      function &locale()
 170      {
 171          return $this->Locale;
 172      }
 173  
 174      /*!
 175       Sets the hour leaving the other elements untouched.
 176      */
 177      function setHour( $hour )
 178      {
 179          $this->Time = ($hour % 24) * EZTIME_SECONDS_AN_HOUR + $this->minute() * EZTIME_SECONDS_A_MINUTE + $this->second();
 180          $this->IsValid = $this->Time >= 0;
 181      }
 182  
 183      /*!
 184       Sets the minute leaving the other elements untouched.
 185      */
 186      function setMinute( $min )
 187      {
 188          $this->Time = $this->hour() * EZTIME_SECONDS_AN_HOUR + ($min % 60) * EZTIME_SECONDS_A_MINUTE + $this->second();
 189          $this->IsValid = $this->Time >= 0;
 190      }
 191  
 192      /*!
 193       Sets the second leaving the other elements untouched.
 194      */
 195      function setSecond( $sec )
 196      {
 197          $this->Time = $this->hour() * EZTIME_SECONDS_AN_HOUR + $this->minute() * EZTIME_SECONDS_A_MINUTE + ($sec % 60);
 198          $this->IsValid = $this->Time >= 0;
 199      }
 200  
 201      /*!
 202       Sets all hour, minute and second elements.
 203      */
 204      function setHMS( $hour, $min = 0, $sec = 0 )
 205      {
 206          $this->Time = ($hour % 24 ) * EZTIME_SECONDS_AN_HOUR + ($min % 60) * EZTIME_SECONDS_A_MINUTE + ($sec % 60);
 207          $this->IsValid = $this->Time >= 0;
 208      }
 209  
 210      /*!
 211       Returns the hour element.
 212      */
 213      function hour()
 214      {
 215          return (int)($this->Time / EZTIME_SECONDS_AN_HOUR);
 216      }
 217  
 218      /*!
 219       Returns the minute element.
 220      */
 221      function minute()
 222      {
 223          return (int)(($this->Time % EZTIME_SECONDS_AN_HOUR) / EZTIME_SECONDS_A_MINUTE);
 224      }
 225  
 226      /*!
 227       Returns the second element.
 228      */
 229      function second()
 230      {
 231          return (int)($this->Time % EZTIME_SECONDS_A_MINUTE);
 232      }
 233  
 234      /*
 235       Return long timestamp ($this->Time + current day midnight timestamp)
 236      */
 237      function timeStamp()
 238      {
 239          $cur_date = getdate();
 240          $longtimestamp = mktime( $this->hour(),
 241                                   $this->minute(),
 242                                   $this->second(),
 243                                   $cur_date[ 'mon' ],
 244                                   $cur_date[ 'mday' ],
 245                                   $cur_date[ 'year' ] );
 246          return $longtimestamp;
 247      }
 248  
 249      /*
 250       Sets timestamp (time of the das) by clamping off time from the last midnight.
 251      */
 252      function setTimeStamp( $timestamp )
 253      {
 254          $date = getdate( $timestamp );
 255          $this->Time = $date[ 'hours' ] * EZTIME_SECONDS_AN_HOUR +
 256                        $date[ 'minutes' ] * EZTIME_SECONDS_A_MINUTE +
 257                        $date[ 'seconds' ];
 258      }
 259  
 260      /*!
 261       Returns the timestamp value, this is not the number of seconds since the epoch but
 262       a clamped value to the number of seconds in a day.
 263      */
 264      function timeOfDay()
 265      {
 266          return $this->Time;
 267      }
 268  
 269      /*
 270       Sets time of day (the number of seconds since begining of day)
 271      */
 272      function setTimeOfDay( $timestamp )
 273      {
 274          $this->Time = $timestamp % EZTIME_SECONDS_A_DAY;
 275          if ( $this->Time < 0 )
 276              $this->Time += EZTIME_SECONDS_A_DAY;
 277          $this->IsValid = $this->Time >= 0;
 278      }
 279  
 280  
 281      /*!
 282       Adjusts the time relative to it's current value. This is useful for adding/subtracting
 283       hours, minutes or seconds to an existing time.
 284      */
 285      function adjustTime( $hour, $minute = 0, $second = 0 )
 286      {
 287          $this->setTimeOfDay( $hour * EZTIME_SECONDS_AN_HOUR +
 288                               $minute * EZTIME_SECONDS_A_MINUTE +
 289                               $second + $this->Time );
 290      }
 291  
 292      /*!
 293       Creates a new eZTime object with the time values $hour, $min and $sec and returns a reference to it.
 294       Any value can be ommitted or set to -1 to use the current time value.
 295      */
 296      function create( $hour = -1, $minute = -1, $second = -1 )
 297      {
 298          $cur_date = getdate();
 299  
 300          $time = new eZTime();
 301          $time->setHMS( $hour < 0 ? $cur_date[ 'hours' ] : $hour,
 302                         $minute < 0 ? $cur_date[ 'minutes' ] : $minute,
 303                         $second < 0 ? $cur_date[ 'seconds' ] : $second );
 304          return $time;
 305      }
 306  
 307      /*!
 308       Creates an exact copy of this object and returns a reference to it.
 309      */
 310      function &duplicate()
 311      {
 312          $t = new eZTime( $this->Time );
 313          $t->setLocale( $this->Locale );
 314          return $t;
 315      }
 316  
 317      /*!
 318       Returns true if this object has a time greater than $time. $time can be specified as
 319       a timestamp value or as an eZTime object. If $equal is true it returns true if
 320       they are equal as well.
 321      */
 322      function isGreaterThan( &$time, $equal = false )
 323      {
 324          $t1 =& $this->Time;
 325          if ( get_class( $time ) == 'eztime' )
 326              $t2 = $time->timeOfDay();
 327          else
 328              $t2 = ( $time % EZTIME_SECONDS_A_DAY );
 329          if ( $t1 > $t2 )
 330              return true;
 331          else if ( $equal and $t1 == $t2 )
 332              return true;
 333          else
 334              return false;
 335      }
 336  
 337      /*!
 338       Returns true if this object is equal to $time. $time can be specified as
 339       a timestamp value or as an eZTime object.
 340      */
 341      function isEqualTo( &$time )
 342      {
 343          $t1 =& $this->Time;
 344          if ( get_class( $time ) == 'eztime' )
 345              $t2 = $time->timeOfDay();
 346          else
 347              $t2 = ( $time % EZTIME_SECONDS_A_DAY );
 348          return $t1 == $t2;
 349      }
 350  
 351      /*!
 352       Creates a string representation of the time using the current locale and returns it.
 353       If $short is true a short representation is used.
 354      */
 355      function toString( $short = false )
 356      {
 357          if ( $short )
 358              $str = $this->Locale->formatShortTime( $this->timeStamp() );
 359          else
 360              $str = $this->Locale->formatTime( $this->timeStamp() );
 361          return $str;
 362      }
 363  
 364      /*!
 365       \static
 366       Get number of seconds per day
 367      */
 368      function secondsPerDay()
 369      {
 370          return EZTIME_SECONDS_A_DAY;
 371      }
 372  
 373      /// Locale object, is just a reference to minimize memory usage.
 374      var $Locale;
 375      /// The current time as a clamped timestamp
 376      var $Time;
 377  }
 378  
 379  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7