summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/Time/IntlTimeFormatter.php
blob: 888d6711c419968bfff4c260143d385d17abf537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php

namespace SMW\DataValues\Time;

use Language;
use SMW\Localizer;
use SMWDITime as DITime;

/**
 * @license GNU GPL v2+
 * @since 2.4
 *
 * @author mwjames
 */
class IntlTimeFormatter {

	const LOCL_DEFAULT = 0;
	const LOCL_TIMEZONE = 0x2;
	const LOCL_TIMEOFFSET = 0x4;

	/**
	 * @var DITime
	 */
	private $dataItem;

	/**
	 * @var Language
	 */
	private $language;

	/**
	 * @var boolean
	 */
	private $hasLocalTimeCorrection = false;

	/**
	 * @since 2.4
	 *
	 * @param DITime $dataItem
	 * @param Language|null $language
	 */
	public function __construct( DITime $dataItem, Language $language = null ) {
		$this->dataItem = $dataItem;
		$this->language = $language;

		if ( $this->language === null ) {
			$this->language = Localizer::getInstance()->getContentLanguage();
		}
	}

	/**
	 * @since 3.0
	 *
	 * @return boolean
	 */
	public function hasLocalTimeCorrection() {
		return $this->hasLocalTimeCorrection;
	}

	/**
	 * @since 2.4
	 *
	 * @param integer $formatFlag
	 *
	 * @return string|boolean
	 */
	public function getLocalizedFormat( $formatFlag = self::LOCL_DEFAULT ) {

		$dateTime = $this->dataItem->asDateTime();
		$timezone = '';

		$this->hasLocalTimeCorrection = false;

		if ( !$dateTime ) {
			return false;
		}

		$localizer = Localizer::getInstance();

		if ( ( self::LOCL_TIMEOFFSET & $formatFlag ) != 0 ) {
			$dateTime = $localizer->getLocalTime( $dateTime );
			$this->hasLocalTimeCorrection = isset( $dateTime->hasLocalTimeCorrection ) ? $dateTime->hasLocalTimeCorrection : false;
		}

		if ( ( self::LOCL_TIMEZONE & $formatFlag ) != 0 ) {
			$timezone = $this->dataItem->getTimezone();
			$dateTime = Timezone::getModifiedTime( $dateTime, $timezone );
		}

		$lang = $localizer->getLang(
			$this->language
		);

		$precision = $this->dataItem->getPrecision();

		// Look for the Z precision which indicates the position of the TZ
		if ( $precision === SMW_PREC_YMDT && $timezone !== '' ) {
			$precision = SMW_PREC_YMDTZ;
		}

		$preferredDateFormatByPrecision = $lang->getPreferredDateFormatByPrecision(
			$precision
		);

		// Mark the position since we cannot use DateTime::setTimezone in case
		// it is a military zone
		$preferredDateFormatByPrecision = str_replace( 'T', '**', $preferredDateFormatByPrecision );

		$dateString = $this->formatWithLocalizedTextReplacement(
			$dateTime,
			$preferredDateFormatByPrecision
		);

		return str_replace( '**', $timezone, $dateString );
	}

	/**
	 * Permitted formatting options are specified by http://php.net/manual/en/function.date.php
	 *
	 * @since 2.4
	 *
	 * @param string $format
	 *
	 * @return string|boolean
	 */
	public function format( $format ) {

		$dateTime = $this->dataItem->asDateTime();

		if ( !$dateTime ) {
			return false;
		}

		$output = $this->formatWithLocalizedTextReplacement(
			$dateTime,
			$format
		);

		return $output;
	}

	/**
	 * @since 2.4
	 *
	 * @param string $format
	 *
	 * @return boolean
	 */
	public function containsValidDateFormatRule( $format ) {

		foreach ( str_split( $format ) as $value ) {
			if ( in_array( $value, [ 'd', 'D', 'j', 'l', 'N', 'w', 'W', 'F', 'M', 'm', 'n', 't', 'L', 'o', 'Y', 'y', "c", 'r' ] ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * DateTime generally outputs English textual representation
	 *
	 * - D	A textual representation of a day, three letters
	 * - l (lowercase 'L'), A full textual representation of the day of the week
	 * - F	A full textual representation of a month, such as January or March
	 * - M	A short textual representation of a month, three letters
	 * - a	Lowercase Ante meridiem and Post meridiem am or pm
	 * - A	Uppercase Ante meridiem and Post meridiem
	 */
	private function formatWithLocalizedTextReplacement( $dateTime, $format ) {

		$output = $dateTime->format( $format );

		// (n) DateTime => 1 through 12
		$monthNumber = $dateTime->format( 'n' );

		// (N) DateTime => 1 (for Monday) through 7 (for Sunday)
		// (w) DateTime => 0 (for Sunday) through 6 (for Saturday)
		// MW => 1 (for Sunday) through 7 (for Saturday)
		$dayNumber = $dateTime->format( 'w' ) + 1;

		if ( strpos( $format, 'F' ) !== false ) {
			$output = str_replace(
				$dateTime->format( 'F' ),
				$this->language->getMonthName( $monthNumber ),
				$output
			);
		}

		if ( strpos( $format, 'M' ) !== false ) {
			$output = str_replace(
				$dateTime->format( 'M' ),
				$this->language->getMonthAbbreviation( $monthNumber ),
				$output
			);
		}

		if ( strpos( $format, 'l' ) !== false ) {
			$output = str_replace(
				$dateTime->format( 'l' ),
				$this->language->getWeekdayName( $dayNumber ),
				$output
			);
		}

		if ( strpos( $format, 'D' ) !== false ) {
			$output = str_replace(
				$dateTime->format( 'D' ),
				$this->language->getWeekdayAbbreviation( $dayNumber ),
				$output
			);
		}

		return $output;
	}

}