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