class PersianDateFormatter in Persian Date for Drupal 8 8
Same name and namespace in other branches
- 8.4 src/Service/Formatter/PersianDateFormatter.php \Drupal\persian_date\Service\Formatter\PersianDateFormatter
Hierarchy
- class \Drupal\Core\Datetime\DateFormatter implements DateFormatterInterface uses StringTranslationTrait
- class \Drupal\persian_date\Service\Formatter\PersianDateFormatter
Expanded class hierarchy of PersianDateFormatter
1 file declares its use of PersianDateFormatter
- PersianDatePass.php in src/
DependencyInjection/ Pass/ PersianDatePass.php
File
- src/
Service/ Formatter/ PersianDateFormatter.php, line 10
Namespace
Drupal\persian_date\Service\FormatterView source
class PersianDateFormatter extends DateFormatter {
public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
if (!PersianLanguageDiscovery::isPersian()) {
return parent::format($timestamp, $type, $format, $timezone, $langcode);
}
if (!isset($timezone)) {
$timezone = date_default_timezone_get();
}
// Store DateTimeZone objects in an array rather than repeatedly
// constructing identical objects over the life of a request.
if (!isset($this->timezones[$timezone])) {
$this->timezones[$timezone] = timezone_open($timezone);
}
if (empty($langcode)) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
}
$date = PersianDateFactory::buildFromTimestamp($timestamp, $this->timezones[$timezone]);
// If we have a non-custom date format use the provided date format pattern.
if ($type !== 'custom') {
if ($date_format = $this
->dateFormat($type, $langcode)) {
$format = $date_format
->getPattern();
}
}
// called by query builder
if ($type === 'custom' & $format === DATETIME_DATETIME_STORAGE_FORMAT) {
// convert shamsi to georgian
$parent = parent::format($timestamp, $type, $format, $timezone, $langcode);
$other = PersianDateConverter::normalizeDate(new \DateTime($parent));
return parent::format($other
->getTimestamp(), $type, $format, $timezone, $langcode);
}
// Fall back to the 'medium' date format type if the format string is
// empty, either from not finding a requested date format or being given an
// empty custom format string.
if (empty($format)) {
$format = $this
->dateFormat('fallback', $langcode)
->getPattern();
}
return $date
->format($format);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DateFormatter:: |
protected | property | The configuration factory. | |
DateFormatter:: |
protected | property | ||
DateFormatter:: |
protected | property | ||
DateFormatter:: |
protected | property | The date format storage. | |
DateFormatter:: |
protected | property | Language manager for retrieving the default langcode when none is specified. | |
DateFormatter:: |
protected | property | The request stack. | |
DateFormatter:: |
protected | property | The list of loaded timezones. | |
DateFormatter:: |
protected | property | Contains the different date interval units. | |
DateFormatter:: |
protected | function | Returns the default country from config. | |
DateFormatter:: |
protected | function | Loads the given format pattern for the given langcode. | |
DateFormatter:: |
public | function |
Formats a time interval between two timestamps. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Formats a time interval with the requested granularity. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Formats the time difference from a timestamp to the current request time. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Formats the time difference from the current request time to a timestamp. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function |
Provides values for all date formatting characters for a given timestamp. Overrides DateFormatterInterface:: |
|
DateFormatter:: |
public | function | Constructs a Date object. | |
PersianDateFormatter:: |
public | function |
Formats a date, using a date type or a custom date format string. Overrides DateFormatter:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. |