You are here

function date_sql_handler::sql_tz in Date 7

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::sql_tz()
  2. 6.2 date_api_sql.inc \date_sql_handler::sql_tz()
  3. 6 date_api_sql.inc \date_sql_handler::sql_tz()
  4. 7.3 date_api/date_api_sql.inc \date_sql_handler::sql_tz()
  5. 7.2 date_api/date_api_sql.inc \date_sql_handler::sql_tz()

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

$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 date_sql_handler::sql_tz()
date_sql_handler::sql_field in date_api/date_api_sql.inc
Helper function to create cross-database SQL dates.

File

date_api/date_api_sql.inc, line 271
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

Code

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

  // If the timezones are values they need to be quoted, but
  // if they are field names they do not.
  $db_zone = $this->db_timezone_field ? $this->db_timezone_field : "'{$this->db_timezone}'";
  $localzone = $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()) {
    if (!empty($this->offset_field)) {
      return $this
        ->sql_offset($field, $this->offset_field);
    }
    else {
      return $this
        ->sql_offset($field, $this
        ->get_offset());
    }
  }
  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}";
    }
  }
}