function SqliteDateSql::getDateFormat

Same name in other branches
  1. 8.9.x core/modules/views/src/Plugin/views/query/SqliteDateSql.php \Drupal\views\Plugin\views\query\SqliteDateSql::getDateFormat()
  2. 10 core/modules/views/src/Plugin/views/query/SqliteDateSql.php \Drupal\views\Plugin\views\query\SqliteDateSql::getDateFormat()
  3. 11.x core/modules/views/src/Plugin/views/query/SqliteDateSql.php \Drupal\views\Plugin\views\query\SqliteDateSql::getDateFormat()

Overrides DateSqlInterface::getDateFormat

File

core/modules/views/src/Plugin/views/query/SqliteDateSql.php, line 83

Class

SqliteDateSql
SQLite-specific date handling.

Namespace

Drupal\views\Plugin\views\query

Code

public function getDateFormat($field, $format) {
    $format = strtr($format, static::$replace);
    // SQLite does not have an ISO week substitution string, so it needs special
    // handling.
    // @see http://wikipedia.org/wiki/ISO_week_date#Calculation
    // @see http://stackoverflow.com/a/15511864/1499564
    if ($format === '%W') {
        $expression = "((strftime('%j', date(strftime('%Y-%m-%d', {$field}, 'unixepoch'), '-3 days', 'weekday 4')) - 1) / 7 + 1)";
    }
    else {
        $expression = "strftime('{$format}', {$field}, 'unixepoch')";
    }
    // The expression yields a string, but the comparison value is an integer in
    // case the comparison value is a float, integer, or numeric. All of the
    // above SQLite format tokens only produce integers. However, the given
    // $format may contain 'Y-m-d', which results in a string.
    // @see \Drupal\sqlite\Driver\Database\sqlite\Connection::expandArguments()
    // @see http://www.sqlite.org/lang_datefunc.html
    // @see http://www.sqlite.org/lang_expr.html#castexpr
    if (preg_match('/^(?:%\\w)+$/', $format)) {
        $expression = "CAST({$expression} AS NUMERIC)";
    }
    return $expression;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.