diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php index e13a4a8ead875b936c1028e4cef1c254ec36e6be..308cbb2d6ab09b6a33348764b2bc9687581cc8fe 100644 --- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php +++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php @@ -568,8 +568,6 @@ public static function datePad($value, $size = 2) { * @param string $format * A format string using either PHP's date(). * @param array $settings - * - langcode: (optional) String two letter language code used to control - * the result of the format(). Defaults to NULL. * - timezone: (optional) String timezone name. Defaults to the timezone * of the date object. * @@ -585,11 +583,18 @@ public function format($format, $settings = array()) { // Format the date and catch errors. try { - $value = $this->dateTimeObject->format($format); + // Clone the date/time object so we can change the time zone without + // disturbing the value stored in the object. + $dateTimeObject = clone $this->dateTimeObject; + if (isset($settings['timezone'])) { + $dateTimeObject->setTimezone(new \DateTimeZone($settings['timezone'])); + } + $value = $dateTimeObject->format($format); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); } + return $value; } } diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php index 977108b61f1dc00d1fd770854bab88da1a0da8a0..98e8fb9a608358abe1c7066237b52034a239dd8a 100644 --- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php @@ -106,7 +106,7 @@ public function format($format, $settings = array()) { $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("\xEF\\\\\\\\\xFF", "\xEF\\\\\$1\$1\xFF"), $format); // Call date_format(). - $format = parent::format($format); + $format = parent::format($format, $settings); // Translates a formatted date string. $translation_callback = function($matches) use ($langcode) { diff --git a/core/modules/system/src/Tests/Datetime/DrupalDateTimeTest.php b/core/modules/system/src/Tests/Datetime/DrupalDateTimeTest.php index 85ad10971ed91bac6de71d18f919f7f9530541e3..f560c32d2b6435ef029e21ce0e004465c1ac45b1 100644 --- a/core/modules/system/src/Tests/Datetime/DrupalDateTimeTest.php +++ b/core/modules/system/src/Tests/Datetime/DrupalDateTimeTest.php @@ -7,8 +7,8 @@ namespace Drupal\system\Tests\Datetime; -use Drupal\simpletest\WebTestBase; use Drupal\Core\Datetime\DrupalDateTime; +use Drupal\simpletest\WebTestBase; use Drupal\user\Entity\User; /** @@ -103,4 +103,22 @@ public function testDateTimezone() { $this->assertTrue($timezone == 'Asia/Manila', 'DrupalDateTime uses the user timezone, if configurable timezones are used and it is set.'); } + /** + * Tests the ability to override the time zone in the format method. + */ + function testTimezoneFormat() { + // Create a date in UTC + $date = DrupalDateTime::createFromTimestamp(87654321, 'UTC'); + + // Verify that the date format method displays the default time zone. + $this->assertEqual($date->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date has default UTC time zone and correct date/time.'); + + // Verify that the format method can override the time zone. + $this->assertEqual($date->format('Y/m/d H:i:s e', array('timezone' => 'America/New_York')), '1972/10/11 08:25:21 America/New_York', 'Date displayed overidden time zone and correct date/time'); + + // Verify that the date format method still displays the default time zone + // for the date object. + $this->assertEqual($date->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date still has default UTC time zone and correct date/time'); + } + }