diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index f25140f..cc19c39 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -14,8 +14,11 @@ /** * Defines the format that date and time should be stored in. + * + * This is equivalent to 'c' for output, but also compatible with + * \DateTime::createFromFormat() for parsing purposes. */ -const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s'; +const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:sP'; /** * Defines the format that dates should be stored in. diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php index 3fac252..99ccea4 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php @@ -48,7 +48,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } // 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", array('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 f07d564..1de9601 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php @@ -68,7 +68,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'value' => array( 'description' => 'The date value.', 'type' => 'varchar', - 'length' => 20, + 'length' => 25, ), ), 'indexes' => array( diff --git a/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php b/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php index bba2c33..7f015fd 100644 --- a/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php +++ b/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\datetime\Kernel; +use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\entity_test\Entity\EntityTest; @@ -50,7 +51,7 @@ protected function setUp() { public function testDateTimeItem() { // Verify entity creation. $entity = EntityTest::create(); - $value = '2014-01-01T20:00:00Z'; + $value = '2014-01-01T20:00:00+00:00'; $entity->field_datetime = $value; $entity->name->value = $this->randomMachineName(); $entity->save(); @@ -85,7 +86,7 @@ public function testDateTimeItem() { public function testSetValue() { // Test DateTimeItem::setValue() using string. $entity = EntityTest::create(); - $value = '2014-01-01T20:00:00Z'; + $value = '2014-01-01T20:00:00+00:00'; $entity->get('field_datetime')->set(0, $value); $entity->save(); // Load the entity and ensure the field was saved correctly. @@ -110,7 +111,7 @@ public function testSetValue() { public function testSetValueProperty() { // Test Date::setValue(). $entity = EntityTest::create(); - $value = '2014-01-01T20:00:00Z'; + $value = '2014-01-01T20:00:00+00:00'; $entity->set('field_datetime', $value); $entity->save(); @@ -120,4 +121,22 @@ public function testSetValueProperty() { $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.'); } + /** + * Tests setting the value with setDateTime() + */ + public function testSetDateTime() { + $entity = EntityTest::create(); + $value = '2014-01-01T20:00:00+00:00'; + // 'c' isn't a valid format string per http://php.net/manual/en/datetime.createfromformat.php + // so build the format string from the parts. + $datetime = DrupalDateTime::createFromFormat('Y-m-d\TH:i:sP', $value); + $date_item = $entity->get('field_datetime')->appendItem(); + $date_item->get('value')->setDateTime($datetime); + $entity->save(); + + $id = $entity->id(); + $entity = entity_load('entity_test', $id); + $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set from DrupalDateTime object.'); + } + } diff --git a/core/modules/rdf/tests/src/Kernel/Field/DateTimeFieldRdfaTest.php b/core/modules/rdf/tests/src/Kernel/Field/DateTimeFieldRdfaTest.php index 012b57d..31cd298 100644 --- a/core/modules/rdf/tests/src/Kernel/Field/DateTimeFieldRdfaTest.php +++ b/core/modules/rdf/tests/src/Kernel/Field/DateTimeFieldRdfaTest.php @@ -21,7 +21,7 @@ class DateTimeFieldRdfaTest extends FieldRdfaTestBase { * * @var string */ - protected $testValue = '2014-01-28T06:01:01'; + protected $testValue = '2014-01-28T06:01:01+00:00'; /** * {@inheritdoc} @@ -48,6 +48,6 @@ protected function setUp() { * Tests the default formatter. */ public function testDefaultFormatter() { - $this->assertFormatterRdfa(array('type' => 'datetime_default'), 'http://schema.org/dateCreated', array('value' => $this->testValue . 'Z', 'type' => 'literal', 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime')); + $this->assertFormatterRdfa(array('type' => 'datetime_default'), 'http://schema.org/dateCreated', array('value' => $this->testValue, 'type' => 'literal', 'datatype' => 'http://www.w3.org/2001/XMLSchema#dateTime')); } }