MediaWiki  master
MWTimestamp.php
Go to the documentation of this file.
1 <?php
28 use Wikimedia\Timestamp\ConvertibleTimestamp;
29 
38 class MWTimestamp extends ConvertibleTimestamp {
45  public static function getInstance( $ts = false ) {
46  return new static( $ts );
47  }
48 
67  public function getHumanTimestamp(
68  MWTimestamp $relativeTo = null, UserIdentity $user = null, Language $lang = null
69  ) {
70  if ( $lang === null ) {
71  $lang = RequestContext::getMain()->getLanguage();
72  }
73 
74  return $lang->getHumanTimestamp( $this, $relativeTo, $user );
75  }
76 
85  public function offsetForUser( UserIdentity $user ) {
86  $option = MediaWikiServices::getInstance()
87  ->getUserOptionsLookup()
88  ->getOption( $user, 'timecorrection' );
89 
90  $value = new UserTimeCorrection(
91  $option,
92  $this->timestamp,
93  MediaWikiServices::getInstance()->getMainConfig()->get( 'LocalTZoffset' )
94  );
95  $tz = $value->getTimeZone();
96  if ( $tz ) {
97  $this->timestamp->setTimezone( $tz );
98  return new DateInterval( 'P0Y' );
99  }
100  $interval = $value->getTimeOffsetInterval();
101  $this->timestamp->add( $interval );
102  return $interval;
103  }
104 
115  public function getRelativeTimestamp(
116  MWTimestamp $relativeTo = null,
117  UserIdentity $user = null,
118  Language $lang = null,
119  array $chosenIntervals = []
120  ) {
121  if ( $relativeTo === null ) {
122  $relativeTo = new self;
123  }
124  if ( $user === null ) {
125  $user = RequestContext::getMain()->getUser();
126  }
127  if ( $lang === null ) {
128  $lang = RequestContext::getMain()->getLanguage();
129  }
130 
131  $ts = '';
132  $diff = $this->diff( $relativeTo );
133 
134  $user = User::newFromIdentity( $user ); // For compatibility with the hook signature
135  if ( Hooks::runner()->onGetRelativeTimestamp(
136  $ts, $diff, $this, $relativeTo, $user, $lang )
137  ) {
138  $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
139  $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
140  ->inLanguage( $lang )->text();
141  }
142 
143  return $ts;
144  }
145 
155  public function getTimezoneMessage() {
156  $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
157  $key = 'timezone-' . strtolower( trim( $tzMsg ) );
158  $msg = wfMessage( $key );
159  if ( $msg->exists() ) {
160  return $msg;
161  }
162 
163  return new RawMessage( $tzMsg );
164  }
165 
173  public static function getLocalInstance( $ts = false ) {
174  global $wgLocaltimezone;
175  $timestamp = new self( $ts );
176  $timestamp->setTimezone( $wgLocaltimezone );
177  return $timestamp;
178  }
179 }
MWTimestamp\getTimezoneMessage
getTimezoneMessage()
Get the localized timezone message, if available.
Definition: MWTimestamp.php:155
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:38
MediaWiki\User\UserTimeCorrection
Utility class to parse the TimeCorrection string value.
Definition: UserTimeCorrection.php:41
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:186
$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:1182
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:679
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:115
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:45
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
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:67
$wgLocaltimezone
$wgLocaltimezone
Fake out the timezone that the server thinks it's in.
Definition: DefaultSettings.php:3712
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:173
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
MWTimestamp\offsetForUser
offsetForUser(UserIdentity $user)
Adjust the timestamp depending on the given user's preferences.
Definition: MWTimestamp.php:85