* @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:----tgCfDYyedyaPDpf0ZP3d4aXssc2WZHM+BjIpYvFOo5blOmQJs8BZK2STGS/kDiVyhv63TrryhekadMuthtRXDeoqw4xe3HPx+f6lcy7oXTpKdxTuPXG4Rd7fc/Ri5YLLtozx55cZ9ej4c+k3FQT0RSFnWP6XoOKYZzAb3Y2xTMLds9Mx6jWMJ+FuiCf+vc3QkHJKVWQnKrVk0cjFigqvzJlZMzqPwymHQ6pjF/9DUnMtYRvAsrEQp3fIVZLmJwMsw8rI6GUFy9VSDbe+GRHt+yz4Etic8twZZuTPwih6UkTPCvlesLtaH5XqmzPVV68ZGxj1P2LBDP30wU5pzoE+hDnJaOYjXDuC4ERxWrhlXFM1sHJuiPDXlZmm232J188OH6q07nVl/Nb1Z9JP0SfXPucOcuV3a2R6doTEFQjSG54wXh7eeMZ8XVBICYpTUyd6Nftl5f4D37afS1wRzaoY3iOKiLrFVNZuzx5kS6ySaVGPNqziamvJJdoaJAtzSZ6mJCWl7LgyAu8gMTHhQqsPMv6Xi3Fi1U53EXz8Km35TG+ugRPQdC2pEXDZCWlKd/W2y2eryUjOQ+QiP9jUeiNj5RiD6C6pdJLDLDjPpk6D0AqiWhAYl3EylMUgsr0Bmj2aNp0iPjk9cRPnLRordPDyYn8FJCfDWy/Fn1AnQAr+XRo=----ATTACHMENT:----NTMxMTE2ODk4MDQ3Njk3NSA1NDM3NjQ4MTk0ODYwNjc4IDI1NDU4MDc5OTMyMDE3NTc=