1 date_sql_handler.inc public date_sql_handler::set_db_timezone($offset = '+00:00')

Set the database timezone offset.

Setting the db timezone to UTC is done to ensure consistency in date handling whether or not the database can do proper timezone conversion.

Views filters that not exposed are cached and won't set the timezone so views date filters should add 'cacheable' => 'no' to their definitions to ensure that the database timezone gets set properly when the query is executed.

Parameters

string $offset: An offset value to set the database timezone to. This will only set a fixed offset, not a timezone, so any value other than '+00:00' should be used with caution.

File

core/modules/date/views/date_sql_handler.inc, line 78
SQL helper for Date API.

Class

date_sql_handler
A class to manipulate date SQL.

Code

public function set_db_timezone($offset = '+00:00') {
  static $already_set = FALSE;
  $type = Database::getConnection()->databaseType();
  if (!$already_set) {
    switch ($type) {
      case 'mysql':
        db_query("SET @@session.time_zone = '$offset'");
        break;

      case 'pgsql':
        db_query("SET TIME ZONE INTERVAL '$offset' HOUR TO MINUTE");
        break;

      case 'sqlsrv':
        // Issue #1201342, This is the wrong way to set the timezone, this
        // still needs to be fixed. In the meantime, commenting this out makes
        // SQLSRV functional.
        // db_query('TimeZone.setDefault(TimeZone.getTimeZone("GMT"))');
        break;
    }
    $already_set = TRUE;
  }
}