Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.48% covered (danger)
40.48%
17 / 42
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MWTimestamp
41.46% covered (danger)
41.46%
17 / 41
20.00% covered (danger)
20.00%
1 / 5
20.84
0.00% covered (danger)
0.00%
0 / 1
 getInstance
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 offsetForUser
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 getRelativeTimestamp
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
 getTimezoneMessage
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getLocalInstance
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Creation and parsing of MW-style timestamps.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @since 1.20
22 * @author Tyler Romeo, 2012
23 */
24
25namespace MediaWiki\Utils;
26
27use DateInterval;
28use Language;
29use MediaWiki\Context\RequestContext;
30use MediaWiki\HookContainer\HookRunner;
31use MediaWiki\Language\RawMessage;
32use MediaWiki\MainConfigNames;
33use MediaWiki\MediaWikiServices;
34use MediaWiki\Message\Message;
35use MediaWiki\User\User;
36use MediaWiki\User\UserIdentity;
37use MediaWiki\User\UserTimeCorrection;
38use Wikimedia\Timestamp\ConvertibleTimestamp;
39
40/**
41 * Library for creating and parsing MW-style timestamps. Based on the JS
42 * library that does the same thing.
43 *
44 * @newable
45 *
46 * @since 1.20
47 */
48class MWTimestamp extends ConvertibleTimestamp {
49    /**
50     * Get a timestamp instance in GMT
51     *
52     * @param bool|string $ts Timestamp to set, or false for current time
53     * @return MWTimestamp The instance
54     */
55    public static function getInstance( $ts = false ) {
56        return new static( $ts );
57    }
58
59    /**
60     * Adjust the timestamp depending on the given user's preferences.
61     *
62     * @since 1.22
63     *
64     * @param UserIdentity $user User to take preferences from
65     * @return DateInterval Offset that was applied to the timestamp
66     */
67    public function offsetForUser( UserIdentity $user ) {
68        $option = MediaWikiServices::getInstance()->getUserOptionsLookup()->getOption( $user, 'timecorrection' );
69
70        $value = new UserTimeCorrection(
71            $option,
72            $this->timestamp,
73            MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LocalTZoffset )
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
85    /**
86     * Generate a purely relative timestamp, i.e., represent the time elapsed between
87     * the given base timestamp and this object.
88     *
89     * @param MWTimestamp|null $relativeTo Relative base timestamp (defaults to now)
90     * @param UserIdentity|null $user Use to use offset for
91     * @param Language|null $lang Language to use
92     * @param array $chosenIntervals Intervals to use to represent it
93     * @return string Relative timestamp
94     */
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
124    /**
125     * Get the localized timezone message, if available.
126     *
127     * Premade translations are not shipped as format() may return whatever the
128     * system uses, localized or not, so translation must be done through wiki.
129     *
130     * @since 1.27
131     * @return Message The localized timezone message
132     */
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
144    /**
145     * Get a timestamp instance in the server local timezone ($wgLocaltimezone)
146     *
147     * @since 1.22
148     * @param bool|string $ts Timestamp to set, or false for current time
149     * @return MWTimestamp The local instance
150     */
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
159/** @deprecated class alias since 1.41 */
160class_alias( MWTimestamp::class, 'MWTimestamp' );