You are here

function date_sql_handler::sql_tz in Date 5.2

Same name and namespace in other branches
  1. 6.2 date_api_sql.inc \date_sql_handler::sql_tz()
  2. 6 date_api_sql.inc \date_sql_handler::sql_tz()
  3. 7.3 date_api/date_api_sql.inc \date_sql_handler::sql_tz()
  4. 7 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_sql.inc
Helper function to create cross-database SQL dates.

File

./date_api_sql.inc, line 263

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 "TIMESTAMP WITH TIME ZONE {$field} AT TIME ZONE {$localzone}";
    }
  }
}