MediaWiki REL1_28
ConvertibleTimestamp.php
Go to the documentation of this file.
1<?php
35 private static $formats = [
36 TS_UNIX => 'U',
37 TS_MW => 'YmdHis',
38 TS_DB => 'Y-m-d H:i:s',
39 TS_ISO_8601 => 'Y-m-d\TH:i:s\Z',
40 TS_ISO_8601_BASIC => 'Ymd\THis\Z',
41 TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness
42 TS_RFC2822 => 'D, d M Y H:i:s',
43 TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500
44 TS_POSTGRES => 'Y-m-d H:i:s',
45 ];
46
51 public $timestamp;
52
59 public function __construct( $timestamp = false ) {
60 if ( $timestamp instanceof DateTime ) {
61 $this->timestamp = $timestamp;
62 } else {
63 $this->setTimestamp( $timestamp );
64 }
65 }
66
76 public function setTimestamp( $ts = false ) {
77 $m = [];
78 $da = [];
79 $strtime = '';
80
81 // We want to catch 0, '', null... but not date strings starting with a letter.
82 if ( !$ts || $ts === "\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ) {
83 $uts = time();
84 $strtime = "@$uts";
85 } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
86 # TS_DB
87 } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
88 # TS_EXIF
89 } elseif ( preg_match( '/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/D', $ts, $da ) ) {
90 # TS_MW
91 } elseif ( preg_match( '/^(-?\d{1,13})(\.\d+)?$/D', $ts, $m ) ) {
92 # TS_UNIX
93 $strtime = "@{$m[1]}"; // http://php.net/manual/en/datetime.formats.compound.php
94 } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
95 # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
96 $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
97 str_replace( '+00:00', 'UTC', $ts ) );
98 } elseif ( preg_match(
99 '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z?$/',
100 $ts,
101 $da
102 ) ) {
103 # TS_ISO_8601
104 } elseif ( preg_match(
105 '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z?$/',
106 $ts,
107 $da
108 ) ) {
109 # TS_ISO_8601_BASIC
110 } elseif ( preg_match(
111 '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/',
112 $ts,
113 $da
114 ) ) {
115 # TS_POSTGRES
116 } elseif ( preg_match(
117 '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/',
118 $ts,
119 $da
120 ) ) {
121 # TS_POSTGRES
122 } elseif ( preg_match(
123 # Day of week
124 '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' .
125 # dd Mon yyyy
126 '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' .
127 # hh:mm:ss
128 '[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d/S',
129 $ts
130 ) ) {
131 # TS_RFC2822, accepting a trailing comment.
132 # See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171
133 # The regex is a superset of rfc2822 for readability
134 $strtime = strtok( $ts, ';' );
135 } 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 ) ) {
136 # TS_RFC850
137 $strtime = $ts;
138 } 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 ) ) {
139 # asctime
140 $strtime = $ts;
141 } else {
142 throw new TimestampException( __METHOD__ . ": Invalid timestamp - $ts" );
143 }
144
145 if ( !$strtime ) {
146 $da = array_map( 'intval', $da );
147 $da[0] = "%04d-%02d-%02dT%02d:%02d:%02d.00+00:00";
148 $strtime = call_user_func_array( "sprintf", $da );
149 }
150
151 try {
152 $final = new DateTime( $strtime, new DateTimeZone( 'GMT' ) );
153 } catch ( Exception $e ) {
154 throw new TimestampException( __METHOD__ . ': Invalid timestamp format.', $e->getCode(), $e );
155 }
156
157 if ( $final === false ) {
158 throw new TimestampException( __METHOD__ . ': Invalid timestamp format.' );
159 }
160
161 $this->timestamp = $final;
162 }
163
171 public static function convert( $style = TS_UNIX, $ts ) {
172 try {
173 $ct = new static( $ts );
174 return $ct->getTimestamp( $style );
175 } catch ( TimestampException $e ) {
176 return false;
177 }
178 }
179
186 public static function now( $style = TS_MW ) {
187 return static::convert( $style, time() );
188 }
189
200 public function getTimestamp( $style = TS_UNIX ) {
201 if ( !isset( self::$formats[$style] ) ) {
202 throw new TimestampException( __METHOD__ . ': Illegal timestamp output type.' );
203 }
204
205 $output = $this->timestamp->format( self::$formats[$style] );
206
207 if ( ( $style == TS_RFC2822 ) || ( $style == TS_POSTGRES ) ) {
208 $output .= ' GMT';
209 }
210
211 if ( $style == TS_MW && strlen( $output ) !== 14 ) {
212 throw new TimestampException( __METHOD__ . ': The timestamp cannot be represented in ' .
213 'the specified format' );
214 }
215
216 return $output;
217 }
218
222 public function __toString() {
223 return $this->getTimestamp();
224 }
225
233 public function diff( ConvertibleTimestamp $relativeTo ) {
234 return $this->timestamp->diff( $relativeTo->timestamp );
235 }
236
243 public function setTimezone( $timezone ) {
244 try {
245 $this->timestamp->setTimezone( new DateTimeZone( $timezone ) );
246 } catch ( Exception $e ) {
247 throw new TimestampException( __METHOD__ . ': Invalid timezone.', $e->getCode(), $e );
248 }
249 }
250
256 public function getTimezone() {
257 return $this->timestamp->getTimezone();
258 }
259
266 public function format( $format ) {
267 return $this->timestamp->format( $format );
268 }
269}
Library for creating, parsing, and converting timestamps.
format( $format)
Format the timestamp in a given format.
static convert( $style=TS_UNIX, $ts)
Convert a timestamp string to a given format.
setTimestamp( $ts=false)
Set the timestamp to the specified time, or the current time if unspecified.
__construct( $timestamp=false)
Make a new timestamp and set it to the specified time, or the current time if unspecified.
diff(ConvertibleTimestamp $relativeTo)
Calculate the difference between two ConvertibleTimestamp objects.
getTimezone()
Get the timezone of this timestamp.
DateTime $timestamp
The actual timestamp being wrapped (DateTime object).
setTimezone( $timezone)
Set the timezone of this timestamp to the specified timezone.
static now( $style=TS_MW)
Get the current time in the given format.
static $formats
Standard gmdate() formats for the different timestamp types.
getTimestamp( $style=TS_UNIX)
Get the timestamp represented by this object in a certain form.
globals txt Globals are evil The original MediaWiki code relied on globals for processing context far too often MediaWiki development since then has been a story of slowly moving context out of global variables and into objects Storing processing context in object member variables allows those objects to be reused in a much more flexible way Consider the elegance of
database rows
Definition globals.txt:10
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition hooks.txt:1102
returning false will NOT prevent logging $e
Definition hooks.txt:2110
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
const TS_ORACLE
Oracle format time.
Definition defines.php:42
const TS_POSTGRES
Postgres format time.
Definition defines.php:47
const TS_DB
MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
Definition defines.php:16
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition defines.php:28
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition defines.php:6
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition defines.php:11
const TS_RFC2822
RFC 2822 format, for E-mail and HTTP headers.
Definition defines.php:21
const TS_ISO_8601_BASIC
ISO 8601 basic format with no timezone: 19860209T200000Z.
Definition defines.php:52
const TS_EXIF
An Exif timestamp (YYYY:MM:DD HH:MM:SS)
Definition defines.php:37