You are here

function DateSqlHandler::sql_tz in Date 8

Select a date value from the database, adjusting the value for the timezone.

Check whether database timezone conversion is supported in this system and use it if possible, otherwise use an offset.

Parameters

string $field: The field to be adjusted.

bool $offset: Set a fixed offset or offset field to use for the date. If set, no timezone conversion will be done and the offset will be used.

1 call to DateSqlHandler::sql_tz()
DateSqlHandler::sql_field in date_api/lib/Drupal/date_api/DateSqlHandler.php
Helper function to create cross-database SQL dates.

File

date_api/lib/Drupal/date_api/DateSqlHandler.php, line 266

Class

DateSqlHandler
A class to manipulate date SQL.

Namespace

Drupal\date_api

Code

function sql_tz($field, $offset = NULL, $comp_date = NULL) {

  // If the timezones are values they need to be quoted, but
  // if they are field names they do not.
  $db_zone = !empty($this->db_timezone_field) ? $this->db_timezone_field : "'{$this->db_timezone}'";
  $localzone = !empty($this->local_timezone_field) ? $this->local_timezone_field : "'{$this->local_timezone}'";

  // If a fixed offset is required, use it.
  if ($offset !== NULL) {
    return $this
      ->sql_offset($field, $offset);
  }
  elseif ($db_zone == $localzone) {
    return $this
      ->sql_offset($field, 0);
  }
  elseif (!$this
    ->db_tz_support() || empty($localzone)) {
    if (!empty($this->offset_field)) {
      return $this
        ->sql_offset($field, $this->offset_field);
    }
    else {
      return $this
        ->sql_offset($field, $this
        ->get_offset($comp_date));
    }
  }
  else {
    switch ($this->db_type) {
      case 'mysql':
      case 'mysqli':
        return "CONVERT_TZ({$field}, {$db_zone}, {$localzone})";
      case 'pgsql':

        // WITH TIME ZONE assumes the date is using the system
        // timezone, which should have been set to UTC.
        return "{$field}::timestamp with time zone AT TIME ZONE {$localzone}";
    }
  }
}