22 private const FIELD_CLASS =
'mw-htmlform-timezone-field';
25 private $msgFormatter;
33 if ( isset(
$params[
'options'] ) ) {
34 throw new InvalidArgumentException(
"Options should not be provided to " . __CLASS__ );
36 $params[
'placeholder-message'] ??=
'timezone-useoffset-placeholder';
39 $lang = $this->mParent ? $this->mParent->getLanguage() : RequestContext::getMain()->getLanguage();
40 $langCode = $lang->getCode();
42 ->getTextFormatter( $langCode );
43 $this->mOptions = $this->getTimezoneOptions();
49 private function getTimezoneOptions(): array {
53 $timeZoneList = $this->getTimeZoneList();
55 $timestamp = MWTimestamp::getLocalInstance();
57 if ( $localTZoffset === (
int)$timestamp->format(
'Z' ) / 60 ) {
58 $timezoneName = $timestamp->getTimezone()->getName();
60 if ( isset( $timeZoneList[$timezoneName] ) ) {
61 $timezoneName = $timeZoneList[$timezoneName][
'name'];
63 $server_tz_msg = $this->msgFormatter->format(
64 MessageValue::new(
'timezoneuseserverdefault', [ $timezoneName ] )
68 $server_tz_msg = $this->msgFormatter->format(
69 MessageValue::new(
'timezoneuseserverdefault', [ $tzstring ] )
72 $opt[$server_tz_msg] =
"System|$localTZoffset";
73 $opt[$this->msgFormatter->format( MessageValue::new(
'timezoneuseoffset' ) )] =
'other';
74 $opt[$this->msgFormatter->format( MessageValue::new(
'guesstimezone' ) )] =
'guess';
76 foreach ( $timeZoneList as $timeZoneInfo ) {
77 $region = $timeZoneInfo[
'region'];
78 if ( !isset( $opt[$region] ) ) {
81 $opt[$region][$timeZoneInfo[
'name']] = $timeZoneInfo[
'timecorrection'];
92 private function getTimeZoneList(): array {
93 $identifiers = DateTimeZone::listIdentifiers();
94 '@phan-var array|false $identifiers';
95 if ( $identifiers ===
false ) {
101 'Africa' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-africa' ) ),
102 'America' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-america' ) ),
103 'Antarctica' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-antarctica' ) ),
104 'Arctic' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-arctic' ) ),
105 'Asia' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-asia' ) ),
106 'Atlantic' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-atlantic' ) ),
107 'Australia' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-australia' ) ),
108 'Europe' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-europe' ) ),
109 'Indian' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-indian' ) ),
110 'Pacific' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-pacific' ) ),
116 $now =
new DateTime();
118 foreach ( $identifiers as $identifier ) {
119 $parts = explode(
'/', $identifier, 2 );
124 if ( count( $parts ) !== 2 || !array_key_exists( $parts[0], $tzRegions ) ) {
129 $parts[0] = $tzRegions[$parts[0]];
131 $dateTimeZone =
new DateTimeZone( $identifier );
132 $minDiff = floor( $dateTimeZone->getOffset( $now ) / 60 );
134 $display = str_replace(
'_',
' ', $parts[0] .
'/' . $parts[1] );
135 $value =
"ZoneInfo|$minDiff|$identifier";
137 $timeZoneList[$identifier] = [
139 'timecorrection' => $value,
140 'region' => $parts[0],
144 return $timeZoneList;
151 $p = parent::validate( $value, $alldata );
157 return $this->mParent->msg(
'timezone-invalid' )->escaped();
167 $classes = parent::getFieldClasses();
168 $classes[] = self::FIELD_CLASS;