diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php index 11611686728ba198e1c82cd16e4ff6891ea37bd6..d045679434c6ae4eb637c7ebf62b20db453f8230 100644 --- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php +++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php @@ -715,4 +715,14 @@ public function setDefaultDateTime() { $this->dateTimeObject->setTime(12, 0, 0); } + /** + * Gets a clone of the proxied PHP \DateTime object wrapped by this class. + * + * @return \DateTime + * A clone of the wrapped PHP \DateTime object. + */ + public function getPhpDateTime() { + return clone $this->dateTimeObject; + } + } diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php index b89d351c755c9c3067eb64904fdd91dc16fb9e9d..08eb2068ff5a97c191f40ac2010130c2e177ab99 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @@ -891,4 +891,29 @@ public function testChainableNonCallable() { $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent(); } + /** + * @covers ::getPhpDateTime + */ + public function testGetPhpDateTime() { + $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 DateTimePlus object. + $datetimeplus = DateTimePlus::createFromFormat('Y-m-d H:i:s', '2017-07-13 22:40:00', $new_york, ['langcode' => 'en']); + $this->assertEquals(1500000000, $datetimeplus->getTimestamp()); + $this->assertEquals('America/New_York', $datetimeplus->getTimezone()->getName()); + + $datetime = $datetimeplus->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, $datetimeplus->getTimestamp()); + $this->assertEquals('America/New_York', $datetimeplus->getTimezone()->getName()); + } + } diff --git a/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php index 6c4777953545e7c74b6d223ecbbec90294413b61..4e915f772b8ccca4811f0d75c70c77137a1149fb 100644 --- a/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php @@ -212,4 +212,29 @@ public function testChainableNonCallable() { $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent(); } + /** + * @covers ::getPhpDateTime + */ + public function testGetPhpDateTime() { + $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. + $drupaldatetime = DrupalDateTime::createFromFormat('Y-m-d H:i:s', '2017-07-13 22:40:00', $new_york, ['langcode' => 'en']); + $this->assertEquals(1500000000, $drupaldatetime->getTimestamp()); + $this->assertEquals('America/New_York', $drupaldatetime->getTimezone()->getName()); + + $datetime = $drupaldatetime->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, $drupaldatetime->getTimestamp()); + $this->assertEquals('America/New_York', $drupaldatetime->getTimezone()->getName()); + } + }