You are here

public function date_sql_handler::set_db_timezone in Date 7.3

Same name and namespace in other branches
  1. 5.2 date_api_sql.inc \date_sql_handler::set_db_timezone()
  2. 6.2 date_api_sql.inc \date_sql_handler::set_db_timezone()
  3. 6 date_api_sql.inc \date_sql_handler::set_db_timezone()
  4. 7 date_api/date_api_sql.inc \date_sql_handler::set_db_timezone()
  5. 7.2 date_api/date_api_sql.inc \date_sql_handler::set_db_timezone()

Set the database timzone 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.

1 call to date_sql_handler::set_db_timezone()
date_sql_handler::__construct in date_api/date_api_sql.inc
The object constuctor.

File

date_api/date_api_sql.inc, line 167
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.
        // @code
        // db_query('TimeZone.setDefault(TimeZone.getTimeZone("GMT"))');
        // @endcode
        break;
    }
    $already_set = TRUE;
  }
}