Commit a79508ac authored by alexpott's avatar alexpott

Issue #2497447 by mpdonadio, stefan.r, jhodgdon: DrupalDateTime::format() and...

Issue #2497447 by mpdonadio, stefan.r, jhodgdon: DrupalDateTime::format() and DateTimePlus::format() ignore the timezone setting
parent f3e2e681
......@@ -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;
}
}
......@@ -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) {
......
......@@ -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');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment