MediaWiki  master
MWTimestamp.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\Timestamp\ConvertibleTimestamp;
25 
34 class MWTimestamp extends ConvertibleTimestamp {
41  public static function getInstance( $ts = false ) {
42  return new static( $ts );
43  }
44 
63  public function getHumanTimestamp(
64  MWTimestamp $relativeTo = null, User $user = null, Language $lang = null
65  ) {
66  if ( $lang === null ) {
67  $lang = RequestContext::getMain()->getLanguage();
68  }
69 
70  return $lang->getHumanTimestamp( $this, $relativeTo, $user );
71  }
72 
81  public function offsetForUser( User $user ) {
82  global $wgLocalTZoffset;
83 
84  $option = $user->getOption( 'timecorrection' );
85  $data = explode( '|', $option, 3 );
86 
87  // First handle the case of an actual timezone being specified.
88  if ( $data[0] == 'ZoneInfo' ) {
89  try {
90  $tz = new DateTimeZone( $data[2] );
91  } catch ( Exception $e ) {
92  $tz = false;
93  }
94 
95  if ( $tz ) {
96  $this->timestamp->setTimezone( $tz );
97  return new DateInterval( 'P0Y' );
98  }
99 
100  $data[0] = 'Offset';
101  }
102 
103  $diff = 0;
104  // If $option is in fact a pipe-separated value, check the
105  // first value.
106  if ( $data[0] == 'System' ) {
107  // First value is System, so use the system offset.
108  if ( $wgLocalTZoffset !== null ) {
109  $diff = $wgLocalTZoffset;
110  }
111  } elseif ( $data[0] == 'Offset' ) {
112  // First value is Offset, so use the specified offset
113  $diff = (int)$data[1];
114  } else {
115  // $option actually isn't a pipe separated value, but instead
116  // a comma separated value. Isn't MediaWiki fun?
117  $data = explode( ':', $option );
118  if ( count( $data ) >= 2 ) {
119  // Combination hours and minutes.
120  $diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
121  if ( (int)$data[0] < 0 ) {
122  $diff *= -1;
123  }
124  } else {
125  // Just hours.
126  $diff = (int)$data[0] * 60;
127  }
128  }
129 
130  $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' );
131  if ( $diff < 1 ) {
132  $interval->invert = 1;
133  }
134 
135  $this->timestamp->add( $interval );
136  return $interval;
137  }
138 
149  public function getRelativeTimestamp(
150  MWTimestamp $relativeTo = null,
151  User $user = null,
152  Language $lang = null,
153  array $chosenIntervals = []
154  ) {
155  if ( $relativeTo === null ) {
156  $relativeTo = new self;
157  }
158  if ( $user === null ) {
159  $user = RequestContext::getMain()->getUser();
160  }
161  if ( $lang === null ) {
162  $lang = RequestContext::getMain()->getLanguage();
163  }
164 
165  $ts = '';
166  $diff = $this->diff( $relativeTo );
167  if ( Hooks::runner()->onGetRelativeTimestamp(
168  $ts, $diff, $this, $relativeTo, $user, $lang )
169  ) {
170  $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
171  $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
172  ->inLanguage( $lang )->text();
173  }
174 
175  return $ts;
176  }
177 
187  public function getTimezoneMessage() {
188  $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
189  $key = 'timezone-' . strtolower( trim( $tzMsg ) );
190  $msg = wfMessage( $key );
191  if ( $msg->exists() ) {
192  return $msg;
193  }
194 
195  return new RawMessage( $tzMsg );
196  }
197 
205  public static function getLocalInstance( $ts = false ) {
206  global $wgLocaltimezone;
207  $timestamp = new self( $ts );
208  $timestamp->setTimezone( $wgLocaltimezone );
209  return $timestamp;
210  }
211 }
MWTimestamp\getTimezoneMessage
getTimezoneMessage()
Get the localized timezone message, if available.
Definition: MWTimestamp.php:187
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:34
$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:1220
MWTimestamp\offsetForUser
offsetForUser(User $user)
Adjust the timestamp depending on the given user's preferences.
Definition: MWTimestamp.php:81
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:41
MWTimestamp\getRelativeTimestamp
getRelativeTimestamp(MWTimestamp $relativeTo=null, User $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:149
$wgLocalTZoffset
$wgLocalTZoffset
Set an offset from UTC in minutes to use for the default timezone setting for anonymous users and new...
Definition: DefaultSettings.php:3352
User\getOption
getOption( $oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
Definition: User.php:2591
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:451
$wgLocaltimezone
$wgLocaltimezone
Fake out the timezone that the server thinks it's in.
Definition: DefaultSettings.php:3341
RawMessage
Variant of the Message class.
Definition: RawMessage.php:35
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
MWTimestamp\getLocalInstance
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
Definition: MWTimestamp.php:205
MWTimestamp\getHumanTimestamp
getHumanTimestamp(MWTimestamp $relativeTo=null, User $user=null, Language $lang=null)
Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
Definition: MWTimestamp.php:63
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41