diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php index 0158eac..dfec5a1 100644 --- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php +++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php @@ -246,11 +246,12 @@ public static function createFromFormat($format, $time, $timezone = NULL, $setti /** * Constructs a date object set to a requested date and timezone. * - * @param string $time - * (optional) A date/time string. Defaults to 'now'. + * @param mixed $time + * (optional) A \DateTime object or date/time string. Defaults to 'now'. * @param mixed $timezone * (optional) \DateTimeZone object, time zone string or NULL. NULL uses the - * default system time zone. Defaults to NULL. + * default system time zone. Defaults to NULL. This parameter is ignored + * if $time is a \DateTime object. * @param array $settings * (optional) Keyed array of settings. Defaults to empty array. * - langcode: (optional) String two letter language code used to control @@ -278,6 +279,15 @@ public function __construct($time = 'now', $timezone = NULL, $settings = []) { if (empty($this->errors)) { $this->dateTimeObject = new \DateTime($prepared_time, $prepared_timezone); + + // If $time is a DateTimePlus object, then its time zone is used + // and $timezone is ignored. If $prepared_timezone isn't NULL, then + // set the time zone to handle cases where \DateTime::__construct() + // ignored this parameter. + // @see http://php.net/manual/en/datetime.construct.php + if (!($time instanceof DateTimePlus) && $prepared_timezone !== NULL) { + $this->dateTimeObject->setTimezone($prepared_timezone); + } } } catch (\Exception $e) { diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php index e50e4c4..3b376db 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @@ -264,7 +264,7 @@ public function testInvalidDates($input, $timezone, $format, $message, $class) { public function testDateTimezone($input, $timezone, $expected_timezone, $message) { $date = new DateTimePlus($input, $timezone); $timezone = $date->getTimezone()->getName(); - $this->assertEquals($timezone, $expected_timezone, $message); + $this->assertEquals($expected_timezone, $timezone, $message); } /** @@ -470,6 +470,10 @@ public function providerTestDateTimezone() { [$date_string, new \DateTimeZone('Australia/Canberra'), 'Australia/Canberra', 'DateTimePlus uses the specified timezone if provided.'], // Create a date object with another date object. [new DateTimePlus('now', 'Pacific/Midway'), NULL, 'Pacific/Midway', 'DateTimePlus uses the specified timezone if provided.'], + // Create a date object from a string with an explicit time zone offset, and a specified time zone. + ['2007-01-31T21:00:00-05:00', 'America/New_York', 'America/New_York', 'DateTimePlus uses the specified timezone if provided.'], + // Create a date object from a string with an explicit time zone offset, and an specified time zone. + ['2007-01-31T21:00:00-05:00', NULL, $system_timezone, 'DateTimePlus uses the system timezone when there is no site timezone.'], ]; }