You are here

function DateTimeFunctionalTest::testTimeZoneHandling in Drupal 7

Test time zones and DST handling.

File

modules/system/system.test, line 1280
Tests for system.module.

Class

DateTimeFunctionalTest
Tests generic date and time handling capabilities of Drupal.

Code

function testTimeZoneHandling() {

  // Setup date/time settings for Honolulu time.
  variable_set('date_default_timezone', 'Pacific/Honolulu');
  variable_set('configurable_timezones', 0);
  variable_set('date_format_medium', 'Y-m-d H:i:s O');

  // Create some nodes with different authored-on dates.
  $date1 = '2007-01-31 21:00:00 -1000';
  $date2 = '2007-07-31 21:00:00 -1000';
  $node1 = $this
    ->drupalCreateNode(array(
    'created' => strtotime($date1),
    'type' => 'article',
  ));
  $node2 = $this
    ->drupalCreateNode(array(
    'created' => strtotime($date2),
    'type' => 'article',
  ));

  // Confirm date format and time zone.
  $this
    ->drupalGet("node/{$node1->nid}");
  $this
    ->assertText('2007-01-31 21:00:00 -1000', 'Date should be identical, with GMT offset of -10 hours.');
  $this
    ->drupalGet("node/{$node2->nid}");
  $this
    ->assertText('2007-07-31 21:00:00 -1000', 'Date should be identical, with GMT offset of -10 hours.');

  // Set time zone to Los Angeles time.
  variable_set('date_default_timezone', 'America/Los_Angeles');

  // Confirm date format and time zone.
  $this
    ->drupalGet("node/{$node1->nid}");
  $this
    ->assertText('2007-01-31 23:00:00 -0800', 'Date should be two hours ahead, with GMT offset of -8 hours.');
  $this
    ->drupalGet("node/{$node2->nid}");
  $this
    ->assertText('2007-08-01 00:00:00 -0700', 'Date should be three hours ahead, with GMT offset of -7 hours.');
}