diff --cc core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php index fe5fa75,31bd1d1..0000000 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php @@@ -42,8 -40,22 +42,8 @@@ class DateTimeCustomFormatter extends D /** @var \Drupal\Core\Datetime\DrupalDateTime $date */ $date = $item->date; - $elements[$delta] = $this->buildDate($date); - if ($this->getFieldSetting('datetime_type') == 'date') { - // A date without time will pick up the current time, use the default. - datetime_date_default_time($date); - } - $this->setTimeZone($date, $item->timezone); - - $output = $this->formatDate($date); ++ $elements[$delta] = $this->buildDate($date, $item->timezone); } - $elements[$delta] = [ - '#markup' => $output, - '#cache' => [ - 'contexts' => [ - 'timezone', - ], - ], - ]; } return $elements; diff --cc core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php index 8f703fb,b330179..0000000 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php diff --cc core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php index 5bb6766,6b80133..0000000 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php @@@ -29,8 -30,22 +29,8 @@@ class DateTimePlainFormatter extends Da /** @var \Drupal\Core\Datetime\DrupalDateTime $date */ $date = $item->date; - $elements[$delta] = $this->buildDate($date); - if ($this->getFieldSetting('datetime_type') == 'date') { - // A date without time will pick up the current time, use the default. - datetime_date_default_time($date); - } - $this->setTimeZone($date, $item->timezone); - - $output = $this->formatDate($date); ++ $elements[$delta] = $this->buildDate($date, $item->timezone); } - $elements[$delta] = [ - '#cache' => [ - 'contexts' => [ - 'timezone', - ], - ], - '#markup' => $output, - ]; } return $elements; diff --cc core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php index 424cbd9,e452e6c..0000000 --- a/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php +++ b/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php @@@ -46,11 -31,14 +46,14 @@@ class DateTimeItemTest extends FieldKer 'field_name' => 'field_datetime', 'type' => 'datetime', 'entity_type' => 'entity_test', - 'settings' => array('datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME), + 'settings' => array( - 'datetime_type' => 'date', ++ 'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME, + 'timezone_handling' => 'site', + ), )); - $field_storage->save(); - $field = FieldConfig::create([ - 'field_storage' => $field_storage, + $this->fieldStorage->save(); + $this->field = FieldConfig::create([ + 'field_storage' => $this->fieldStorage, 'bundle' => 'entity_test', 'settings' => array( 'default_value' => 'blank', diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php index 996d071..3d8684c 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php @@ -160,7 +160,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if ($item->date) { /** @var \Drupal\Core\Datetime\DrupalDateTime $date */ $date = $item->date; - $elements[$delta] = $this->buildDateWithIsoAttribute($date); + $elements[$delta] = $this->buildDateWithIsoAttribute($date, $item->timezone); if (!empty($item->_attributes)) { $elements[$delta]['#attributes'] += $item->_attributes; @@ -231,16 +231,18 @@ protected function getFormatSettings() { * * @param \Drupal\Core\Datetime\DrupalDateTime $date * A date object. + * @param string $timezone + * (optional) A timezone to explicitly set the date to. * * @return array * A render array. */ - protected function buildDate(DrupalDateTime $date) { + protected function buildDate(DrupalDateTime $date, $timezone = NULL) { if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) { // A date without time will pick up the current time, use the default. datetime_date_default_time($date); } - $this->setTimeZone($date); + $this->setTimeZone($date, $timezone); $build = [ '#markup' => $this->formatDate($date), @@ -259,11 +261,13 @@ protected function buildDate(DrupalDateTime $date) { * * @param \Drupal\Core\Datetime\DrupalDateTime $date * A date object. + * @param string $timezone + * (optional) A timezone to explicitly set the date to. * * @return array * A render array. */ - protected function buildDateWithIsoAttribute(DrupalDateTime $date) { + protected function buildDateWithIsoAttribute(DrupalDateTime $date, $timezone = NULL) { if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) { // A date without time will pick up the current time, use the default. datetime_date_default_time($date); @@ -272,7 +276,7 @@ protected function buildDateWithIsoAttribute(DrupalDateTime $date) { // Create the ISO date in Universal Time. $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z'; - $this->setTimeZone($date); + $this->setTimeZone($date, $timezone); $build = [ '#theme' => 'time',