/** * Overrides format(). * * @param string $format * A format string using either PHP's date(). * @param array $settings * - timezone: (optional) String timezone name. Defaults to the timezone * of the date object. * - langcode: (optional) String two letter language code used to control * the result of the format() method. Defaults to NULL. * * @return string * The formatted value of the date. */ public function format($format, $settings = array()) { $settings['langcode'] = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; // Format the date and catch errors. try { // Encode markers that should be translated. 'A' becomes // '\xEF\AA\xFF'. xEF and xFF are invalid UTF-8 sequences, // and we assume they are not in the input string. // Paired backslashes are isolated to prevent errors in // read-ahead evaluation. The read-ahead expression ensures that // A matches, but not \A. $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("ï\\\\\\\\ÿ", "ï\\\\\$1\$1ÿ"), $format); // Call date_format(). $format = parent::format($format); // Pass the langcode to _format_date_callback(). _format_date_callback(NULL, $settings['langcode']); // Translate the marked sequences. $value = preg_replace_callback('/\\xEF([AaeDlMTF]?)(.*?)\\xFF/', '_format_date_callback', $format); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); } return $value; }
/** * Assertion helper for testTimestamp and testDateTimestamp since they need * different dataProviders. * * @param DateTimePlus $date * DateTimePlus to test. * @input mixed $input * The original input passed to the test method. * @param array $initial * @see testTimestamp() * @param array $transform * @see testTimestamp() */ public function assertDateTimestamp($date, $input, $initial, $transform) { // Check format. $value = $date->format($initial['format']); $this->assertEquals($initial['expected_date'], $value, sprintf("Test new DateTimePlus(%s, %s): should be %s, found %s.", $input, $initial['timezone'], $initial['expected_date'], $value)); // Check timezone name. $value = $date->getTimeZone()->getName(); $this->assertEquals($initial['expected_timezone'], $value, sprintf("The current timezone is %s: should be %s.", $value, $initial['expected_timezone'])); // Check offset. $value = $date->getOffset(); $this->assertEquals($initial['expected_offset'], $value, sprintf("The current offset is %s: should be %s.", $value, $initial['expected_offset'])); // Transform the date to another timezone. $date->setTimezone(new \DateTimeZone($transform['timezone'])); // Check transformed format. $value = $date->format($transform['format']); $this->assertEquals($transform['expected_date'], $value, sprintf("Test \$date->setTimezone(new \\DateTimeZone(%s)): should be %s, found %s.", $transform['timezone'], $transform['expected_date'], $value)); // Check transformed timezone. $value = $date->getTimeZone()->getName(); $this->assertEquals($transform['expected_timezone'], $value, sprintf("The current timezone should be %s, found %s.", $transform['expected_timezone'], $value)); // Check transformed offset. $value = $date->getOffset(); $this->assertEquals($transform['expected_offset'], $value, sprintf("The current offset should be %s, found %s.", $transform['expected_offset'], $value)); }
/** * Overrides format(). * * @param string $format * A format string using either PHP's date(). * @param array $settings * - timezone: (optional) String timezone name. Defaults to the timezone * of the date object. * - langcode: (optional) String two letter language code used to control * the result of the format() method. Defaults to NULL. * * @return string * The formatted value of the date. */ public function format($format, $settings = array()) { $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; $value = ''; // Format the date and catch errors. try { // Encode markers that should be translated. 'A' becomes // '\xEF\AA\xFF'. xEF and xFF are invalid UTF-8 sequences, // and we assume they are not in the input string. // Paired backslashes are isolated to prevent errors in // read-ahead evaluation. The read-ahead expression ensures that // A matches, but not \A. $format = preg_replace(array('/\\\\\\\\/', '/(?<!\\\\)([AaeDlMTF])/'), array("ï\\\\\\\\ÿ", "ï\\\\\$1\$1ÿ"), $format); // Call date_format(). $format = parent::format($format, $settings); // Translates a formatted date string. $translation_callback = function ($matches) use($langcode) { $code = $matches[1]; $string = $matches[2]; if (!isset($this->formatTranslationCache[$langcode][$code][$string])) { $options = array('langcode' => $langcode); if ($code == 'F') { $options['context'] = 'Long month name'; } if ($code == '') { $this->formatTranslationCache[$langcode][$code][$string] = $string; } else { $this->formatTranslationCache[$langcode][$code][$string] = $this->t($string, array(), $options); } } return $this->formatTranslationCache[$langcode][$code][$string]; }; // Translate the marked sequences. $value = preg_replace_callback('/\\xEF([AaeDlMTF]?)(.*?)\\xFF/', $translation_callback, $format); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); } return $value; }