You are here

class CalendarSystemsDateFormatter in Calendar Systems 8.2

Hierarchy

Expanded class hierarchy of CalendarSystemsDateFormatter

1 file declares its use of CalendarSystemsDateFormatter
CalendarSystemsServiceProvider.php in src/CalendarSystemsServiceProvider.php

File

src/Services/DateFormatter/CalendarSystemsDateFormatter.php, line 8

Namespace

Drupal\calendar_systems\Services\DateFormatter
View source
class CalendarSystemsDateFormatter extends DateFormatter {
  public function format($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {

    // Return default formatter needs caller needs machine-readable data
    $calendar_name = Helpers::calendar_systems_get_calendar_system_name();
    $calendar = Helpers::calendar_systems_get_calendar_instance();
    if ($calendar_name == 'default' || !$calendar || Helpers::shouldIgnoreCalendarSystemsDateConversion()) {
      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);
    }
    $calendar
      ->setTimeZoneOffset($this->timezones[$timezone]
      ->getOffset());

    //$cal->setTimestamp($timestamp);
    if (empty($langcode)) {
      $langcode = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
    }

    // 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();
      }
    }

    // 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 $calendar
      ->timestampToStr($format, $timestamp + $this->timezones[$timezone]
      ->getOffset());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CalendarSystemsDateFormatter::format public function Formats a date, using a date type or a custom date format string. Overrides DateFormatter::format
DateFormatter::$configFactory protected property The configuration factory.
DateFormatter::$country protected property
DateFormatter::$dateFormats protected property
DateFormatter::$dateFormatStorage protected property The date format storage.
DateFormatter::$languageManager protected property Language manager for retrieving the default langcode when none is specified.
DateFormatter::$requestStack protected property The request stack.
DateFormatter::$timezones protected property The list of loaded timezones.
DateFormatter::$units protected property Contains the different date interval units.
DateFormatter::country protected function Returns the default country from config.
DateFormatter::dateFormat protected function Loads the given format pattern for the given langcode.
DateFormatter::formatDiff public function Formats a time interval between two timestamps. Overrides DateFormatterInterface::formatDiff
DateFormatter::formatInterval public function Formats a time interval with the requested granularity. Overrides DateFormatterInterface::formatInterval
DateFormatter::formatTimeDiffSince public function Formats the time difference from a timestamp to the current request time. Overrides DateFormatterInterface::formatTimeDiffSince
DateFormatter::formatTimeDiffUntil public function Formats the time difference from the current request time to a timestamp. Overrides DateFormatterInterface::formatTimeDiffUntil
DateFormatter::getSampleDateFormats public function Provides values for all date formatting characters for a given timestamp. Overrides DateFormatterInterface::getSampleDateFormats
DateFormatter::__construct public function Constructs a Date object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.