class DrupalDateTime in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/Datetime/DrupalDateTime.php \Drupal\Core\Datetime\DrupalDateTime
Extends DateTimePlus().
This class extends the basic component and adds in Drupal-specific handling, like translation of the format() method.
Static methods in base class can also be used to create DrupalDateTime objects. For example:
DrupalDateTime::createFromArray( array('year' => 2010, 'month' => 9, 'day' => 28) )
Hierarchy
- class \Drupal\Component\Datetime\DateTimePlus uses ToStringTrait
- class \Drupal\Core\Datetime\DrupalDateTime uses StringTranslationTrait
Expanded class hierarchy of DrupalDateTime
See also
\Drupal/Component/Datetime/DateTimePlus.php
23 files declare their use of DrupalDateTime
- CommentForm.php in core/
modules/ comment/ src/ CommentForm.php - Contains \Drupal\comment\CommentForm.
- CommentPreviewTest.php in core/
modules/ comment/ src/ Tests/ CommentPreviewTest.php - Contains \Drupal\comment\Tests\CommentPreviewTest.
- DateElementBase.php in core/
lib/ Drupal/ Core/ Datetime/ Element/ DateElementBase.php - Contains \Drupal\Core\Datetime\Element\DateElementBase.
- DateFormatFormBase.php in core/
modules/ system/ src/ Form/ DateFormatFormBase.php - Contains \Drupal\system\Form\DateFormatFormBase.
- Datelist.php in core/
lib/ Drupal/ Core/ Datetime/ Element/ Datelist.php - Contains \Drupal\Core\Datetime\Element\Datelist.
File
- core/
lib/ Drupal/ Core/ Datetime/ DrupalDateTime.php, line 25 - Contains \Drupal\Core\Datetime\DrupalDateTime.
Namespace
Drupal\Core\DatetimeView source
class DrupalDateTime extends DateTimePlus {
use StringTranslationTrait;
/**
* Format string translation cache.
*
*/
protected $formatTranslationCache;
/**
* Constructs a date object.
*
* @param string $time
* A date/input_time_adjusted string. Defaults to 'now'.
* @param mixed $timezone
* PHP DateTimeZone object, string or NULL allowed.
* Defaults to NULL.
* @param array $settings
* - validate_format: (optional) Boolean choice to validate the
* created date using the input format. The format used in
* createFromFormat() allows slightly different values than format().
* Using an input format that works in both functions makes it
* possible to a validation step to confirm that the date created
* from a format string exactly matches the input. This option
* indicates the format can be used for validation. Defaults to TRUE.
* - langcode: (optional) Used to control the result of the format() method.
* Defaults to NULL.
* - debug: (optional) Boolean choice to leave debug values in the
* date object for debugging purposes. Defaults to FALSE.
*/
public function __construct($time = 'now', $timezone = NULL, $settings = array()) {
if (!isset($settings['langcode'])) {
$settings['langcode'] = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
}
// Instantiate the parent class.
parent::__construct($time, $timezone, $settings);
}
/**
* Overrides prepareTimezone().
*
* Override basic component timezone handling to use Drupal's
* knowledge of the preferred user timezone.
*/
protected function prepareTimezone($timezone) {
if (empty($timezone)) {
// Fallback to user or system default timezone.
$timezone = drupal_get_user_timezone();
}
return parent::prepareTimezone($timezone);
}
/**
* 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. Since the format may contain user input,
* this value should be escaped when output.
*/
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(
"",
"",
), $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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateTimePlus:: |
protected static | property | An array of possible date parts. | |
DateTimePlus:: |
protected | property | The DateTime object. | |
DateTimePlus:: |
protected | property | An array of errors encountered when creating this date. | |
DateTimePlus:: |
protected | property | The prepared format, if provided. | |
DateTimePlus:: |
protected | property | The value of the format passed to the constructor. | |
DateTimePlus:: |
protected | property | The prepared time, without timezone, for this date. | |
DateTimePlus:: |
protected | property | The value of the time value passed to the constructor. | |
DateTimePlus:: |
protected | property | The prepared timezone object used to construct this date. | |
DateTimePlus:: |
protected | property | The value of the timezone passed to the constructor. | |
DateTimePlus:: |
protected | property | The value of the language code passed to the constructor. | |
DateTimePlus:: |
public static | function | Creates an ISO date from an array of values. | |
DateTimePlus:: |
public static | function | Checks that arrays of date parts will create a valid date. | |
DateTimePlus:: |
public | function | Examines getLastErrors() to see what errors to report. | |
DateTimePlus:: |
public static | function | Creates a date object from an array of date parts. | |
DateTimePlus:: |
public static | function | Creates a date object from an input date object. | |
DateTimePlus:: |
public static | function | Creates a date object from an input format. | |
DateTimePlus:: |
public static | function | Creates a date object from timestamp input. | |
DateTimePlus:: |
public static | function | Pads date parts with zeros. | |
DateTimePlus:: |
constant | |||
DateTimePlus:: |
public | function | Gets error messages. | |
DateTimePlus:: |
public | function | Detects if there were errors in the processing of this date. | |
DateTimePlus:: |
public static | function | Creates a complete array from a possibly incomplete array of date parts. | |
DateTimePlus:: |
protected | function | Prepares the input format value. | |
DateTimePlus:: |
protected | function | Prepares the input time value. | |
DateTimePlus:: |
public | function |
Renders the timezone name. Overrides ToStringTrait:: |
|
DateTimePlus:: |
constant | A RFC7231 Compliant date. | ||
DateTimePlus:: |
public | function | Implements the magic __call method. | |
DateTimePlus:: |
public static | function | Implements the magic __callStatic method. | |
DateTimePlus:: |
public | function | Implements the magic __clone method. | |
DrupalDateTime:: |
protected | property | Format string translation cache. | |
DrupalDateTime:: |
public | function |
Overrides format(). Overrides DateTimePlus:: |
|
DrupalDateTime:: |
protected | function |
Overrides prepareTimezone(). Overrides DateTimePlus:: |
|
DrupalDateTime:: |
public | function |
Constructs a date object. Overrides DateTimePlus:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ToStringTrait:: |
protected | function | For test purposes, wrap die() in an overridable method. | |
ToStringTrait:: |
public | function | Implements the magic __toString() method. |