* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class DateTimeTest extends TestCase { public function testDateTime() { $date = new DateTime('2015-04-25T19:35:20'); $this->assertEquals(2015, $date->getYear()); $this->assertEquals(4, $date->getMonth()); $this->assertEquals(25, $date->getDay()); $this->assertEquals(19, $date->getHour()); $this->assertEquals(35, $date->getMinute()); $this->assertEquals(20, $date->getSecond()); $this->assertEquals(0, $date->getMicroSecond()); $this->assertEquals(0, $date->getOffset()); $this->assertEquals('2015-04-25T19:35:20Z', $date->toString()); $this->assertEquals('"2015-04-25T19:35:20Z"', \json_encode($date)); } public function testDateTimeMicroSeconds() { $date = new DateTime('2015-04-25T19:35:20.1234'); $this->assertEquals(2015, $date->getYear()); $this->assertEquals(4, $date->getMonth()); $this->assertEquals(25, $date->getDay()); $this->assertEquals(19, $date->getHour()); $this->assertEquals(35, $date->getMinute()); $this->assertEquals(20, $date->getSecond()); $this->assertEquals(123400, $date->getMicroSecond()); $this->assertEquals(0, $date->getOffset()); $this->assertEquals('2015-04-25T19:35:20Z', $date->toString()); } public function testDateTimeOffset() { $date = new DateTime('2015-04-25T19:35:20+01:00'); $this->assertEquals(2015, $date->getYear()); $this->assertEquals(4, $date->getMonth()); $this->assertEquals(25, $date->getDay()); $this->assertEquals(19, $date->getHour()); $this->assertEquals(35, $date->getMinute()); $this->assertEquals(20, $date->getSecond()); $this->assertEquals(0, $date->getMicroSecond()); $this->assertEquals(3600, $date->getOffset()); $this->assertEquals('2015-04-25T19:35:20+01:00', $date->toString()); } public function testDateTimeMicroSecondsAndOffset() { $date = new DateTime('2015-04-25T19:35:20.1234+01:00'); $this->assertEquals(2015, $date->getYear()); $this->assertEquals(4, $date->getMonth()); $this->assertEquals(25, $date->getDay()); $this->assertEquals(19, $date->getHour()); $this->assertEquals(35, $date->getMinute()); $this->assertEquals(20, $date->getSecond()); $this->assertEquals(123400, $date->getMicroSecond()); $this->assertEquals(3600, $date->getOffset()); $this->assertEquals('2015-04-25T19:35:20+01:00', $date->toString()); } /** * @dataProvider providerRfc */ public function testRfcExamples($data, $expected) { $date = new DateTime($data); $this->assertEquals($expected, $date->toString()); } public function providerRfc() { return [ ['1985-04-12T23:20:50.52Z', '1985-04-12T23:20:50Z'], ['1996-12-19T16:39:57-08:00', '1996-12-19T16:39:57-08:00'], ['1937-01-01T12:00:27.87+00:20', '1937-01-01T12:00:27+00:20'], ]; } public function testConstructorFull() { $date = new DateTime(2014, 1, 1, 13, 37, 12); $this->assertEquals('2014-01-01T13:37:12Z', $date->toString()); } public function testToString() { $date = new DateTime(2014, 1, 1, 13, 37, 12); $this->assertEquals('2014-01-01T13:37:12Z', (string) $date); } public function testDateTimeNow() { $date = new DateTime(); $this->assertEquals(date('Y-m-d\TH:i:s\Z'), $date->toString()); } /** * @expectedException \InvalidArgumentException */ public function testDateTimeEmpty() { new DateTime(''); } /** * @expectedException \InvalidArgumentException */ public function testDateTimeInvalid() { new DateTime('foo'); } /** * @expectedException \InvalidArgumentException */ public function testDateTimeInvalidOffset() { new DateTime('2015-04-25T19:35:20+50:00'); } public function testMysqlDateTimeFormat() { $date = new DateTime('2015-04-25 19:35:20'); $this->assertEquals('2015-04-25T19:35:20Z', $date->toString()); } public function testFromDateTime() { $date = DateTime::fromDateTime(new \DateTime('2015-04-25T19:35:20')); $this->assertEquals('2015-04-25T19:35:20Z', $date->toString()); } public function testGetOffsetBySeconds() { $this->assertEquals('+00:00', DateTime::getOffsetBySeconds(0)); $this->assertEquals('+01:00', DateTime::getOffsetBySeconds(3600)); $this->assertEquals('-01:00', DateTime::getOffsetBySeconds(-3600)); $this->assertEquals('+01:30', DateTime::getOffsetBySeconds(5400)); $this->assertEquals('-01:30', DateTime::getOffsetBySeconds(-5400)); $this->assertEquals('+24:00', DateTime::getOffsetBySeconds(86400)); $this->assertEquals('-24:00', DateTime::getOffsetBySeconds(-86400)); } public function testGetSecondsFromOffset() { $this->assertEquals(0, DateTime::getSecondsFromOffset('+', 0, 0)); $this->assertEquals(3600, DateTime::getSecondsFromOffset('+', 1, 0)); $this->assertEquals(-3600, DateTime::getSecondsFromOffset('-', 1, 0)); $this->assertEquals(5400, DateTime::getSecondsFromOffset('+', 1, 30)); $this->assertEquals(-5400, DateTime::getSecondsFromOffset('-', 1, 30)); $this->assertEquals(86400, DateTime::getSecondsFromOffset('+', 24, 0)); $this->assertEquals(-86400, DateTime::getSecondsFromOffset('-', 24, 0)); } } __halt_compiler();----SIGNATURE:----iLMi5sQTk8Ki5tGkqIPfkVsNIo1kKsUW4Ol5TS4luJExZ6j2bOUElsY0pX+grMAIO4DEQ40d+BlOOKkz76y16UX04hOimzgaXAHDJ+i1ybn47X7h5pEF7im5p88Wc5cf+MVDhBO6M/YjYRXZc1k92JvF8bo8cBSH9MS4IoZoNiFIQnueJ/E89DsPMpJJphMWa/RRaTfIpQlHHTDk/oRK/VDCXLpXFmvsIQUp3V3aXtvHzzTcDMxxfLkptUJdNUot55l0i9UrBkqT6fgtXhYGLQnc5qiOIi3w/ryti6T9z0/mBkWPVwJRaDMZVurdVD9IworvZRr0f25yBvqDoGvzg91UNVYq/7a5nX7oY32fuyZh3KF3PEWXiLbSFYP/OUFyvonoqRy4N7/UIafPmw2YY9Gayenw3CX7D7cDNWGh9pxJvfOK7KmTEsH60B/koD55WdCGh9AT54OzsRepQdUeUCqNLvfJsJOwkYI8S/sysmmT+9QiAtOtb+TVjGXZWeYH9DIUCOhCC0B4m3DOCSCGtBH/b8uJh3Sp6uPKF6ooTqhTcVpvkT/RTQS2/rAjpkZ1r4/fpLksmAnynOdvDZbtmOWpGjyl2lICOxR3EO+kS17omU/TgJlgnZkmRuVG+n7Dz6EeK9TeyLZuYTPEREKKt3MTgPzKDuTAhQa7kFFjrUY=----ATTACHMENT:----ODE5ODkwODk2MjA2NTg3IDc0NzEwNTUzMjQ1OTczNTMgOTMzNjI1MjIzNjE5ODA5OQ==