MediaWiki REL1_40
MWTimestamp.php
Go to the documentation of this file.
1<?php
30use Wikimedia\Timestamp\ConvertibleTimestamp;
31
40class MWTimestamp extends ConvertibleTimestamp {
47 public static function getInstance( $ts = false ) {
48 return new static( $ts );
49 }
50
59 public function offsetForUser( UserIdentity $user ) {
60 $option = MediaWikiServices::getInstance()
61 ->getUserOptionsLookup()
62 ->getOption( $user, 'timecorrection' );
63
64 $value = new UserTimeCorrection(
65 $option,
66 $this->timestamp,
67 MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LocalTZoffset )
68 );
69 $tz = $value->getTimeZone();
70 if ( $tz ) {
71 $this->timestamp->setTimezone( $tz );
72 return new DateInterval( 'P0Y' );
73 }
74 $interval = $value->getTimeOffsetInterval();
75 $this->timestamp->add( $interval );
76 return $interval;
77 }
78
89 public function getRelativeTimestamp(
90 MWTimestamp $relativeTo = null,
91 UserIdentity $user = null,
92 Language $lang = null,
93 array $chosenIntervals = []
94 ) {
95 $relativeTo ??= new self();
96 $user ??= RequestContext::getMain()->getUser();
97 $lang ??= RequestContext::getMain()->getLanguage();
98
99 $ts = '';
100 $diff = $this->diff( $relativeTo );
101
102 $user = User::newFromIdentity( $user ); // For compatibility with the hook signature
103 if ( Hooks::runner()->onGetRelativeTimestamp(
104 $ts, $diff, $this, $relativeTo, $user, $lang )
105 ) {
106 $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
107 $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
108 ->inLanguage( $lang )->text();
109 }
110
111 return $ts;
112 }
113
123 public function getTimezoneMessage() {
124 $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
125 $key = 'timezone-' . strtolower( trim( $tzMsg ) );
126 $msg = wfMessage( $key );
127 if ( $msg->exists() ) {
128 return $msg;
129 }
130
131 return new RawMessage( $tzMsg );
132 }
133
141 public static function getLocalInstance( $ts = false ) {
142 $localtimezone = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Localtimezone );
143 $timestamp = new self( $ts );
144 $timestamp->setTimezone( $localtimezone );
145 return $timestamp;
146 }
147}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Base class for language-specific code.
Definition Language.php:56
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)
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...
Variant of the Message class.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Utility class to parse the TimeCorrection string value.
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition User.php:662
Interface for objects representing user identity.
if(!isset( $args[0])) $lang