1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW; |
4
|
|
|
|
5
|
|
|
use Language; |
6
|
|
|
use SMWDITime as DITime; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @license GNU GPL v2+ |
10
|
|
|
* @since 2.4 |
11
|
|
|
* |
12
|
|
|
* @author mwjames |
13
|
|
|
*/ |
14
|
|
|
class IntlTimeFormatter { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var DITime |
18
|
|
|
*/ |
19
|
|
|
private $dataItem; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Language |
23
|
|
|
*/ |
24
|
|
|
private $language; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @since 2.4 |
28
|
|
|
* |
29
|
|
|
* @param DITime $dataItem |
30
|
|
|
* @param Language|null $language |
31
|
|
|
*/ |
32
|
12 |
|
public function __construct( DITime $dataItem, Language $language = null ) { |
33
|
12 |
|
$this->dataItem = $dataItem; |
34
|
12 |
|
$this->language = $language; |
35
|
|
|
|
36
|
12 |
|
if ( $this->language === null ) { |
37
|
1 |
|
$this->language = Localizer::getInstance()->getContentLanguage(); |
38
|
|
|
} |
39
|
12 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @since 2.4 |
43
|
|
|
* |
44
|
|
|
* @return string|boolean |
45
|
|
|
*/ |
46
|
3 |
|
public function getLocalizedFormat() { |
47
|
|
|
|
48
|
3 |
|
$dateTime = $this->dataItem->asDateTime(); |
49
|
|
|
|
50
|
3 |
|
if ( !$dateTime ) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
$extraneousLanguage = Localizer::getInstance()->getExtraneousLanguage( |
55
|
3 |
|
$this->language |
56
|
|
|
); |
57
|
|
|
|
58
|
3 |
|
$preferredDateFormatByPrecision = $extraneousLanguage->getPreferredDateFormatByPrecision( |
59
|
3 |
|
$this->dataItem->getPrecision() |
60
|
|
|
); |
61
|
|
|
|
62
|
3 |
|
return $this->formatWithLocalizedTextReplacement( |
63
|
|
|
$dateTime, |
64
|
|
|
$preferredDateFormatByPrecision |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Permitted formatting options are specified by http://php.net/manual/en/function.date.php |
70
|
|
|
* |
71
|
|
|
* @since 2.4 |
72
|
|
|
* |
73
|
|
|
* @param string $format |
74
|
|
|
* |
75
|
|
|
* @return string|boolean |
76
|
|
|
*/ |
77
|
7 |
|
public function format( $format ) { |
78
|
|
|
|
79
|
7 |
|
$dateTime = $this->dataItem->asDateTime(); |
80
|
|
|
|
81
|
7 |
|
if ( !$dateTime ) { |
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
$output = $this->formatWithLocalizedTextReplacement( |
86
|
|
|
$dateTime, |
87
|
|
|
$format |
88
|
|
|
); |
89
|
|
|
|
90
|
7 |
|
return $output; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @since 2.4 |
95
|
|
|
* |
96
|
|
|
* @param string $format |
97
|
|
|
* |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
2 |
|
public function containsValidDateFormatRule( $format ) { |
101
|
|
|
|
102
|
2 |
|
foreach ( str_split( $format ) as $value ) { |
103
|
2 |
|
if ( in_array( $value, array( 'd', 'D', 'j', 'l', 'N', 'w', 'W', 'F', 'M', 'm', 'n', 't', 'L', 'o', 'Y', 'y', "c", 'r' ) ) ) { |
104
|
2 |
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* DateTime generally outputs English textual representation |
113
|
|
|
* |
114
|
|
|
* - D A textual representation of a day, three letters |
115
|
|
|
* - l (lowercase 'L'), A full textual representation of the day of the week |
116
|
|
|
* - F A full textual representation of a month, such as January or March |
117
|
|
|
* - M A short textual representation of a month, three letters |
118
|
|
|
* - a Lowercase Ante meridiem and Post meridiem am or pm |
119
|
|
|
* - A Uppercase Ante meridiem and Post meridiem |
120
|
|
|
*/ |
121
|
10 |
|
private function formatWithLocalizedTextReplacement( $dateTime, $format ) { |
122
|
|
|
|
123
|
10 |
|
$output = $dateTime->format( $format ); |
124
|
|
|
|
125
|
10 |
|
$monthNumber = $dateTime->format( 'n' ); |
126
|
10 |
|
$dayNumber = $dateTime->format( 'N' ); |
127
|
|
|
|
128
|
10 |
|
if ( strpos( $format, 'F' ) !== false ) { |
129
|
2 |
|
$output = str_replace( |
130
|
2 |
|
$dateTime->format( 'F' ), |
131
|
2 |
|
$this->language->getMonthName( $monthNumber ), |
132
|
|
|
$output |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
10 |
|
if ( strpos( $format, 'M' ) !== false ) { |
137
|
1 |
|
$output = str_replace( |
138
|
1 |
|
$dateTime->format( 'M' ), |
139
|
1 |
|
$this->language->getMonthAbbreviation( $monthNumber ), |
140
|
|
|
$output |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
10 |
|
if ( strpos( $format, 'l' ) !== false ) { |
145
|
|
|
$output = str_replace( |
146
|
|
|
$dateTime->format( 'l' ), |
147
|
|
|
$this->language->getWeekdayName( $dayNumber ), |
148
|
|
|
$output |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
10 |
|
if ( strpos( $format, 'D' ) !== false ) { |
153
|
1 |
|
$output = str_replace( |
154
|
1 |
|
$dateTime->format( 'D' ), |
155
|
1 |
|
$this->language->getWeekdayAbbreviation( $dayNumber ), |
156
|
|
|
$output |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
10 |
|
return $output; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|