MediaWiki master
MWTimestamp.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Utils;
26
27use DateInterval;
28use Language;
38use Wikimedia\Timestamp\ConvertibleTimestamp;
39
48class MWTimestamp extends ConvertibleTimestamp {
55 public static function getInstance( $ts = false ) {
56 return new static( $ts );
57 }
58
67 public function offsetForUser( UserIdentity $user ) {
68 $option = MediaWikiServices::getInstance()->getUserOptionsLookup()->getOption( $user, 'timecorrection' );
69
70 $value = new UserTimeCorrection(
71 $option,
72 $this->timestamp,
74 );
75 $tz = $value->getTimeZone();
76 if ( $tz ) {
77 $this->timestamp->setTimezone( $tz );
78 return new DateInterval( 'P0Y' );
79 }
80 $interval = $value->getTimeOffsetInterval();
81 $this->timestamp->add( $interval );
82 return $interval;
83 }
84
95 public function getRelativeTimestamp(
96 MWTimestamp $relativeTo = null,
97 UserIdentity $user = null,
98 Language $lang = null,
99 array $chosenIntervals = []
100 ) {
101 $relativeTo ??= new self();
102 $user ??= RequestContext::getMain()->getUser();
103 $lang ??= RequestContext::getMain()->getLanguage();
104
105 $ts = '';
106 $diff = $this->diff( $relativeTo );
107
108 $user = User::newFromIdentity( $user ); // For compatibility with the hook signature
109 if ( ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onGetRelativeTimestamp(
110 $ts,
111 $diff,
112 $this,
113 $relativeTo,
114 $user,
115 $lang
116 ) ) {
117 $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
118 $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )->inLanguage( $lang )->text();
119 }
120
121 return $ts;
122 }
123
133 public function getTimezoneMessage() {
134 $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
135 $key = 'timezone-' . strtolower( trim( $tzMsg ) );
136 $msg = wfMessage( $key );
137 if ( $msg->exists() ) {
138 return $msg;
139 }
140
141 return new RawMessage( $tzMsg );
142 }
143
151 public static function getLocalInstance( $ts = false ) {
152 $localtimezone = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Localtimezone );
153 $timestamp = new self( $ts );
154 $timestamp->setTimezone( $localtimezone );
155 return $timestamp;
156 }
157}
158
160class_alias( MWTimestamp::class, 'MWTimestamp' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Base class for language-specific code.
Definition Language.php:63
Group all the pieces relevant to the context of a request into one instance.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Variant of the Message class.
A class containing constants representing the names of configuration variables.
const Localtimezone
Name constant for the Localtimezone setting, for use with Config::get()
const LocalTZoffset
Name constant for the LocalTZoffset setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:158
Utility class to parse the TimeCorrection string value.
internal since 1.36
Definition User.php:93
Library for creating and parsing MW-style timestamps.
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
offsetForUser(UserIdentity $user)
Adjust the timestamp depending on the given user's preferences.
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...
getTimezoneMessage()
Get the localized timezone message, if available.
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Interface for objects representing user identity.