Commit 5694189a authored by alexpott's avatar alexpott

Issue #2739290 by jhedstrom, mpdonadio: UTC+12 is broken on date only fields

parent fca71fa1
......@@ -86,7 +86,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
*/
protected function formatDate($date) {
$format_type = $this->getSetting('format_type');
$timezone = $this->getSetting('timezone_override');
$timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();
return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
}
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -140,7 +141,14 @@ public function settingsSummary() {
* A DrupalDateTime object.
*/
protected function setTimeZone(DrupalDateTime $date) {
$date->setTimeZone(timezone_open(drupal_get_user_timezone()));
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time has no timezone conversion.
$timezone = DATETIME_STORAGE_TIMEZONE;
}
else {
$timezone = drupal_get_user_timezone();
}
$date->setTimeZone(timezone_open($timezone));
}
/**
......
......@@ -3,6 +3,7 @@
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
/**
* Plugin implementation of the 'Plain' formatter for 'datetime' fields.
......@@ -33,8 +34,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
// A date without time will pick up the current time, use the default.
datetime_date_default_time($date);
}
else {
}
$this->setTimeZone($date);
$output = $this->formatDate($date);
......@@ -56,7 +55,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
* {@inheritdoc}
*/
protected function formatDate($date) {
$format = $this->getFieldSetting('datetime_type') == 'date' ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
$format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
$timezone = $this->getSetting('timezone_override');
return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
}
......
......@@ -34,6 +34,12 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#required' => $element['#required'],
);
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date-only field should have no timezone conversion performed, so
// use the same timezone as for storage.
$element['value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE;
}
if ($items[$delta]->date) {
$date = $items[$delta]->date;
// The date was created and verified during field_load(), so it is safe to
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment