Problem/Motivation

DateTimeWidgetBase sets timezone twice to the same value/creates unnecessary objects/minor bloat.

...
    if ($items[$delta]->date) {
      $date = $items[$delta]->date;
      // The date was created and verified during field_load(), so it is safe to
      // use without further inspection.
      $date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
      $element['value']['#default_value'] = $this->createDefaultValue($date, $element['value']['#date_timezone']);
    }
...
  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;
  }

Proposed resolution

Clean up duplication

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

None

Issue fork drupal-3251100

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

NickDickinsonWilde created an issue. See original summary.

nickdickinsonwilde’s picture

Status: Needs work » Needs review
larowlan’s picture

Category: Bug report » Task
Status: Needs review » Reviewed & tested by the community
Issue tags: +Bug Smash Initiative

Looks reasonable to me (disclaimer reviewed this on my phone)

This sort of cleanup is normally a task rather than a bug

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 4e34cd3fd0a to 10.0.x and bbc518ad835 to 9.4.x. Thanks!

  • alexpott committed 4e34cd3 on 10.0.x
    Issue #3251100 by NickDickinsonWilde: DateTimeWidgetBase sets Timezone...

  • alexpott committed bbc518a on 9.4.x
    Issue #3251100 by NickDickinsonWilde: DateTimeWidgetBase sets Timezone...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.