function TimezoneController::getTimezone

Same name in other branches
  1. 9 core/modules/system/src/Controller/TimezoneController.php \Drupal\system\Controller\TimezoneController::getTimezone()
  2. 8.9.x core/modules/system/src/Controller/TimezoneController.php \Drupal\system\Controller\TimezoneController::getTimezone()
  3. 10 core/modules/system/src/Controller/TimezoneController.php \Drupal\system\Controller\TimezoneController::getTimezone()

Returns a time zone identifier given a time zone abbreviation.

Parameters

string $abbreviation: Time zone abbreviation.

int $offset: Offset from GMT in seconds. Defaults to -1 which means that first found time zone corresponding to abbreviation is returned. Otherwise exact offset is searched and only if not found then the first time zone with any offset is returned.

null|int $is_daylight_saving_time: Daylight saving time indicator. If abbreviation does not exist then the time zone is searched solely by offset and is DST.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The time zone identifier or 'false' in JsonResponse object.

1 string reference to 'TimezoneController::getTimezone'
system.routing.yml in core/modules/system/system.routing.yml
core/modules/system/system.routing.yml

File

core/modules/system/src/Controller/TimezoneController.php, line 29

Class

TimezoneController
Provides a callback for finding a time zone identifier.

Namespace

Drupal\system\Controller

Code

public function getTimezone($abbreviation = '', $offset = -1, $is_daylight_saving_time = NULL) {
    $offset = intval($offset);
    // Out of bounds check for offset. Offset +/- UTC is typically no
    // smaller/larger than -12/+14.
    if ($offset < -60000 || $offset > 60000) {
        return new JsonResponse(FALSE);
    }
    if (isset($is_daylight_saving_time)) {
        $original = intval($is_daylight_saving_time);
        $is_daylight_saving_time = min(1, max(-1, intval($is_daylight_saving_time)));
        // Catch if out of boundary.
        if ($original !== $is_daylight_saving_time) {
            return new JsonResponse(FALSE);
        }
    }
    // An abbreviation of "0" passed in the callback arguments should be
    // interpreted as the empty string.
    $abbreviation = $abbreviation ? $abbreviation : '';
    $timezone = timezone_name_from_abbr($abbreviation, $offset, $is_daylight_saving_time);
    return new JsonResponse($timezone);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.