MediaWiki
master
|
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 TS_DB2 => 'Y-m-d H:i:s', 00046 ); 00047 00052 private static $units = array( 00053 "milliseconds" => 1, 00054 "seconds" => 1000, // 1000 milliseconds per second 00055 "minutes" => 60, // 60 seconds per minute 00056 "hours" => 60, // 60 minutes per hour 00057 "days" => 24 // 24 hours per day 00058 ); 00059 00066 private $timestamp; 00067 00076 public function __construct( $timestamp = false ) { 00077 $this->setTimestamp( $timestamp ); 00078 } 00079 00091 public function setTimestamp( $ts = false ) { 00092 $da = array(); 00093 $strtime = ''; 00094 00095 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. 00096 $uts = time(); 00097 $strtime = "@$uts"; 00098 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) { 00099 # TS_DB 00100 } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) { 00101 # TS_EXIF 00102 } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) { 00103 # TS_MW 00104 } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) { 00105 # TS_UNIX 00106 $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php 00107 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) { 00108 # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6 00109 $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3", 00110 str_replace( '+00:00', 'UTC', $ts ) ); 00111 } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) { 00112 # TS_ISO_8601 00113 } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) { 00114 #TS_ISO_8601_BASIC 00115 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) { 00116 # TS_POSTGRES 00117 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', $ts, $da ) ) { 00118 # TS_POSTGRES 00119 } elseif (preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.\d\d\d$/', $ts, $da ) ) { 00120 # TS_DB2 00121 } elseif ( preg_match( '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . # Day of week 00122 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' . # dd Mon yyyy 00123 '[ \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 00124 # TS_RFC2822, accepting a trailing comment. See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171 00125 # The regex is a superset of rfc2822 for readability 00126 $strtime = strtok( $ts, ';' ); 00127 } 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 ) ) { 00128 # TS_RFC850 00129 $strtime = $ts; 00130 } 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 ) ) { 00131 # asctime 00132 $strtime = $ts; 00133 } else { 00134 throw new TimestampException( __METHOD__ . " : Invalid timestamp - $ts" ); 00135 } 00136 00137 if( !$strtime ) { 00138 $da = array_map( 'intval', $da ); 00139 $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00"; 00140 $strtime = call_user_func_array( "sprintf", $da ); 00141 } 00142 00143 try { 00144 $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) ); 00145 } catch(Exception $e) { 00146 throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' ); 00147 } 00148 00149 if( $final === false ) { 00150 throw new TimestampException( __METHOD__ . ' Invalid timestamp format.' ); 00151 } 00152 $this->timestamp = $final; 00153 } 00154 00167 public function getTimestamp( $style = TS_UNIX ) { 00168 if( !isset( self::$formats[$style] ) ) { 00169 throw new TimestampException( __METHOD__ . ' : Illegal timestamp output type.' ); 00170 } 00171 00172 if( is_object( $this->timestamp ) ) { 00173 // DateTime object was used, call DateTime::format. 00174 $output = $this->timestamp->format( self::$formats[$style] ); 00175 } elseif( TS_UNIX == $style ) { 00176 // Unix timestamp was used and is wanted, just return it. 00177 $output = $this->timestamp; 00178 } else { 00179 // Unix timestamp was used, use gmdate(). 00180 $output = gmdate( self::$formats[$style], $this->timestamp ); 00181 } 00182 00183 if ( ( $style == TS_RFC2822 ) || ( $style == TS_POSTGRES ) ) { 00184 $output .= ' GMT'; 00185 } 00186 00187 return $output; 00188 } 00189 00201 public function getHumanTimestamp() { 00202 $then = $this->getTimestamp( TS_UNIX ); 00203 $now = time(); 00204 $timeago = ($now - $then) * 1000; 00205 $message = false; 00206 00207 foreach( self::$units as $unit => $factor ) { 00208 $next = $timeago / $factor; 00209 if( $next < 1 ) { 00210 break; 00211 } else { 00212 $timeago = $next; 00213 $message = array( $unit, floor( $timeago ) ); 00214 } 00215 } 00216 00217 if( $message ) { 00218 $initial = call_user_func_array( 'wfMessage', $message ); 00219 return wfMessage( 'ago', $initial ); 00220 } else { 00221 return wfMessage( 'just-now' ); 00222 } 00223 } 00224 00230 public function __toString() { 00231 return $this->getTimestamp(); 00232 } 00233 } 00234 00238 class TimestampException extends MWException {}