diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index 245d0c4..a925706 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -27,7 +27,7 @@ * * @see https://www.drupal.org/node/2912980 */ -const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s'; +const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s+00:00'; /** * Defines the format that dates should be stored in. diff --git a/core/modules/datetime/src/DateTimeComputed.php b/core/modules/datetime/src/DateTimeComputed.php index 16f664b..f0ba5f1 100644 --- a/core/modules/datetime/src/DateTimeComputed.php +++ b/core/modules/datetime/src/DateTimeComputed.php @@ -3,11 +3,11 @@ namespace Drupal\datetime; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; +use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TypedData; -use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; /** * A computed property for dates of date time field items. @@ -47,7 +47,16 @@ public function getValue() { $value = $item->{($this->definition->getSetting('date source'))}; $datetime_type = $item->getFieldDefinition()->getSetting('datetime_type'); - $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT; + if ($item->getFieldDefinition()->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) { + $storage_format = DateTimeItemInterface::DATE_STORAGE_FORMAT; + } + else { + // Strip off an explicit time zone offset from the format and replace + // with the time zone offset placeholder to allow the string to be parsed + // properly. + $storage_format = preg_replace('/\\\+\d\d\:\d\d$/', 'P', DateTimeItemInterface::DATETIME_STORAGE_FORMAT); + } + try { $date = DrupalDateTime::createFromFormat($storage_format, $value, DateTimeItemInterface::STORAGE_TIMEZONE); if ($date instanceof DrupalDateTime && !$date->hasErrors()) { diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php index a6680f6..1d41688 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php @@ -227,7 +227,7 @@ protected function buildDate(DrupalDateTime $date) { */ protected function buildDateWithIsoAttribute(DrupalDateTime $date) { // Create the ISO date in Universal Time. - $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z'; + $iso_date = $date->format("Y-m-d\TH:i:sP", ['timezone' => 'UTC']); $this->setTimeZone($date); diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php index 3264069..6528192 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php @@ -69,7 +69,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'value' => [ 'description' => 'The date value.', 'type' => 'varchar', - 'length' => 20, + 'length' => 25, ], ], 'indexes' => [ diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItemInterface.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItemInterface.php index a9231c6..ae0343e 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItemInterface.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItemInterface.php @@ -15,7 +15,7 @@ /** * Defines the format that date and time should be stored in. */ - const DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s'; + const DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s\+00:00'; /** * Defines the format that dates should be stored in. diff --git a/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php b/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php index 75f892a..dc145b1 100644 --- a/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php +++ b/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php @@ -69,7 +69,7 @@ public function testDateTime() { // Verify entity creation. $entity = EntityTest::create(); - $value = '2014-01-01T20:00:00'; + $value = '2014-01-01T20:00:00+00:00'; $entity->field_datetime = $value; $entity->name->value = $this->randomMachineName(); $this->entityValidateAndSave($entity); @@ -84,7 +84,7 @@ public function testDateTime() { $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName()); // Verify changing the date value. - $new_value = '2016-11-04T00:21:00'; + $new_value = '2016-11-04T00:21:00+00:00'; $entity->field_datetime->value = $new_value; $this->assertEqual($entity->field_datetime->value, $new_value); $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName()); @@ -164,7 +164,7 @@ public function testSetValue() { // Test DateTimeItem::setValue() using string. $entity = EntityTest::create(); - $value = '2014-01-01T20:00:00'; + $value = '2014-01-01T20:00:00+00:00'; $entity->get('field_datetime')->set(0, $value); $this->entityValidateAndSave($entity); // Load the entity and ensure the field was saved correctly. @@ -175,7 +175,7 @@ public function testSetValue() { // Test DateTimeItem::setValue() using property array. $entity = EntityTest::create(); - $value = '2014-01-01T20:00:00'; + $value = '2014-01-01T20:00:00+00:00'; $entity->set('field_datetime', $value); $this->entityValidateAndSave($entity); // Load the entity and ensure the field was saved correctly. @@ -220,7 +220,7 @@ public function testSetValueProperty() { $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME); $this->fieldStorage->save(); $entity = EntityTest::create(); - $value = '2014-01-01T20:00:00'; + $value = '2014-01-01T20:00:00+00:00'; $entity->set('field_datetime', $value); $this->entityValidateAndSave($entity); @@ -269,10 +269,10 @@ public function datetimeValidationProvider() { return [ // Valid ISO 8601 dates, but unsupported by DateTimeItem. ['2014-01-01T20:00:00Z'], - ['2014-01-01T20:00:00+04:00'], ['2014-01-01T20:00:00+0400'], ['2014-01-01T20:00:00+04'], ['2014-01-01T20:00:00.123'], + ['2014-01-01T20:00:00'], ['2014-01-01T200000'], ['2014-01-01T2000'], ['2014-01-01T20'],