You are here

public function date_sql_handler::sql_tz in Date 7.3

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 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 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 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 372
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

Code

public 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':
        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}";
    }
  }
}