diff -u b/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php b/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php --- b/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php +++ b/core/modules/migrate/src/Plugin/migrate/process/FormatDate.php @@ -25,6 +25,18 @@ * DateTimePlus::__construct(). * - settings: keyed array of settings, see DateTimePlus::__construct(). * + * Configuration keys from_timezone and to_timezone are both optional. Possible + * input variants: + * - Both from_timezone and to_timezone are empty. Date will not be converted + * and be treated as date in default timezone. + * - Only from_timezone is set. Date will be converted from timezone specified + * in from_timezone key to the default timezone. + * - Only to_timezone is set. Date will be converted from the default timezone + * to the timezone specified in to_timezone key. + * - Both from_timezone and to_timezone are set. Date will be converted from + * timezone specified in from_timezone key to the timezone specified in + * to_timezone key. + * * Examples: * * Example usage for date only fields (DATETIME_DATE_STORAGE_FORMAT): @@ -100,19 +112,22 @@ $toFormat = $this->configuration['to_format']; if (isset($this->configuration['timezone'])) { @trigger_error('Configuration key "timezone" is deprecated in 8.4.0 and will be removed before Drupal 9.0.0, use "from_timezone" and "to_timezone" instead. See https://www.drupal.org/node/2885746', E_USER_DEPRECATED); + $from_timezone = $this->configuration['timezone']; + $to_timezone = NULL; + } + else { + $system_timezone = date_default_timezone_get(); + $default_timezone = !empty($system_timezone) ? $system_timezone : 'UTC'; + $from_timezone = isset($this->configuration['from_timezone']) ? $this->configuration['from_timezone'] : $default_timezone; + $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : $default_timezone; } - $timezone = isset($this->configuration['timezone']) ? $this->configuration['timezone'] : NULL; - $from_timezone = isset($this->configuration['from_timezone']) ? $this->configuration['from_timezone'] : $timezone; - $to_timezone = isset($this->configuration['to_timezone']) ? $this->configuration['to_timezone'] : NULL; $settings = isset($this->configuration['settings']) ? $this->configuration['settings'] : []; // Attempts to transform the supplied date using the defined input format. // DateTimePlus::createFromFormat can throw exceptions, so we need to // explicitly check for problems. try { - $date = DateTimePlus::createFromFormat($fromFormat, $value, $from_timezone, $settings); - //$date->setTimezone(new \DateTimeZone($from_timezone)); - $transformed = $date->format($toFormat, ['timezone' => $to_timezone]); + $transformed = DateTimePlus::createFromFormat($fromFormat, $value, $from_timezone, $settings)->format($toFormat, ['timezone' => $to_timezone]); } catch (\InvalidArgumentException $e) { throw new MigrateException(sprintf('Format date plugin could not transform "%s" using the format "%s". Error: %s', $value, $fromFormat, $e->getMessage()), $e->getCode(), $e); diff -u b/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php b/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php --- b/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php +++ b/core/modules/migrate/tests/src/Unit/process/FormatDateTest.php @@ -84,7 +84,7 @@ $this->plugin = new FormatDate($configuration, 'test_format_date', []); $actual = $this->plugin->transform('2004-12-19T10:19:42-0600', $this->migrateExecutable, $this->row, 'field_date'); - $this->assertEquals('2004-12-19T10:19:42-06:00 America/Managua', $actual); + $this->assertEquals('2004-12-19T10:19:42-06:00 -06:00', $actual); } /** @@ -127,10 +127,10 @@ 'datetime_datetime' => [ 'configuration' => [ 'from_format' => 'm/d/Y H:i:s', - 'to_format' => 'Y-m-d\TH:i:s', + 'to_format' => 'Y-m-d\TH:i:s e', ], 'value' => '01/05/1955 10:43:22', - 'expected' => '1955-01-05T10:43:22', + 'expected' => '1955-01-05T10:43:22 Australia/Sydney', ], 'empty_values' => [ 'configuration' => [ @@ -143,30 +143,34 @@ 'timezone_from_to' => [ 'configuration' => [ 'from_format' => 'Y-m-d H:i:s', - 'to_format' => 'Y-m-d H:i:s', + 'to_format' => 'Y-m-d H:i:s e', 'from_timezone' => 'America/Managua', 'to_timezone' => 'UTC', ], 'value' => '2004-12-19 10:19:42', - 'expected' => '2004-12-19 16:19:42', + 'expected' => '2004-12-19 16:19:42 UTC', ], 'timezone_from' => [ 'configuration' => [ 'from_format' => 'Y-m-d h:i:s', - 'to_format' => 'Y-m-d h:i:s', + 'to_format' => 'Y-m-d h:i:s e', 'from_timezone' => 'America/Managua', ], 'value' => '2004-11-19 10:25:33', - 'expected' => '2004-11-19 10:25:33', + // Unit tests use Australia/Sydney timezone, so date value will be + // converted from America/Managua to Australia/Sydney timezone. + 'expected' => '2004-11-20 03:25:33 Australia/Sydney', ], 'timezone_to' => [ 'configuration' => [ 'from_format' => 'Y-m-d H:i:s', - 'to_format' => 'Y-m-d H:i:s', + 'to_format' => 'Y-m-d H:i:s e', 'to_timezone' => 'America/Managua', ], 'value' => '2004-12-19 10:19:42', - 'expected' => '2004-12-19 04:19:42', + // Unit tests use Australia/Sydney timezone, so date value will be + // converted from Australia/Sydney to America/Managua timezone. + 'expected' => '2004-12-18 17:19:42 America/Managua', ], ]; }