Unverified Commit 8a5dba55 authored by alexpott's avatar alexpott

Issue #2906229 by mpdonadio, wengerk, colan, gambry, Sutharsan, joelstein,...

Issue #2906229 by mpdonadio, wengerk, colan, gambry, Sutharsan, joelstein, Manuel Garcia, catch, marcvangend: Add DateTimePlus::getPhpDateTime() for situations that require a DateTimeInterface
parent b5818f8d
......@@ -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;
}
}
......@@ -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());
}
}
......@@ -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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment