MediaWiki REL1_37
MWTimestamp.php
Go to the documentation of this file.
1<?php
28use Wikimedia\Timestamp\ConvertibleTimestamp;
29
38class 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}
$wgLocaltimezone
Fake out the timezone that the server thinks it's in.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:42
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...
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:684
Interface for objects representing user identity.
if(!isset( $args[0])) $lang