00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 include_once( 'lib/ezlocale/classes/ezlocale.php' );
00081
00082
00083
00084
00085 define( 'EZTIME_SECONDS_A_MINUTE', 60 );
00086
00087
00088
00089 define( 'EZTIME_SECONDS_AN_HOUR', 3600 );
00090
00091
00092
00093 define( 'EZTIME_SECONDS_A_DAY', 86400 );
00094
00095 class eZTime
00096 {
00097
00098
00099
00100
00101 function eZTime( $timestamp = false )
00102 {
00103 if ( $timestamp === false )
00104 {
00105 $cur_date = getdate();
00106 $this->setHMS( $cur_date[ 'hours' ],
00107 $cur_date[ 'minutes' ],
00108 $cur_date[ 'seconds' ] );
00109 }
00110 else if ( $timestamp > EZTIME_SECONDS_A_DAY )
00111 $this->setTimeStamp( $timestamp );
00112 else
00113 $this->setTimeOfDay( $timestamp );
00114
00115 $this->Locale =& eZLocale::instance();
00116 }
00117
00118 function attributes()
00119 {
00120 return array( 'timestamp',
00121 'time_of_day',
00122 'hour',
00123 'minute',
00124 'is_valid' );
00125 }
00126
00127 function hasAttribute( $name )
00128 {
00129 return in_array( $name, $this->attributes() );
00130 }
00131
00132 function &attribute( $name )
00133 {
00134 if ( $name == 'timestamp' )
00135 $retValue = $this->timeStamp();
00136 else if ( $name == 'time_of_day' )
00137 $retValue = $this->timeOfDay();
00138 else if ( $name == 'hour' )
00139 $retValue = $this->hour();
00140 else if ( $name == 'minute' )
00141 $retValue = $this->minute();
00142 else if ( $name == 'is_valid' )
00143 $retValue = $this->isValid();
00144 else
00145 {
00146 eZDebug::writeError( "Attribute '$name' does not exist", 'eZTime::attribute' );
00147 $retValue = false;
00148 }
00149 return $retValue;
00150 }
00151
00152
00153
00154
00155 function isValid()
00156 {
00157 return $this->IsValid;
00158 }
00159
00160
00161
00162
00163 function setLocale( &$locale )
00164 {
00165 $this->Locale =& $locale;
00166 }
00167
00168
00169
00170
00171 function &locale()
00172 {
00173 return $this->Locale;
00174 }
00175
00176
00177
00178
00179 function setHour( $hour )
00180 {
00181 $this->Time = ($hour % 24) * EZTIME_SECONDS_AN_HOUR + $this->minute() * EZTIME_SECONDS_A_MINUTE + $this->second();
00182 $this->IsValid = $this->Time >= 0;
00183 }
00184
00185
00186
00187
00188 function setMinute( $min )
00189 {
00190 $this->Time = $this->hour() * EZTIME_SECONDS_AN_HOUR + ($min % 60) * EZTIME_SECONDS_A_MINUTE + $this->second();
00191 $this->IsValid = $this->Time >= 0;
00192 }
00193
00194
00195
00196
00197 function setSecond( $sec )
00198 {
00199 $this->Time = $this->hour() * EZTIME_SECONDS_AN_HOUR + $this->minute() * EZTIME_SECONDS_A_MINUTE + ($sec % 60);
00200 $this->IsValid = $this->Time >= 0;
00201 }
00202
00203
00204
00205
00206 function setHMS( $hour, $min = 0, $sec = 0 )
00207 {
00208 $this->Time = ($hour % 24 ) * EZTIME_SECONDS_AN_HOUR + ($min % 60) * EZTIME_SECONDS_A_MINUTE + ($sec % 60);
00209 $this->IsValid = $this->Time >= 0;
00210 }
00211
00212
00213
00214
00215 function hour()
00216 {
00217 return (int)($this->Time / EZTIME_SECONDS_AN_HOUR);
00218 }
00219
00220
00221
00222
00223 function minute()
00224 {
00225 return (int)(($this->Time % EZTIME_SECONDS_AN_HOUR) / EZTIME_SECONDS_A_MINUTE);
00226 }
00227
00228
00229
00230
00231 function second()
00232 {
00233 return (int)($this->Time % EZTIME_SECONDS_A_MINUTE);
00234 }
00235
00236
00237
00238
00239 function timeStamp()
00240 {
00241 $cur_date = getdate();
00242 $longtimestamp = mktime( $this->hour(),
00243 $this->minute(),
00244 $this->second(),
00245 $cur_date[ 'mon' ],
00246 $cur_date[ 'mday' ],
00247 $cur_date[ 'year' ] );
00248 return $longtimestamp;
00249 }
00250
00251
00252
00253
00254 function setTimeStamp( $timestamp )
00255 {
00256 $date = getdate( $timestamp );
00257 $this->Time = $date[ 'hours' ] * EZTIME_SECONDS_AN_HOUR +
00258 $date[ 'minutes' ] * EZTIME_SECONDS_A_MINUTE +
00259 $date[ 'seconds' ];
00260 }
00261
00262
00263
00264
00265
00266 function timeOfDay()
00267 {
00268 return $this->Time;
00269 }
00270
00271
00272
00273
00274 function setTimeOfDay( $timestamp )
00275 {
00276 $this->Time = $timestamp % EZTIME_SECONDS_A_DAY;
00277 if ( $this->Time < 0 )
00278 $this->Time += EZTIME_SECONDS_A_DAY;
00279 $this->IsValid = $this->Time >= 0;
00280 }
00281
00282
00283
00284
00285
00286
00287 function adjustTime( $hour, $minute = 0, $second = 0 )
00288 {
00289 $this->setTimeOfDay( $hour * EZTIME_SECONDS_AN_HOUR +
00290 $minute * EZTIME_SECONDS_A_MINUTE +
00291 $second + $this->Time );
00292 }
00293
00294
00295
00296
00297
00298 function create( $hour = -1, $minute = -1, $second = -1 )
00299 {
00300 $cur_date = getdate();
00301
00302 $time = new eZTime();
00303 $time->setHMS( $hour < 0 ? $cur_date[ 'hours' ] : $hour,
00304 $minute < 0 ? $cur_date[ 'minutes' ] : $minute,
00305 $second < 0 ? $cur_date[ 'seconds' ] : $second );
00306 return $time;
00307 }
00308
00309
00310
00311
00312 function &duplicate()
00313 {
00314 $t = new eZTime( $this->Time );
00315 $t->setLocale( $this->Locale );
00316 return $t;
00317 }
00318
00319
00320
00321
00322
00323
00324 function isGreaterThan( &$time, $equal = false )
00325 {
00326 $t1 =& $this->Time;
00327 if ( get_class( $time ) == 'eztime' )
00328 $t2 = $time->timeOfDay();
00329 else
00330 $t2 = ( $time % EZTIME_SECONDS_A_DAY );
00331 if ( $t1 > $t2 )
00332 return true;
00333 else if ( $equal and $t1 == $t2 )
00334 return true;
00335 else
00336 return false;
00337 }
00338
00339
00340
00341
00342
00343 function isEqualTo( &$time )
00344 {
00345 $t1 =& $this->Time;
00346 if ( get_class( $time ) == 'eztime' )
00347 $t2 = $time->timeOfDay();
00348 else
00349 $t2 = ( $time % EZTIME_SECONDS_A_DAY );
00350 return $t1 == $t2;
00351 }
00352
00353
00354
00355
00356
00357 function toString( $short = false )
00358 {
00359 if ( $short )
00360 $str = $this->Locale->formatShortTime( $this->timeStamp() );
00361 else
00362 $str = $this->Locale->formatTime( $this->timeStamp() );
00363 return $str;
00364 }
00365
00366
00367
00368
00369
00370 function secondsPerDay()
00371 {
00372 return EZTIME_SECONDS_A_DAY;
00373 }
00374
00375
00376 var $Locale;
00377
00378 var $Time;
00379 }
00380
00381 ?>