Merge "Adjust margin of wpSummary elements per move of summary box."
[lhc/web/wiklou.git] / includes / Timestamp.php
1 <?php
2
3 /**
4 * Library for creating and parsing MW-style timestamps. Based on the JS
5 * library that does the same thing.
6 *
7 * @author Tyler Romeo, 2012
8 * @since 1.20
9 */
10 class MWTimestamp {
11 /**
12 * Standard gmdate() formats for the different timestamp types.
13 */
14 private static $formats = array(
15 TS_UNIX => 'U',
16 TS_MW => 'YmdHis',
17 TS_DB => 'Y-m-d H:i:s',
18 TS_ISO_8601 => 'Y-m-d\TH:i:s\Z',
19 TS_ISO_8601_BASIC => 'Ymd\THis\Z',
20 TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness
21 TS_RFC2822 => 'D, d M Y H:i:s',
22 TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500
23 TS_POSTGRES => 'Y-m-d H:i:s',
24 TS_DB2 => 'Y-m-d H:i:s',
25 );
26
27 /**
28 * Different units for human readable timestamps.
29 * @see MWTimestamp::getHumanTimestamp
30 */
31 private static $units = array(
32 "milliseconds" => 1,
33 "seconds" => 1000, // 1000 milliseconds per second
34 "minutes" => 60, // 60 seconds per minute
35 "hours" => 60, // 60 minutes per hour
36 "days" => 24 // 24 hours per day
37 );
38
39 /**
40 * The actual timestamp being wrapped. Either a DateTime
41 * object or a string with a Unix timestamp depending on
42 * PHP.
43 */
44 private $timestamp;
45
46 /**
47 * Make a new timestamp and set it to the specified time,
48 * or the current time if unspecified.
49 *
50 * @param $timestamp Timestamp to set, or false for current time
51 */
52 public function __construct( $timestamp = false ) {
53 $this->setTimestamp( $timestamp );
54 }
55
56 /**
57 * Set the timestamp to the specified time, or the current time if unspecified.
58 *
59 * Parse the given timestamp into either a DateTime object or a Unix teimstamp,
60 * and then store it.
61 *
62 * @param $ts Timestamp to store, or false for now
63 */
64 public function setTimestamp( $ts = false ) {
65 $uts = 0;
66 $da = array();
67 $strtime = '';
68
69 if ( !$ts ) { // We want to catch 0, '', null... but not date strings starting with a letter.
70 $uts = time();
71 $strtime = "@$uts";
72 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
73 # TS_DB
74 } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
75 # TS_EXIF
76 } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) {
77 # TS_MW
78 } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
79 # TS_UNIX
80 $uts = $ts;
81 $strtime = "@$ts"; // http://php.net/manual/en/datetime.formats.compound.php
82 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
83 # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
84 $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
85 str_replace( '+00:00', 'UTC', $ts ) );
86 } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
87 # TS_ISO_8601
88 } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
89 #TS_ISO_8601_BASIC
90 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) {
91 # TS_POSTGRES
92 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', $ts, $da ) ) {
93 # TS_POSTGRES
94 } elseif (preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.\d\d\d$/', $ts, $da ) ) {
95 # TS_DB2
96 } elseif ( preg_match( '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . # Day of week
97 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' . # dd Mon yyyy
98 '[ \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
99 # TS_RFC2822, accepting a trailing comment. See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171
100 # The regex is a superset of rfc2822 for readability
101 $strtime = strtok( $ts, ';' );
102 } 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 ) ) {
103 # TS_RFC850
104 $strtime = $ts;
105 } 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 ) ) {
106 # asctime
107 $strtime = $ts;
108 } else {
109 throw new TimestampException( __METHOD__ . " : Invalid timestamp - $ts" );
110 }
111
112 if( !$strtime ) {
113 $da = array_map( 'intval', $da );
114 $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00";
115 $strtime = call_user_func_array( "sprintf", $da );
116 }
117
118 if( function_exists( "date_create" ) ) {
119 try {
120 $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
121 } catch(Exception $e) {
122 throw new TimestampException( __METHOD__ . 'Invalid timestamp format.' );
123 }
124 } else {
125 $final = strtotime( $strtime );
126 }
127
128 if( $final === false ) {
129 throw new TimestampException( __METHOD__ . 'Invalid timestamp format.' );
130 }
131 $this->timestamp = $final;
132 }
133
134 /**
135 * Get the timestamp represented by this object in a certain form.
136 *
137 * Convert the internal timestamp to the specified format and then
138 * return it.
139 *
140 * @param $style Output format for timestamp
141 * @return string The formatted timestamp
142 */
143 public function getTimestamp( $style = TS_UNIX ) {
144 if( !isset( self::$formats[$style] ) ) {
145 throw new TimestampException( __METHOD__ . ' : Illegal timestamp output type.' );
146 }
147
148 if( is_object( $this->timestamp ) ) {
149 // DateTime object was used, call DateTime::format.
150 $output = $this->timestamp->format( self::$formats[$style] );
151 } elseif( TS_UNIX == $style ) {
152 // Unix timestamp was used and is wanted, just return it.
153 $output = $this->timestamp;
154 } else {
155 // Unix timestamp was used, use gmdate().
156 $output = gmdate( self::$formats[$style], $this->timestamp );
157 }
158
159 if ( ( $style == TS_RFC2822 ) || ( $style == TS_POSTGRES ) ) {
160 $output .= ' GMT';
161 }
162
163 return $output;
164 }
165
166 /**
167 * Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
168 *
169 * Determine the difference between the timestamp and the current time, and
170 * generate a readable timestamp by returning "<N> <units> ago", where the
171 * largest possible unit is used.
172 *
173 * @return string Formatted timestamp
174 */
175 public function getHumanTimestamp() {
176 $then = $this->getTimestamp( TS_UNIX );
177 $now = time();
178 $timeago = ($now - $then) * 1000;
179 $message = false;
180
181 foreach( self::$units as $unit => $factor ) {
182 $next = $timeago / $factor;
183 if( $next < 1 ) {
184 break;
185 } else {
186 $timeago = $next;
187 $message = array( $unit, floor( $timeago ) );
188 }
189 }
190
191 if( $message ) {
192 $initial = call_user_func_array( 'wfMessage', $message );
193 return wfMessage( 'ago', $initial );
194 } else {
195 return wfMessage( 'just-now' );
196 }
197 }
198
199 public function __toString() {
200 return $this->getTimestamp();
201 }
202 }
203
204 class TimestampException extends MWException {}