You are here

public function DrupalDateTimeTest::testTimezoneFormat in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php \Drupal\Tests\system\Functional\Datetime\DrupalDateTimeTest::testTimezoneFormat()
  2. 10 core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php \Drupal\Tests\system\Functional\Datetime\DrupalDateTimeTest::testTimezoneFormat()

Tests the ability to override the time zone in the format method.

File

core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php, line 109

Class

DrupalDateTimeTest
Tests DrupalDateTime functionality.

Namespace

Drupal\Tests\system\Functional\Datetime

Code

public function testTimezoneFormat() {

  // Create a date in UTC
  $date = DrupalDateTime::createFromTimestamp(87654321, 'UTC');

  // Verify that the date format method displays the default time zone.
  $this
    ->assertEqual($date
    ->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date has default UTC time zone and correct date/time.');

  // Verify that the format method can override the time zone.
  $this
    ->assertEqual($date
    ->format('Y/m/d H:i:s e', [
    'timezone' => 'America/New_York',
  ]), '1972/10/11 08:25:21 America/New_York', 'Date displayed overidden time zone and correct date/time');

  // Verify that the date format method still displays the default time zone
  // for the date object.
  $this
    ->assertEqual($date
    ->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date still has default UTC time zone and correct date/time');
}