Issue #2830094 by mpdonadio, Yogesh Pawar, arunkumark, jhedstrom, larowlan, alexpott: Deprecate and remove usages of datetime_date_default_time()

8.5.x
Nathaniel Catchpole 2017-10-05 14:34:14 +01:00
parent f1aa6b3e1c
commit 50ab19584f
15 changed files with 126 additions and 63 deletions

View File

@ -658,4 +658,15 @@ class DateTimePlus {
return $value;
}
/**
* Sets the default time for an object built from date-only data.
*
* The default time for a date without time can be anything, so long as it is
* consistently applied. If we use noon, dates in most timezones will have the
* same value for in both the local timezone and UTC.
*/
public function setDefaultDateTime() {
$this->dateTimeObject->setTime(12, 0, 0);
}
}

View File

@ -50,7 +50,20 @@ function datetime_help($route_name, RouteMatchInterface $route_match) {
* same value for in both the local timezone and UTC.
*
* @param $date
*
* @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use
* \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime() or
* \Drupal\Core\Datetime\DrupalDateTime::setDefaultDateTime() instead.
*
* @see https://www.drupal.org/node/2880931
*/
function datetime_date_default_time($date) {
@trigger_error('datetime_date_default_time() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime() or \Drupal\Core\Datetime\DrupalDateTime::setDefaultDateTime() instead. See https://www.drupal.org/node/2880931.', E_USER_DEPRECATED);
// For maximum BC before this method is removed, we do not use the
// recommendation from the deprecation method. Instead, we call the setTime()
// method directly. This allows the method to continue to work with
// \DateTime, DateTimePlus, and DrupalDateTime objects (and classes that
// may derive from them).
$date->setTime(12, 0, 0);
}

View File

@ -56,12 +56,10 @@ class DateTimeComputed extends TypedData {
// set the time to 12:00:00 UTC for date-only fields. This is used so
// that the local date portion is the same, across nearly all time
// zones.
// @see datetime_date_default_time()
// @see \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime()
// @see http://php.net/manual/en/datetime.createfromformat.php
// @todo Update comment and/or code per the chosen solution in
// https://www.drupal.org/node/2830094
if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
$this->date->setTime(12, 0, 0);
$this->date->setDefaultDateTime();
}
}
}

View File

@ -202,10 +202,6 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
* A render array.
*/
protected function buildDate(DrupalDateTime $date) {
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);
$build = [
@ -230,11 +226,6 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
* A render array.
*/
protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
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);
}
// Create the ISO date in Universal Time.
$iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';

View File

@ -110,10 +110,6 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
$date = $item->date;
$output = [];
if (!empty($item->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);
}
$output = $this->formatDate($date);
}
$elements[$delta] = $output;

View File

@ -35,13 +35,8 @@ class DateTimeWidgetBase extends WidgetBase {
$date = $items[$delta]->date;
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time will pick up the current time, use the default
// time.
datetime_date_default_time($date);
}
$date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
$element['value']['#default_value'] = $date;
$element['value']['#default_value'] = $this->createDefaultValue($date, $element['value']['#date_timezone']);
}
return $element;
@ -59,9 +54,6 @@ class DateTimeWidgetBase extends WidgetBase {
$date = $item['value'];
switch ($this->getFieldSetting('datetime_type')) {
case DateTimeItem::DATETIME_TYPE_DATE:
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
datetime_date_default_time($date);
$format = DATETIME_DATE_STORAGE_FORMAT;
break;
@ -77,4 +69,28 @@ class DateTimeWidgetBase extends WidgetBase {
return $values;
}
/**
* Creates a date object for use as a default value.
*
* This will take a default value, apply the proper timezone for display in
* a widget, and set the default time for date-only fields.
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* The UTC default date.
* @param string $timezone
* The timezone to apply.
*
* @return \Drupal\Core\Datetime\DrupalDateTime
* A date object for use as a default value in a field widget.
*/
protected function createDefaultValue($date, $timezone) {
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
$date->setDefaultDateTime();
}
$date->setTimezone(new \DateTimeZone($timezone));
return $date;
}
}

View File

@ -185,4 +185,20 @@ abstract class DateTestBase extends WebTestBase {
->save();
}
/**
* Massages test date values.
*
* If a date object is generated directly by a test, then it needs to be
* adjusted to behave like the computed date from the item.
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* A date object directly generated by the test.
*/
protected function massageTestDate($date) {
if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
// Set the default time for date-only items.
$date->setDefaultDateTime();
}
}
}

View File

@ -183,4 +183,20 @@ abstract class DateTestBase extends BrowserTestBase {
->save();
}
/**
* Massages test date values.
*
* If a date object is generated directly by a test, then it needs to be
* adjusted to behave like the computed date from the item.
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* A date object directly generated by the test.
*/
protected function massageTestDate($date) {
if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
// Set the default time for date-only items.
$date->setDefaultDateTime();
}
}
}

View File

@ -100,7 +100,7 @@ class DateTimeFieldTest extends DateTestBase {
// Formats that display a time component for date-only fields will display
// the default time, so that is applied before calculating the expected
// value.
datetime_date_default_time($date);
$this->massageTestDate($date);
foreach ($options as $setting => $values) {
foreach ($values as $new_value) {
// Update the entity display settings.

View File

@ -122,22 +122,34 @@ class DateTimeItemTest extends FieldKernelTestBase {
$this->assertEqual($entity->field_datetime->value, $value);
$this->assertEqual($entity->field_datetime[0]->value, $value);
$this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Verify changing the date value.
$new_value = '2016-11-04';
$entity->field_datetime->value = $new_value;
$this->assertEqual($entity->field_datetime->value, $new_value);
$this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Read changed entity and assert changed values.
$this->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime->value, $new_value);
$this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->field_datetime->generateSampleItems();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$this->entityValidateAndSave($entity);
}

View File

@ -5,7 +5,6 @@ namespace Drupal\datetime_range\Plugin\Field\FieldWidget;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase;
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
@ -58,9 +57,6 @@ class DateRangeWidgetBase extends DateTimeWidgetBase {
$start_date = $item['value'];
switch ($this->getFieldSetting('datetime_type')) {
case DateRangeItem::DATETIME_TYPE_DATE:
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
datetime_date_default_time($start_date);
$format = DATETIME_DATE_STORAGE_FORMAT;
break;
@ -88,9 +84,6 @@ class DateRangeWidgetBase extends DateTimeWidgetBase {
$end_date = $item['end_value'];
switch ($this->getFieldSetting('datetime_type')) {
case DateRangeItem::DATETIME_TYPE_DATE:
// If this is a date-only field, set it to the default time so the
// timezone conversion can be reversed.
datetime_date_default_time($end_date);
$format = DATETIME_DATE_STORAGE_FORMAT;
break;
@ -142,30 +135,4 @@ class DateRangeWidgetBase extends DateTimeWidgetBase {
}
}
/**
* Creates a date object for use as a default value.
*
* This will take a default value, apply the proper timezone for display in
* a widget, and set the default time for date-only fields.
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* The UTC default date.
* @param string $timezone
* The timezone to apply.
*
* @return \Drupal\Core\Datetime\DrupalDateTime
* A date object for use as a default value in a field widget.
*/
protected function createDefaultValue($date, $timezone) {
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time will pick up the current time, use the default
// time.
datetime_date_default_time($date);
}
$date->setTimezone(new \DateTimeZone($timezone));
return $date;
}
}

View File

@ -110,8 +110,8 @@ class DateRangeFieldTest extends DateTestBase {
// Formats that display a time component for date-only fields will display
// the default time, so that is applied before calculating the expected
// value.
datetime_date_default_time($start_date);
datetime_date_default_time($end_date);
$this->massageTestDate($start_date);
$this->massageTestDate($end_date);
// Reset display options since these get changed below.
$this->displayOptions = [
@ -211,7 +211,7 @@ class DateRangeFieldTest extends DateTestBase {
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
datetime_date_default_time($start_date);
$this->massageTestDate($start_date);
$this->displayOptions = [
'type' => 'daterange_default',

View File

@ -100,6 +100,8 @@ class DateRangeItemTest extends FieldKernelTestBase {
sleep(1);
$end_date = $entity->{$field_name}->end_date;
$this->assertEquals($start_date->getTimestamp(), $end_date->getTimestamp());
$this->assertEquals('12:00:00', $start_date->format('H:i:s'));
$this->assertEquals('12:00:00', $end_date->format('H:i:s'));
}
}

View File

@ -804,4 +804,17 @@ class DateTimePlusTest extends TestCase {
$date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => TRUE]);
}
/**
* Tests setting the default time for date-only objects.
*/
public function testDefaultDateTime() {
$utc = new \DateTimeZone('UTC');
$date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '2017-05-23 22:58:00', $utc);
$this->assertEquals('22:58:00', $date->format('H:i:s'));
$date->setDefaultDateTime();
$this->assertEquals('12:00:00', $date->format('H:i:s'));
}
}

View File

@ -156,4 +156,16 @@ class DrupalDateTimeTest extends UnitTestCase {
];
}
/**
* Tests setting the default time for date-only objects.
*/
public function testDefaultDateTime() {
$utc = new \DateTimeZone('UTC');
$date = DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2017-05-23 22:58:00', $utc, ['langcode' => 'en']);
$this->assertEquals('22:58:00', $date->format('H:i:s'));
$date->setDefaultDateTime();
$this->assertEquals('12:00:00', $date->format('H:i:s'));
}
}