Chris@0: 2010, 'month' => 9, 'day' => 28) ) Chris@0: * Chris@18: * @see \Drupal\Component\Datetime\DateTimePlus Chris@0: */ Chris@0: class DrupalDateTime extends DateTimePlus { Chris@0: Chris@0: use StringTranslationTrait; Chris@0: Chris@0: /** Chris@0: * Format string translation cache. Chris@0: */ Chris@0: protected $formatTranslationCache; Chris@0: Chris@0: /** Chris@0: * Constructs a date object. Chris@0: * Chris@0: * @param string $time Chris@0: * A date/input_time_adjusted string. Defaults to 'now'. Chris@0: * @param mixed $timezone Chris@0: * PHP DateTimeZone object, string or NULL allowed. Chris@0: * Defaults to NULL. Note that the $timezone parameter and the current Chris@0: * timezone are ignored when the $time parameter either is a UNIX timestamp Chris@0: * (e.g. @946684800) or specifies a timezone Chris@0: * (e.g. 2010-01-28T15:00:00+02:00). Chris@14: * @see http://php.net/manual/datetime.construct.php Chris@0: * @param array $settings Chris@0: * - validate_format: (optional) Boolean choice to validate the Chris@0: * created date using the input format. The format used in Chris@0: * createFromFormat() allows slightly different values than format(). Chris@0: * Using an input format that works in both functions makes it Chris@0: * possible to a validation step to confirm that the date created Chris@0: * from a format string exactly matches the input. This option Chris@0: * indicates the format can be used for validation. Defaults to TRUE. Chris@0: * - langcode: (optional) Used to control the result of the format() method. Chris@0: * Defaults to NULL. Chris@0: * - debug: (optional) Boolean choice to leave debug values in the Chris@0: * date object for debugging purposes. Defaults to FALSE. Chris@0: */ Chris@0: public function __construct($time = 'now', $timezone = NULL, $settings = []) { Chris@0: if (!isset($settings['langcode'])) { Chris@0: $settings['langcode'] = \Drupal::languageManager()->getCurrentLanguage()->getId(); Chris@0: } Chris@0: Chris@0: // Instantiate the parent class. Chris@0: parent::__construct($time, $timezone, $settings); Chris@0: Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides prepareTimezone(). Chris@0: * Chris@0: * Override basic component timezone handling to use Drupal's Chris@0: * knowledge of the preferred user timezone. Chris@0: */ Chris@0: protected function prepareTimezone($timezone) { Chris@0: if (empty($timezone)) { Chris@0: // Fallback to user or system default timezone. Chris@0: $timezone = drupal_get_user_timezone(); Chris@0: } Chris@0: return parent::prepareTimezone($timezone); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides format(). Chris@0: * Chris@0: * @param string $format Chris@0: * A format string using either PHP's date(). Chris@0: * @param array $settings Chris@0: * - timezone: (optional) String timezone name. Defaults to the timezone Chris@0: * of the date object. Chris@0: * - langcode: (optional) String two letter language code used to control Chris@0: * the result of the format() method. Defaults to NULL. Chris@0: * Chris@0: * @return string Chris@0: * The formatted value of the date. Since the format may contain user input, Chris@0: * this value should be escaped when output. Chris@0: */ Chris@0: public function format($format, $settings = []) { Chris@0: $langcode = !empty($settings['langcode']) ? $settings['langcode'] : $this->langcode; Chris@0: $value = ''; Chris@0: // Format the date and catch errors. Chris@0: try { Chris@0: // Encode markers that should be translated. 'A' becomes Chris@0: // '\xEF\AA\xFF'. xEF and xFF are invalid UTF-8 sequences, Chris@0: // and we assume they are not in the input string. Chris@0: // Paired backslashes are isolated to prevent errors in Chris@0: // read-ahead evaluation. The read-ahead expression ensures that Chris@0: // A matches, but not \A. Chris@0: $format = preg_replace(['/\\\\\\\\/', '/(?formatTranslationCache[$langcode][$code][$string])) { Chris@0: $options = ['langcode' => $langcode]; Chris@0: if ($code == 'F') { Chris@0: $options['context'] = 'Long month name'; Chris@0: } Chris@0: Chris@0: if ($code == '') { Chris@0: $this->formatTranslationCache[$langcode][$code][$string] = $string; Chris@0: } Chris@0: else { Chris@0: $this->formatTranslationCache[$langcode][$code][$string] = $this->t($string, [], $options); Chris@0: } Chris@0: } Chris@0: return $this->formatTranslationCache[$langcode][$code][$string]; Chris@0: }; Chris@0: Chris@0: // Translate the marked sequences. Chris@0: $value = preg_replace_callback('/\xEF([AaeDlMTF]?)(.*?)\xFF/', $translation_callback, $format); Chris@0: } Chris@0: catch (\Exception $e) { Chris@0: $this->errors[] = $e->getMessage(); Chris@0: } Chris@0: return $value; Chris@0: } Chris@0: Chris@0: }