public function DateTimePlusTest::testDateTimezone

Test that DrupalDateTime can detect the right timezone to use. When specified or not.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Datetime/DateTimePlusTest.php, line 387
Definition of Drupal\system\Tests\Datetime\DateTimePlusTest.

Class

DateTimePlusTest

Namespace

Drupal\system\Tests\Datetime

Code

public function testDateTimezone() {
  global $user;
  $date_string = '2007-01-31 21:00:00';

  // Detect the system timezone.
  $system_timezone = date_default_timezone_get();

  // Create a date object with an unspecified timezone, which should
  // end up using the system timezone.
  $date = new DateTimePlus($date_string);
  $timezone = $date
    ->getTimezone()
    ->getName();
  $this
    ->assertTrue($timezone == $system_timezone, 'DateTimePlus uses the system timezone when there is no site timezone.');

  // Create a date object with a specified timezone name.
  $date = new DateTimePlus($date_string, 'America/Yellowknife');
  $timezone = $date
    ->getTimezone()
    ->getName();
  $this
    ->assertTrue($timezone == 'America/Yellowknife', 'DateTimePlus uses the specified timezone if provided.');

  // Create a date object with a timezone object.
  $date = new DateTimePlus($date_string, new \DateTimeZone('Australia/Canberra'));
  $timezone = $date
    ->getTimezone()
    ->getName();
  $this
    ->assertTrue($timezone == 'Australia/Canberra', 'DateTimePlus uses the specified timezone if provided.');

  // Create a date object with another date object.
  $new_date = new DateTimePlus('now', 'Pacific/Midway');
  $date = new DateTimePlus($new_date);
  $timezone = $date
    ->getTimezone()
    ->getName();
  $this
    ->assertTrue($timezone == 'Pacific/Midway', 'DateTimePlus uses the specified timezone if provided.');
}