You are here

public function DrupalDateTimeTest::providerTestDateDiff in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php \Drupal\Tests\Core\Datetime\DrupalDateTimeTest::providerTestDateDiff()
  2. 9 core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php \Drupal\Tests\Core\Datetime\DrupalDateTimeTest::providerTestDateDiff()

Provides data for date tests.

Return value

array An array of arrays, each containing the input parameters for DrupalDateTimeTest::testDateDiff().

See also

DrupalDateTimeTest::testDateDiff()

File

core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php, line 60

Class

DrupalDateTimeTest
@coversDefaultClass \Drupal\Core\Datetime\DrupalDateTime @group Datetime

Namespace

Drupal\Tests\Core\Datetime

Code

public function providerTestDateDiff() {
  $settings = [
    'langcode' => 'en',
  ];
  $utc_tz = new \DateTimeZone('UTC');
  $empty_interval = new \DateInterval('PT0S');
  $positive_19_hours = new \DateInterval('PT19H');
  $positive_18_hours = new \DateInterval('PT18H');
  $positive_1_hour = new \DateInterval('PT1H');
  $negative_1_hour = new \DateInterval('PT1H');
  $negative_1_hour->invert = 1;
  return [
    // There should be a 19 hour time interval between
    // new years in Sydney and new years in LA in year 2000.
    [
      'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings),
      'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings),
      'absolute' => FALSE,
      'expected' => $positive_19_hours,
    ],
    // In 1970 Sydney did not observe daylight savings time
    // So there is only an 18 hour time interval.
    [
      'input2' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('Australia/Sydney'), $settings),
      'input1' => DrupalDateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00', new \DateTimeZone('America/Los_Angeles'), $settings),
      'absolute' => FALSE,
      'expected' => $positive_18_hours,
    ],
    [
      'input1' => DrupalDateTime::createFromFormat('U', 3600, new \DateTimeZone('America/Los_Angeles'), $settings),
      'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
      'absolute' => FALSE,
      'expected' => $negative_1_hour,
    ],
    [
      'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
      'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
      'absolute' => FALSE,
      'expected' => $negative_1_hour,
    ],
    [
      'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
      'input2' => \DateTime::createFromFormat('U', 0),
      'absolute' => FALSE,
      'expected' => $negative_1_hour,
    ],
    [
      'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
      'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
      'absolute' => TRUE,
      'expected' => $positive_1_hour,
    ],
    [
      'input1' => DrupalDateTime::createFromFormat('U', 3600, $utc_tz, $settings),
      'input2' => \DateTime::createFromFormat('U', 0),
      'absolute' => TRUE,
      'expected' => $positive_1_hour,
    ],
    [
      'input1' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
      'input2' => DrupalDateTime::createFromFormat('U', 0, $utc_tz, $settings),
      'absolute' => FALSE,
      'expected' => $empty_interval,
    ],
  ];
}