You are here

public function DrupalDateTimeTest::testTimezoneFormat in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php \Drupal\Tests\system\Functional\Datetime\DrupalDateTimeTest::testTimezoneFormat()
  2. 9 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 111

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
    ->assertEquals('1972/10/11 12:25:21 UTC', $date
    ->format('Y/m/d H:i:s e'), 'Date has default UTC time zone and correct date/time.');

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

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