MediaWiki REL1_39
MWTimestamp.php
Go to the documentation of this file.
1<?php
29use Wikimedia\Timestamp\ConvertibleTimestamp;
30
39class MWTimestamp extends ConvertibleTimestamp {
46 public static function getInstance( $ts = false ) {
47 return new static( $ts );
48 }
49
68 public function getHumanTimestamp(
69 MWTimestamp $relativeTo = null, UserIdentity $user = null, Language $lang = null
70 ) {
71 wfDeprecated( __METHOD__, '1.26' );
72 if ( $lang === null ) {
73 $lang = RequestContext::getMain()->getLanguage();
74 }
75
76 return $lang->getHumanTimestamp( $this, $relativeTo, $user );
77 }
78
87 public function offsetForUser( UserIdentity $user ) {
88 $option = MediaWikiServices::getInstance()
89 ->getUserOptionsLookup()
90 ->getOption( $user, 'timecorrection' );
91
92 $value = new UserTimeCorrection(
93 $option,
94 $this->timestamp,
95 MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LocalTZoffset )
96 );
97 $tz = $value->getTimeZone();
98 if ( $tz ) {
99 $this->timestamp->setTimezone( $tz );
100 return new DateInterval( 'P0Y' );
101 }
102 $interval = $value->getTimeOffsetInterval();
103 $this->timestamp->add( $interval );
104 return $interval;
105 }
106
117 public function getRelativeTimestamp(
118 MWTimestamp $relativeTo = null,
119 UserIdentity $user = null,
120 Language $lang = null,
121 array $chosenIntervals = []
122 ) {
123 if ( $relativeTo === null ) {
124 $relativeTo = new self;
125 }
126 if ( $user === null ) {
127 $user = RequestContext::getMain()->getUser();
128 }
129 if ( $lang === null ) {
130 $lang = RequestContext::getMain()->getLanguage();
131 }
132
133 $ts = '';
134 $diff = $this->diff( $relativeTo );
135
136 $user = User::newFromIdentity( $user ); // For compatibility with the hook signature
137 if ( Hooks::runner()->onGetRelativeTimestamp(
138 $ts, $diff, $this, $relativeTo, $user, $lang )
139 ) {
140 $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
141 $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
142 ->inLanguage( $lang )->text();
143 }
144
145 return $ts;
146 }
147
157 public function getTimezoneMessage() {
158 $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
159 $key = 'timezone-' . strtolower( trim( $tzMsg ) );
160 $msg = wfMessage( $key );
161 if ( $msg->exists() ) {
162 return $msg;
163 }
164
165 return new RawMessage( $tzMsg );
166 }
167
175 public static function getLocalInstance( $ts = false ) {
176 $localtimezone = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Localtimezone );
177 $timestamp = new self( $ts );
178 $timestamp->setTimezone( $localtimezone );
179 return $timestamp;
180 }
181}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Base class for language-specific code.
Definition Language.php:53
Library for creating and parsing MW-style timestamps.
offsetForUser(UserIdentity $user)
Adjust the timestamp depending on the given user's preferences.
static getInstance( $ts=false)
Get a timestamp instance in GMT.
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
getHumanTimestamp(MWTimestamp $relativeTo=null, UserIdentity $user=null, Language $lang=null)
Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
getTimezoneMessage()
Get the localized timezone message, if available.
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...
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Utility class to parse the TimeCorrection string value.
Variant of the Message class.
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition User.php:675
Interface for objects representing user identity.
if(!isset( $args[0])) $lang