function DrupalDateTimeTest::testGetPhpDateTime

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php \Drupal\Tests\Core\Datetime\DrupalDateTimeTest::testGetPhpDateTime()
  2. 8.9.x core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php \Drupal\Tests\Core\Datetime\DrupalDateTimeTest::testGetPhpDateTime()
  3. 11.x core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php \Drupal\Tests\Core\Datetime\DrupalDateTimeTest::testGetPhpDateTime()

@covers ::getPhpDateTime

File

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

Class

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

Namespace

Drupal\Tests\Core\Datetime

Code

public function testGetPhpDateTime() : void {
    $new_york = new \DateTimeZone('America/New_York');
    $berlin = new \DateTimeZone('Europe/Berlin');
    // Test retrieving a cloned copy of the wrapped \DateTime object, and that
    // altering it does not change the DrupalDateTime object.
    $drupal_date_time = DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2017-07-13 22:40:00', $new_york, [
        'langcode' => 'en',
    ]);
    $this->assertEquals(1500000000, $drupal_date_time->getTimestamp());
    $this->assertEquals('America/New_York', $drupal_date_time->getTimezone()
        ->getName());
    $datetime = $drupal_date_time->getPhpDateTime();
    $this->assertInstanceOf('DateTime', $datetime);
    $this->assertEquals(1500000000, $datetime->getTimestamp());
    $this->assertEquals('America/New_York', $datetime->getTimezone()
        ->getName());
    $datetime->setTimestamp(1400000000)
        ->setTimezone($berlin);
    $this->assertEquals(1400000000, $datetime->getTimestamp());
    $this->assertEquals('Europe/Berlin', $datetime->getTimezone()
        ->getName());
    $this->assertEquals(1500000000, $drupal_date_time->getTimestamp());
    $this->assertEquals('America/New_York', $drupal_date_time->getTimezone()
        ->getName());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.