You are here

public function Sql::getDateField in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Plugin/views/query/Sql.php \Drupal\views\Plugin\views\query\Sql::getDateField()

Returns a Unix timestamp to database native timestamp expression.

Parameters

string $field: The query field that will be used in the expression.

Return value

string An expression representing a timestamp with time zone.

Overrides QueryPluginBase::getDateField

File

core/modules/views/src/Plugin/views/query/Sql.php, line 1692
Contains \Drupal\views\Plugin\views\query\Sql.

Class

Sql
Views query plugin for an SQL query.

Namespace

Drupal\views\Plugin\views\query

Code

public function getDateField($field) {
  $db_type = Database::getConnection()
    ->databaseType();
  $offset = $this
    ->setupTimezone();
  if (isset($offset) && !is_numeric($offset)) {
    $dtz = new \DateTimeZone($offset);
    $dt = new \DateTime('now', $dtz);
    $offset_seconds = $dtz
      ->getOffset($dt);
  }
  switch ($db_type) {
    case 'mysql':
      $field = "DATE_ADD('19700101', INTERVAL {$field} SECOND)";
      if (!empty($offset)) {
        $field = "({$field} + INTERVAL {$offset_seconds} SECOND)";
      }
      break;
    case 'pgsql':
      $field = "TO_TIMESTAMP({$field})";
      if (!empty($offset)) {
        $field = "({$field} + INTERVAL '{$offset_seconds} SECONDS')";
      }
      break;
    case 'sqlite':
      if (!empty($offset)) {
        $field = "({$field} + {$offset_seconds})";
      }
      break;
  }
  return $field;
}