diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php index 8e54561..4d25b57 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php @@ -55,14 +55,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { */ protected function formatDate($date) { $format = $this->getSetting('date_format'); - $timezone_override = $this->getSetting('timezone_override'); - if ($this->getSetting('timezone_display') === DateTimeItem::TIMEZONE_NONE && $timezone_override) { - $timezone = $timezone_override; - } - else { - $timezone = $date->getTimezone()->getName(); - } - return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL); + return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $date->getTimezone()->getName()); } /** @@ -89,7 +82,7 @@ public function settingsSummary() { $date = new DrupalDateTime(); $this->setTimeZone($date); - $summary[] = $date->format($this->getSetting('date_format'), $this->getFormatSettings()); + $summary[] = $date->format($this->getSetting('date_format')); return $summary; } diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php index 5fc5ef9..6386630 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php @@ -33,14 +33,7 @@ public static function defaultSettings() { */ protected function formatDate($date) { $format_type = $this->getSetting('format_type'); - $timezone_override = $this->getSetting('timezone_override'); - if ($this->getSetting('timezone_display') === DateTimeItem::TIMEZONE_NONE && $timezone_override) { - $timezone = $timezone_override; - } - else { - $timezone = $date->getTimezone()->getName(); - } - return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL); + return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $date->getTimezone()->getName()); } /** @@ -75,7 +68,8 @@ public function settingsSummary() { $summary = parent::settingsSummary(); $date = new DrupalDateTime(); - $summary[] = t('Format: @display', array('@display' => $this->formatDate($date, $this->getFormatSettings()))); + $this->setTimeZone($date); + $summary[] = t('Format: @display', array('@display' => $this->formatDate($date))); return $summary; } diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php index 49d9261..3b3d74e 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php @@ -188,11 +188,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { abstract protected function formatDate($date); /** - * Sets the proper time zone on a DrupalDateTime object for the current user. + * Sets the proper time zone on a DrupalDateTime object. * * A DrupalDateTime object loaded from the database will have the UTC time - * zone applied to it. This method will apply the time zone for the current - * user, based on system and user settings. + * zone applied to it. This method applies the proper timezone based on + * the formatter configuration. * * @see drupal_get_user_timezone() * @@ -207,29 +207,18 @@ protected function setTimeZone(DrupalDateTime $date, $timezone = NULL) { $timezone = DATETIME_STORAGE_TIMEZONE; } elseif (empty($timezone)) { - $timezone = drupal_get_user_timezone(); + $timezone_override = $this->getSetting('timezone_override'); + if ($this->getSetting('timezone_display') === DateTimeItem::TIMEZONE_NONE && $timezone_override) { + $timezone = $timezone_override; + } + else { + $timezone = drupal_get_user_timezone(); + } } $date->setTimeZone(timezone_open($timezone)); } /** - * Gets a settings array suitable for DrupalDateTime::format(). - * - * @return array - * The settings array that can be passed to DrupalDateTime::format(). - */ - protected function getFormatSettings() { - $settings = []; - - $timezone_override = $this->getSetting('timezone_override'); - if ($this->getSetting('timezone_display') === DateTimeItem::TIMEZONE_NONE && $timezone_override) { - $settings['timezone'] = $timezone_override; - } - - return $settings; - } - - /** * Creates a render array from a date object. * * @param \Drupal\Core\Datetime\DrupalDateTime $date diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php index cb6084b..04b007d 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php @@ -41,14 +41,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { */ protected function formatDate($date) { $format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT; - $timezone_override = $this->getSetting('timezone_override'); - if ($this->getSetting('timezone_display') === DateTimeItem::TIMEZONE_NONE && $timezone_override) { - $timezone = $timezone_override; - } - else { - $timezone = $date->getTimezone()->getName(); - } - return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL); + return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $date->getTimezone()->getName()); } }