MediaWiki
REL1_24
|
00001 <?php 00031 class MWTimestamp { 00035 private static $formats = array( 00036 TS_UNIX => 'U', 00037 TS_MW => 'YmdHis', 00038 TS_DB => 'Y-m-d H:i:s', 00039 TS_ISO_8601 => 'Y-m-d\TH:i:s\Z', 00040 TS_ISO_8601_BASIC => 'Ymd\THis\Z', 00041 TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness 00042 TS_RFC2822 => 'D, d M Y H:i:s', 00043 TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500 00044 TS_POSTGRES => 'Y-m-d H:i:s', 00045 ); 00046 00051 public $timestamp; 00052 00061 public function __construct( $timestamp = false ) { 00062 $this->setTimestamp( $timestamp ); 00063 } 00064 00076 public function setTimestamp( $ts = false ) { 00077 $da = array(); 00078 $strtime = ''; 00079 00080 // We want to catch 0, '', null... but not date strings starting with a letter. 00081 if ( !$ts || $ts === "\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ) { 00082 $uts = time(); 00083 $strtime = "@$uts"; 00084 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) { 00085 # TS_DB 00086 } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) { 00087 # TS_EXIF 00088 } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) { 00089 # TS_MW 00090 } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) { 00091 # TS_UNIX 00092 $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php 00093 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) { 00094 # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6 00095 $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3", 00096 str_replace( '+00:00', 'UTC', $ts ) ); 00097 } elseif ( preg_match( 00098 '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z?$/', 00099 $ts, 00100 $da 00101 ) ) { 00102 # TS_ISO_8601 00103 } elseif ( preg_match( 00104 '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z?$/', 00105 $ts, 00106 $da 00107 ) ) { 00108 #TS_ISO_8601_BASIC 00109 } elseif ( preg_match( 00110 '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', 00111 $ts, 00112 $da 00113 ) ) { 00114 # TS_POSTGRES 00115 } elseif ( preg_match( 00116 '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', 00117 $ts, 00118 $da 00119 ) ) { 00120 # TS_POSTGRES 00121 } elseif ( preg_match( 00122 # Day of week 00123 '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . 00124 # dd Mon yyyy 00125 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' . 00126 # hh:mm:ss 00127 '[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d/S', 00128 $ts 00129 ) ) { 00130 # TS_RFC2822, accepting a trailing comment. 00131 # See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171 00132 # The regex is a superset of rfc2822 for readability 00133 $strtime = strtok( $ts, ';' ); 00134 } elseif ( preg_match( '/^[A-Z][a-z]{5,8}, \d\d-[A-Z][a-z]{2}-\d{2} \d\d:\d\d:\d\d/', $ts ) ) { 00135 # TS_RFC850 00136 $strtime = $ts; 00137 } elseif ( preg_match( '/^[A-Z][a-z]{2} [A-Z][a-z]{2} +\d{1,2} \d\d:\d\d:\d\d \d{4}/', $ts ) ) { 00138 # asctime 00139 $strtime = $ts; 00140 } else { 00141 throw new TimestampException( __METHOD__ . ": Invalid timestamp - $ts" ); 00142 } 00143 00144 if ( !$strtime ) { 00145 $da = array_map( 'intval', $da ); 00146 $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00"; 00147 $strtime = call_user_func_array( "sprintf", $da ); 00148 } 00149 00150 try { 00151 $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) ); 00152 } catch ( Exception $e ) { 00153 throw new TimestampException( __METHOD__ . ': Invalid timestamp format.', $e->getCode(), $e ); 00154 } 00155 00156 if ( $final === false ) { 00157 throw new TimestampException( __METHOD__ . ': Invalid timestamp format.' ); 00158 } 00159 $this->timestamp = $final; 00160 } 00161 00174 public function getTimestamp( $style = TS_UNIX ) { 00175 if ( !isset( self::$formats[$style] ) ) { 00176 throw new TimestampException( __METHOD__ . ': Illegal timestamp output type.' ); 00177 } 00178 00179 $output = $this->timestamp->format( self::$formats[$style] ); 00180 00181 if ( ( $style == TS_RFC2822 ) || ( $style == TS_POSTGRES ) ) { 00182 $output .= ' GMT'; 00183 } 00184 00185 return $output; 00186 } 00187 00206 public function getHumanTimestamp( MWTimestamp $relativeTo = null, 00207 User $user = null, Language $lang = null 00208 ) { 00209 if ( $relativeTo === null ) { 00210 $relativeTo = new self(); 00211 } 00212 if ( $user === null ) { 00213 $user = RequestContext::getMain()->getUser(); 00214 } 00215 if ( $lang === null ) { 00216 $lang = RequestContext::getMain()->getLanguage(); 00217 } 00218 00219 // Adjust for the user's timezone. 00220 $offsetThis = $this->offsetForUser( $user ); 00221 $offsetRel = $relativeTo->offsetForUser( $user ); 00222 00223 $ts = ''; 00224 if ( wfRunHooks( 'GetHumanTimestamp', array( &$ts, $this, $relativeTo, $user, $lang ) ) ) { 00225 $ts = $lang->getHumanTimestamp( $this, $relativeTo, $user ); 00226 } 00227 00228 // Reset the timezone on the objects. 00229 $this->timestamp->sub( $offsetThis ); 00230 $relativeTo->timestamp->sub( $offsetRel ); 00231 00232 return $ts; 00233 } 00234 00243 public function offsetForUser( User $user ) { 00244 global $wgLocalTZoffset; 00245 00246 $option = $user->getOption( 'timecorrection' ); 00247 $data = explode( '|', $option, 3 ); 00248 00249 // First handle the case of an actual timezone being specified. 00250 if ( $data[0] == 'ZoneInfo' ) { 00251 try { 00252 $tz = new DateTimeZone( $data[2] ); 00253 } catch ( Exception $e ) { 00254 $tz = false; 00255 } 00256 00257 if ( $tz ) { 00258 $this->timestamp->setTimezone( $tz ); 00259 return new DateInterval( 'P0Y' ); 00260 } else { 00261 $data[0] = 'Offset'; 00262 } 00263 } 00264 00265 $diff = 0; 00266 // If $option is in fact a pipe-separated value, check the 00267 // first value. 00268 if ( $data[0] == 'System' ) { 00269 // First value is System, so use the system offset. 00270 if ( $wgLocalTZoffset !== null ) { 00271 $diff = $wgLocalTZoffset; 00272 } 00273 } elseif ( $data[0] == 'Offset' ) { 00274 // First value is Offset, so use the specified offset 00275 $diff = (int)$data[1]; 00276 } else { 00277 // $option actually isn't a pipe separated value, but instead 00278 // a comma separated value. Isn't MediaWiki fun? 00279 $data = explode( ':', $option ); 00280 if ( count( $data ) >= 2 ) { 00281 // Combination hours and minutes. 00282 $diff = abs( (int)$data[0] ) * 60 + (int)$data[1]; 00283 if ( (int)$data[0] < 0 ) { 00284 $diff *= -1; 00285 } 00286 } else { 00287 // Just hours. 00288 $diff = (int)$data[0] * 60; 00289 } 00290 } 00291 00292 $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' ); 00293 if ( $diff < 1 ) { 00294 $interval->invert = 1; 00295 } 00296 00297 $this->timestamp->add( $interval ); 00298 return $interval; 00299 } 00300 00311 public function getRelativeTimestamp( 00312 MWTimestamp $relativeTo = null, 00313 User $user = null, 00314 Language $lang = null, 00315 array $chosenIntervals = array() 00316 ) { 00317 if ( $relativeTo === null ) { 00318 $relativeTo = new self; 00319 } 00320 if ( $user === null ) { 00321 $user = RequestContext::getMain()->getUser(); 00322 } 00323 if ( $lang === null ) { 00324 $lang = RequestContext::getMain()->getLanguage(); 00325 } 00326 00327 $ts = ''; 00328 $diff = $this->diff( $relativeTo ); 00329 if ( wfRunHooks( 00330 'GetRelativeTimestamp', 00331 array( &$ts, &$diff, $this, $relativeTo, $user, $lang ) 00332 ) ) { 00333 $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s ); 00334 $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) ) 00335 ->inLanguage( $lang )->text(); 00336 } 00337 00338 return $ts; 00339 } 00340 00346 public function __toString() { 00347 return $this->getTimestamp(); 00348 } 00349 00358 public function diff( MWTimestamp $relativeTo ) { 00359 return $this->timestamp->diff( $relativeTo->timestamp ); 00360 } 00361 00369 public function setTimezone( $timezone ) { 00370 try { 00371 $this->timestamp->setTimezone( new DateTimeZone( $timezone ) ); 00372 } catch ( Exception $e ) { 00373 throw new TimestampException( __METHOD__ . ': Invalid timezone.', $e->getCode(), $e ); 00374 } 00375 } 00376 00383 public function getTimezone() { 00384 return $this->timestamp->getTimezone(); 00385 } 00386 00394 public function format( $format ) { 00395 return $this->timestamp->format( $format ); 00396 } 00397 00405 public static function getLocalInstance( $ts = false ) { 00406 global $wgLocaltimezone; 00407 $timestamp = new self( $ts ); 00408 $timestamp->setTimezone( $wgLocaltimezone ); 00409 return $timestamp; 00410 } 00411 00419 public static function getInstance( $ts = false ) { 00420 return new self( $ts ); 00421 } 00422 }