MediaWiki  REL1_22
Timestamp.php
Go to the documentation of this file.
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         if ( !$ts || $ts === "\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ) { // We want to catch 0, '', null... but not date strings starting with a letter.
00081             $uts = time();
00082             $strtime = "@$uts";
00083         } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
00084             # TS_DB
00085         } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
00086             # TS_EXIF
00087         } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) {
00088             # TS_MW
00089         } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
00090             # TS_UNIX
00091             $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php
00092         } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
00093             # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
00094             $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
00095                     str_replace( '+00:00', 'UTC', $ts ) );
00096         } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
00097             # TS_ISO_8601
00098         } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
00099             #TS_ISO_8601_BASIC
00100         } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) {
00101             # TS_POSTGRES
00102         } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', $ts, $da ) ) {
00103             # TS_POSTGRES
00104         } elseif ( preg_match( '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . # Day of week
00105                                 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' .  # dd Mon yyyy
00106                                 '[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d/S', $ts ) ) { # hh:mm:ss
00107             # TS_RFC2822, accepting a trailing comment. See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171
00108             # The regex is a superset of rfc2822 for readability
00109             $strtime = strtok( $ts, ';' );
00110         } 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 ) ) {
00111             # TS_RFC850
00112             $strtime = $ts;
00113         } 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 ) ) {
00114             # asctime
00115             $strtime = $ts;
00116         } else {
00117             throw new TimestampException( __METHOD__ . ": Invalid timestamp - $ts" );
00118         }
00119 
00120         if ( !$strtime ) {
00121             $da = array_map( 'intval', $da );
00122             $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00";
00123             $strtime = call_user_func_array( "sprintf", $da );
00124         }
00125 
00126         try {
00127             $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
00128         } catch ( Exception $e ) {
00129             throw new TimestampException( __METHOD__ . ': Invalid timestamp format.', $e->getCode(), $e );
00130         }
00131 
00132         if ( $final === false ) {
00133             throw new TimestampException( __METHOD__ . ': Invalid timestamp format.' );
00134         }
00135         $this->timestamp = $final;
00136     }
00137 
00150     public function getTimestamp( $style = TS_UNIX ) {
00151         if ( !isset( self::$formats[$style] ) ) {
00152             throw new TimestampException( __METHOD__ . ': Illegal timestamp output type.' );
00153         }
00154 
00155         $output = $this->timestamp->format( self::$formats[$style] );
00156 
00157         if ( ( $style == TS_RFC2822 ) || ( $style == TS_POSTGRES ) ) {
00158             $output .= ' GMT';
00159         }
00160 
00161         return $output;
00162     }
00163 
00179     public function getHumanTimestamp( MWTimestamp $relativeTo = null, User $user = null, Language $lang = null ) {
00180         if ( $relativeTo === null ) {
00181             $relativeTo = new self();
00182         }
00183         if ( $user === null ) {
00184             $user = RequestContext::getMain()->getUser();
00185         }
00186         if ( $lang === null ) {
00187             $lang = RequestContext::getMain()->getLanguage();
00188         }
00189 
00190         // Adjust for the user's timezone.
00191         $offsetThis = $this->offsetForUser( $user );
00192         $offsetRel = $relativeTo->offsetForUser( $user );
00193 
00194         $ts = '';
00195         if ( wfRunHooks( 'GetHumanTimestamp', array( &$ts, $this, $relativeTo, $user, $lang ) ) ) {
00196             $ts = $lang->getHumanTimestamp( $this, $relativeTo, $user );
00197         }
00198 
00199         // Reset the timezone on the objects.
00200         $this->timestamp->sub( $offsetThis );
00201         $relativeTo->timestamp->sub( $offsetRel );
00202 
00203         return $ts;
00204     }
00205 
00215     public function offsetForUser( User $user ) {
00216         global $wgLocalTZoffset;
00217 
00218         $option = $user->getOption( 'timecorrection' );
00219         $data = explode( '|', $option, 3 );
00220 
00221         // First handle the case of an actual timezone being specified.
00222         if ( $data[0] == 'ZoneInfo' ) {
00223             try {
00224                 $tz = new DateTimeZone( $data[2] );
00225             } catch ( Exception $e ) {
00226                 $tz = false;
00227             }
00228 
00229             if ( $tz ) {
00230                 $this->timestamp->setTimezone( $tz );
00231                 return new DateInterval( 'P0Y' );
00232             } else {
00233                 $data[0] = 'Offset';
00234             }
00235         }
00236 
00237         $diff = 0;
00238         // If $option is in fact a pipe-separated value, check the
00239         // first value.
00240         if ( $data[0] == 'System' ) {
00241             // First value is System, so use the system offset.
00242             if ( isset( $wgLocalTZoffset ) ) {
00243                 $diff = $wgLocalTZoffset;
00244             }
00245         } elseif ( $data[0] == 'Offset' ) {
00246             // First value is Offset, so use the specified offset
00247             $diff = (int)$data[1];
00248         } else {
00249             // $option actually isn't a pipe separated value, but instead
00250             // a comma separated value. Isn't MediaWiki fun?
00251             $data = explode( ':', $option );
00252             if ( count( $data ) >= 2 ) {
00253                 // Combination hours and minutes.
00254                 $diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
00255                 if ( (int)$data[0] < 0 ) {
00256                     $diff *= -1;
00257                 }
00258             } else {
00259                 // Just hours.
00260                 $diff = (int)$data[0] * 60;
00261             }
00262         }
00263 
00264         $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' );
00265         if ( $diff < 1 ) {
00266             $interval->invert = 1;
00267         }
00268 
00269         $this->timestamp->add( $interval );
00270         return $interval;
00271     }
00272 
00283     public function getRelativeTimestamp(
00284         MWTimestamp $relativeTo = null,
00285         User $user = null,
00286         Language $lang = null,
00287         array $chosenIntervals = array()
00288     ) {
00289         if ( $relativeTo === null ) {
00290             $relativeTo = new self;
00291         }
00292         if ( $user === null ) {
00293             $user = RequestContext::getMain()->getUser();
00294         }
00295         if ( $lang === null ) {
00296             $lang = RequestContext::getMain()->getLanguage();
00297         }
00298 
00299         $ts = '';
00300         $diff = $this->diff( $relativeTo );
00301         if ( wfRunHooks( 'GetRelativeTimestamp', array( &$ts, &$diff, $this, $relativeTo, $user, $lang ) ) ) {
00302             $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
00303             $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
00304                 ->inLanguage( $lang )
00305                 ->text();
00306         }
00307 
00308         return $ts;
00309     }
00310 
00316     public function __toString() {
00317         return $this->getTimestamp();
00318     }
00319 
00327     public function diff( MWTimestamp $relativeTo ) {
00328         return $this->timestamp->diff( $relativeTo->timestamp );
00329     }
00330 
00338     public function setTimezone( $timezone ) {
00339         try {
00340             $this->timestamp->setTimezone( new DateTimeZone( $timezone ) );
00341         } catch ( Exception $e ) {
00342             throw new TimestampException( __METHOD__ . ': Invalid timezone.', $e->getCode(), $e );
00343         }
00344     }
00345 
00352     public function getTimezone() {
00353         return $this->timestamp->getTimezone();
00354     }
00355 
00363     public function format( $format ) {
00364         return $this->timestamp->format( $format );
00365     }
00366 
00374     public static function getLocalInstance( $ts = false ) {
00375         global $wgLocaltimezone;
00376         $timestamp = new self( $ts );
00377         $timestamp->setTimezone( $wgLocaltimezone );
00378         return $timestamp;
00379     }
00380 
00388     public static function getInstance( $ts = false ) {
00389         return new self( $ts );
00390     }
00391 }
00392 
00396 class TimestampException extends MWException {}