protected function DateTimePlus::prepareTimezone

Prepares the input timezone value.

Changes the timezone before trying to use it, if necessary. Most imporantly, makes sure there is a valid timezone object before moving further.

Parameters

mixed $timezone: Either a timezone name or a timezone object or NULL.

2 calls to DateTimePlus::prepareTimezone()
DateTimePlus::__construct in drupal/core/lib/Drupal/Component/Datetime/DateTimePlus.php
Constructs a date object set to a requested date and timezone.
DrupalDateTime::prepareTimezone in drupal/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Overrides prepareTimezone().
1 method overrides DateTimePlus::prepareTimezone()
DrupalDateTime::prepareTimezone in drupal/core/lib/Drupal/Core/Datetime/DrupalDateTime.php
Overrides prepareTimezone().

File

drupal/core/lib/Drupal/Component/Datetime/DateTimePlus.php, line 244
Definition of Drupal\Component\Datetime\DateTimePlus

Class

DateTimePlus
Extends DateTime().

Namespace

Drupal\Component\Datetime

Code

protected function prepareTimezone($timezone) {

  // If the input timezone is a valid timezone object, use it.
  if ($timezone instanceof \DateTimezone) {
    $timezone_adjusted = $timezone;
  }
  elseif (empty($timezone) && $this->inputTimeAdjusted instanceof \DateTime) {
    $timezone_adjusted = $this->inputTimeAdjusted
      ->getTimezone();
  }
  elseif (!empty($timezone) && is_string($timezone)) {
    $timezone_adjusted = new \DateTimeZone($timezone);
  }

  // Default to the system timezone when not explicitly provided.
  // If the system timezone is missing, use 'UTC'.
  if (empty($timezone_adjusted) || !$timezone_adjusted instanceof \DateTimezone) {
    $system_timezone = date_default_timezone_get();
    $timezone_name = !empty($system_timezone) ? $system_timezone : 'UTC';
    $timezone_adjusted = new \DateTimeZone($timezone_name);
  }

  // We are finally certain that we have a usable timezone.
  $this->inputTimeZoneAdjusted = $timezone_adjusted;
}