MediaWiki  master
MWTimestamp.php
Go to the documentation of this file.
1 <?php
27 use Wikimedia\Timestamp\ConvertibleTimestamp;
28 
37 class MWTimestamp extends ConvertibleTimestamp {
44  public static function getInstance( $ts = false ) {
45  return new static( $ts );
46  }
47 
66  public function getHumanTimestamp(
67  MWTimestamp $relativeTo = null, UserIdentity $user = null, Language $lang = null
68  ) {
69  if ( $lang === null ) {
70  $lang = RequestContext::getMain()->getLanguage();
71  }
72 
73  return $lang->getHumanTimestamp( $this, $relativeTo, $user );
74  }
75 
84  public function offsetForUser( UserIdentity $user ) {
85  global $wgLocalTZoffset;
86 
87  $option = MediaWikiServices::getInstance()
88  ->getUserOptionsLookup()
89  ->getOption( $user, 'timecorrection' );
90 
91  $data = explode( '|', $option, 3 );
92 
93  // First handle the case of an actual timezone being specified.
94  if ( $data[0] == 'ZoneInfo' ) {
95  try {
96  $tz = new DateTimeZone( $data[2] );
97  } catch ( Exception $e ) {
98  $tz = false;
99  }
100 
101  if ( $tz ) {
102  $this->timestamp->setTimezone( $tz );
103  return new DateInterval( 'P0Y' );
104  }
105 
106  $data[0] = 'Offset';
107  }
108 
109  $diff = 0;
110  // If $option is in fact a pipe-separated value, check the
111  // first value.
112  if ( $data[0] == 'System' ) {
113  // First value is System, so use the system offset.
114  if ( $wgLocalTZoffset !== null ) {
115  $diff = $wgLocalTZoffset;
116  }
117  } elseif ( $data[0] == 'Offset' ) {
118  // First value is Offset, so use the specified offset
119  $diff = (int)$data[1];
120  } else {
121  // $option actually isn't a pipe separated value, but instead
122  // a comma separated value. Isn't MediaWiki fun?
123  $data = explode( ':', $option );
124  if ( count( $data ) >= 2 ) {
125  // Combination hours and minutes.
126  $diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
127  if ( (int)$data[0] < 0 ) {
128  $diff *= -1;
129  }
130  } else {
131  // Just hours.
132  $diff = (int)$data[0] * 60;
133  }
134  }
135 
136  $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' );
137  if ( $diff < 1 ) {
138  $interval->invert = 1;
139  }
140 
141  $this->timestamp->add( $interval );
142  return $interval;
143  }
144 
155  public function getRelativeTimestamp(
156  MWTimestamp $relativeTo = null,
157  UserIdentity $user = null,
158  Language $lang = null,
159  array $chosenIntervals = []
160  ) {
161  if ( $relativeTo === null ) {
162  $relativeTo = new self;
163  }
164  if ( $user === null ) {
165  $user = RequestContext::getMain()->getUser();
166  }
167  if ( $lang === null ) {
168  $lang = RequestContext::getMain()->getLanguage();
169  }
170 
171  $ts = '';
172  $diff = $this->diff( $relativeTo );
173 
174  $user = User::newFromIdentity( $user ); // For compatibility with the hook signature
175  if ( Hooks::runner()->onGetRelativeTimestamp(
176  $ts, $diff, $this, $relativeTo, $user, $lang )
177  ) {
178  $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
179  $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
180  ->inLanguage( $lang )->text();
181  }
182 
183  return $ts;
184  }
185 
195  public function getTimezoneMessage() {
196  $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
197  $key = 'timezone-' . strtolower( trim( $tzMsg ) );
198  $msg = wfMessage( $key );
199  if ( $msg->exists() ) {
200  return $msg;
201  }
202 
203  return new RawMessage( $tzMsg );
204  }
205 
213  public static function getLocalInstance( $ts = false ) {
214  global $wgLocaltimezone;
215  $timestamp = new self( $ts );
216  $timestamp->setTimezone( $wgLocaltimezone );
217  return $timestamp;
218  }
219 }
MWTimestamp\getTimezoneMessage
getTimezoneMessage()
Get the localized timezone message, if available.
Definition: MWTimestamp.php:195
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:37
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:173
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1231
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:663
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MWTimestamp\getRelativeTimestamp
getRelativeTimestamp(MWTimestamp $relativeTo=null, UserIdentity $user=null, Language $lang=null, array $chosenIntervals=[])
Generate a purely relative timestamp, i.e., represent the time elapsed between the given base timesta...
Definition: MWTimestamp.php:155
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:44
$wgLocalTZoffset
$wgLocalTZoffset
Set an offset from UTC in minutes to use for the default timezone setting for anonymous users and new...
Definition: DefaultSettings.php:3455
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:172
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:476
MWTimestamp\getHumanTimestamp
getHumanTimestamp(MWTimestamp $relativeTo=null, UserIdentity $user=null, Language $lang=null)
Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
Definition: MWTimestamp.php:66
$wgLocaltimezone
$wgLocaltimezone
Fake out the timezone that the server thinks it's in.
Definition: DefaultSettings.php:3444
RawMessage
Variant of the Message class.
Definition: RawMessage.php:35
MWTimestamp\getLocalInstance
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
Definition: MWTimestamp.php:213
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:43
MWTimestamp\offsetForUser
offsetForUser(UserIdentity $user)
Adjust the timestamp depending on the given user's preferences.
Definition: MWTimestamp.php:84