diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php index 5e81fce..35987b7 100644 --- a/core/lib/Drupal/Core/Datetime/DateFormatter.php +++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php @@ -210,15 +210,13 @@ public function formatInterval($interval, $granularity = 2, $langcode = NULL) { * @see date() */ public function getSampleDateFormats($langcode = NULL, $timestamp = NULL, $timezone = NULL) { - $language_code = $langcode ?: NULL; $timestamp = $timestamp ?: REQUEST_TIME; - $timezone = $timezone ?: NULL; // All date format characters for the PHP date() function. $date_characters = 'dDjlNSwzWFmMntLoYyaABgGhHisueIOPTZcrU'; $date_chars = str_split($date_characters); $date_elements = array_combine($date_chars, $date_chars); - return array_map(function($character) use ($timestamp, $timezone, $language_code) { - return $this->format($timestamp, 'custom', $character, $timezone, $language_code); + return array_map(function($character) use ($timestamp, $timezone, $langcode) { + return $this->format($timestamp, 'custom', $character, $timezone, $langcode); }, $date_elements); } @@ -234,6 +232,9 @@ public function getSampleDateFormats($langcode = NULL, $timestamp = NULL, $timez * The pattern for the date format in the given language. */ protected function dateFormat($format, $langcode) { + if ($format == 'custom') { + return NULL; + } if (!isset($this->dateFormats[$format][$langcode])) { $original_language = $this->languageManager->getConfigOverrideLanguage(); $this->languageManager->setConfigOverrideLanguage(new Language(array('id' => $langcode))); @@ -256,4 +257,13 @@ protected function country() { return $this->country; } + /** + * Setter for country. + * + * @param $country_code + */ + public function setCountry($country_code) { + $this->country = $country_code; + } + } diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php index ac22dab..6d9e88b 100644 --- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php @@ -62,9 +62,9 @@ public function __construct($time = 'now', $timezone = NULL, $settings = array() * knowledge of the preferred user timezone. */ protected function prepareTimezone($timezone) { - $user_timezone = drupal_get_user_timezone(); - if (empty($timezone) && !empty($user_timezone)) { - $timezone = $user_timezone; + if (empty($timezone)) { + // Fallback to user or system default timezone. + $timezone = drupal_get_user_timezone(); } return parent::prepareTimezone($timezone); } @@ -109,4 +109,5 @@ public function format($format, $settings = array()) { } return $value; } + } diff --git a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php index da9ebaa..a3d7e79 100644 --- a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php @@ -5,9 +5,10 @@ * Contains \Drupal\Tests\Core\Datetime\DateTest. */ -namespace Drupal\Tests\Core\Datetime; +namespace Drupal\Tests\Core\Datetime { use Drupal\Core\Datetime\DateFormatter; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; /** @@ -45,6 +46,7 @@ class DateTest extends UnitTestCase { protected $dateFormatter; protected function setUp() { + parent::setUp(); $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface'); @@ -128,4 +130,42 @@ public function testFormatIntervalZeroSecond() { $this->assertEquals('0 sec', $result); } + /** + * Tests the getSampleDateFormats method. + * + * @see \Drupal\Core\Datetime\DateFormatter::getSampleDateFormats() + */ + public function testGetSampleDateFormats() { + include_once $this->root . '/core/includes/common.inc'; + $container = new ContainerBuilder(); + $container->set('string_translation', $this->getStringTranslationStub()); + \Drupal::setContainer($container); + + $ts = strtotime('2015-03-22 14:23:00'); + $this->dateFormatter->setCountry('GB'); + $expected = $this->dateFormatter->getSampleDateFormats('en', $ts, 'Europe/London'); + + // Removed characters related to timezone 'e' and 'T', as test + // does not have timezone set. + $date_characters = 'dDjlNSwzWFmMntLoYyaABgGhHisuIOPZcrU'; + $date_chars = str_split($date_characters); + + foreach ($date_chars as $val) { + $this->assertEquals($expected[$val], date($val, $ts)); + } + + } + +} + +} + +namespace { + use Drupal\Component\Utility\String; + + if (!function_exists('t')) { + function t($string, array $args = []) { + return String::format($string, $args); + } + } }