21 private const FIELD_CLASS =
'mw-htmlform-timezone-field';
24 private $msgFormatter;
32 if ( isset( $params[
'options'] ) ) {
33 throw new InvalidArgumentException(
"Options should not be provided to " . __CLASS__ );
35 $params[
'placeholder-message'] ??=
'timezone-useoffset-placeholder';
36 $params[
'options'] = [];
37 parent::__construct( $params );
38 $langCode = $this->mParent->getLanguage()->getCode();
40 ->getTextFormatter( $langCode );
41 $this->mOptions = $this->getTimezoneOptions();
47 private function getTimezoneOptions(): array {
51 $timeZoneList = $this->getTimeZoneList();
53 $timestamp = MWTimestamp::getLocalInstance();
55 if ( $localTZoffset === (
int)$timestamp->format(
'Z' ) / 60 ) {
56 $timezoneName = $timestamp->getTimezone()->getName();
58 if ( isset( $timeZoneList[$timezoneName] ) ) {
59 $timezoneName = $timeZoneList[$timezoneName][
'name'];
61 $server_tz_msg = $this->msgFormatter->format(
62 MessageValue::new(
'timezoneuseserverdefault', [ $timezoneName ] )
66 $server_tz_msg = $this->msgFormatter->format(
67 MessageValue::new(
'timezoneuseserverdefault', [ $tzstring ] )
70 $opt[$server_tz_msg] =
"System|$localTZoffset";
71 $opt[$this->msgFormatter->format( MessageValue::new(
'timezoneuseoffset' ) )] =
'other';
72 $opt[$this->msgFormatter->format( MessageValue::new(
'guesstimezone' ) )] =
'guess';
74 foreach ( $timeZoneList as $timeZoneInfo ) {
75 $region = $timeZoneInfo[
'region'];
76 if ( !isset( $opt[$region] ) ) {
79 $opt[$region][$timeZoneInfo[
'name']] = $timeZoneInfo[
'timecorrection'];
90 private function getTimeZoneList(): array {
91 $identifiers = DateTimeZone::listIdentifiers();
92 '@phan-var array|false $identifiers';
93 if ( $identifiers ===
false ) {
99 'Africa' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-africa' ) ),
100 'America' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-america' ) ),
101 'Antarctica' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-antarctica' ) ),
102 'Arctic' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-arctic' ) ),
103 'Asia' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-asia' ) ),
104 'Atlantic' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-atlantic' ) ),
105 'Australia' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-australia' ) ),
106 'Europe' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-europe' ) ),
107 'Indian' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-indian' ) ),
108 'Pacific' => $this->msgFormatter->format( MessageValue::new(
'timezoneregion-pacific' ) ),
114 $now =
new DateTime();
116 foreach ( $identifiers as $identifier ) {
117 $parts = explode(
'/', $identifier, 2 );
122 if ( count( $parts ) !== 2 || !array_key_exists( $parts[0], $tzRegions ) ) {
127 $parts[0] = $tzRegions[$parts[0]];
129 $dateTimeZone =
new DateTimeZone( $identifier );
130 $minDiff = floor( $dateTimeZone->getOffset( $now ) / 60 );
132 $display = str_replace(
'_',
' ', $parts[0] .
'/' . $parts[1] );
133 $value =
"ZoneInfo|$minDiff|$identifier";
135 $timeZoneList[$identifier] = [
137 'timecorrection' => $value,
138 'region' => $parts[0],
142 return $timeZoneList;
149 $p = parent::validate( $value, $alldata );
155 return $this->mParent->msg(
'timezone-invalid' )->escaped();
165 $classes = parent::getFieldClasses();
166 $classes[] = self::FIELD_CLASS;