summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-04-10 08:47:57 (GMT)
committerAlex Pott2018-04-10 08:47:57 (GMT)
commit8a5dba55f8ff41e8d165d6d48a6553247ad91e67 (patch)
treea7c8d0104db74384212beed56dd54132b260dda8
parentb5818f8d7368718c0ff3315e6b21b2b84c963ce8 (diff)
Issue #2906229 by mpdonadio, wengerk, colan, gambry, Sutharsan, joelstein, Manuel Garcia, catch, marcvangend: Add DateTimePlus::getPhpDateTime() for situations that require a DateTimeInterface
-rw-r--r--core/lib/Drupal/Component/Datetime/DateTimePlus.php10
-rw-r--r--core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php25
-rw-r--r--core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php25
3 files changed, 60 insertions, 0 deletions
diff --git a/core/lib/Drupal/Component/Datetime/DateTimePlus.php b/core/lib/Drupal/Component/Datetime/DateTimePlus.php
index 1161168..d045679 100644
--- a/core/lib/Drupal/Component/Datetime/DateTimePlus.php
+++ b/core/lib/Drupal/Component/Datetime/DateTimePlus.php
@@ -715,4 +715,14 @@ class DateTimePlus {
$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 b89d351..08eb206 100644
--- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
+++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php
@@ -891,4 +891,29 @@ class DateTimePlusTest extends TestCase {
$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 6c47779..4e915f7 100644
--- a/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php
+++ b/core/tests/Drupal/Tests/Core/Datetime/DrupalDateTimeTest.php
@@ -212,4 +212,29 @@ class DrupalDateTimeTest extends UnitTestCase {
$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());
+ }
+
}