123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
- namespace Sabre\VObject;
- use DateTime;
- use DateTimeZone;
- use DateInterval;
- class DateTimeParserTest extends \PHPUnit_Framework_TestCase {
- function testParseICalendarDuration() {
- $this->assertEquals('+1 weeks', DateTimeParser::parseDuration('P1W',true));
- $this->assertEquals('+5 days', DateTimeParser::parseDuration('P5D',true));
- $this->assertEquals('+5 days 3 hours 50 minutes 12 seconds', DateTimeParser::parseDuration('P5DT3H50M12S',true));
- $this->assertEquals('-1 weeks 50 minutes', DateTimeParser::parseDuration('-P1WT50M',true));
- $this->assertEquals('+50 days 3 hours 2 seconds', DateTimeParser::parseDuration('+P50DT3H2S',true));
- $this->assertEquals('+0 seconds', DateTimeParser::parseDuration('+PT0S',true));
- $this->assertEquals(new DateInterval('PT0S'), DateTimeParser::parseDuration('PT0S'));
- }
- function testParseICalendarDurationDateInterval() {
- $expected = new DateInterval('P7D');
- $this->assertEquals($expected, DateTimeParser::parseDuration('P1W'));
- $this->assertEquals($expected, DateTimeParser::parse('P1W'));
- $expected = new DateInterval('PT3M');
- $expected->invert = true;
- $this->assertEquals($expected, DateTimeParser::parseDuration('-PT3M'));
- }
- /**
- * @expectedException LogicException
- */
- function testParseICalendarDurationFail() {
- DateTimeParser::parseDuration('P1X',true);
- }
- function testParseICalendarDateTime() {
- $dateTime = DateTimeParser::parseDateTime('20100316T141405');
- $compare = new DateTime('2010-03-16 14:14:05',new DateTimeZone('UTC'));
- $this->assertEquals($compare, $dateTime);
- }
- /**
- * @depends testParseICalendarDateTime
- * @expectedException LogicException
- */
- function testParseICalendarDateTimeBadFormat() {
- $dateTime = DateTimeParser::parseDateTime('20100316T141405 ');
- }
- /**
- * @depends testParseICalendarDateTime
- */
- function testParseICalendarDateTimeUTC() {
- $dateTime = DateTimeParser::parseDateTime('20100316T141405Z');
- $compare = new DateTime('2010-03-16 14:14:05',new DateTimeZone('UTC'));
- $this->assertEquals($compare, $dateTime);
- }
- /**
- * @depends testParseICalendarDateTime
- */
- function testParseICalendarDateTimeUTC2() {
- $dateTime = DateTimeParser::parseDateTime('20101211T160000Z');
- $compare = new DateTime('2010-12-11 16:00:00',new DateTimeZone('UTC'));
- $this->assertEquals($compare, $dateTime);
- }
- /**
- * @depends testParseICalendarDateTime
- */
- function testParseICalendarDateTimeCustomTimeZone() {
- $dateTime = DateTimeParser::parseDateTime('20100316T141405', new DateTimeZone('Europe/Amsterdam'));
- $compare = new DateTime('2010-03-16 14:14:05',new DateTimeZone('Europe/Amsterdam'));
- $this->assertEquals($compare, $dateTime);
- }
- function testParseICalendarDate() {
- $dateTime = DateTimeParser::parseDate('20100316');
- $expected = new DateTime('2010-03-16 00:00:00',new DateTimeZone('UTC'));
- $this->assertEquals($expected, $dateTime);
- $dateTime = DateTimeParser::parse('20100316');
- $this->assertEquals($expected, $dateTime);
- }
- /**
- * TCheck if a date with year > 4000 will not throw an exception. iOS seems to use 45001231 in yearly recurring events
- */
- function testParseICalendarDateGreaterThan4000() {
- $dateTime = DateTimeParser::parseDate('45001231');
- $expected = new DateTime('4500-12-31 00:00:00',new DateTimeZone('UTC'));
- $this->assertEquals($expected, $dateTime);
- $dateTime = DateTimeParser::parse('45001231');
- $this->assertEquals($expected, $dateTime);
- }
- /**
- * Check if a datetime with year > 4000 will not throw an exception. iOS seems to use 45001231T235959 in yearly recurring events
- */
- function testParseICalendarDateTimeGreaterThan4000() {
- $dateTime = DateTimeParser::parseDateTime('45001231T235959');
- $expected = new DateTime('4500-12-31 23:59:59',new DateTimeZone('UTC'));
- $this->assertEquals($expected, $dateTime);
- $dateTime = DateTimeParser::parse('45001231T235959');
- $this->assertEquals($expected, $dateTime);
- }
- /**
- * @depends testParseICalendarDate
- * @expectedException LogicException
- */
- function testParseICalendarDateBadFormat() {
- $dateTime = DateTimeParser::parseDate('20100316T141405');
- }
- /**
- * @dataProvider vcardDates
- */
- function testVCardDate($input, $output) {
- $this->assertEquals(
- $output,
- DateTimeParser::parseVCardDateTime($input)
- );
- }
- /**
- * @dataProvider vcardDates
- * @expectedException \InvalidArgumentException
- */
- function testBadVCardDate() {
- DateTimeParser::parseVCardDateTime('1985---01');
- }
- /**
- * @dataProvider vcardDates
- * @expectedException \InvalidArgumentException
- */
- function testBadVCardTime() {
- DateTimeParser::parseVCardTime('23:12:166');
- }
- function vcardDates() {
- return array(
- array(
- "19961022T140000",
- array(
- "year" => 1996,
- "month" => 10,
- "date" => 22,
- "hour" => 14,
- "minute" => 00,
- "second" => 00,
- "timezone" => null
- ),
- ),
- array(
- "--1022T1400",
- array(
- "year" => null,
- "month" => 10,
- "date" => 22,
- "hour" => 14,
- "minute" => 00,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "---22T14",
- array(
- "year" => null,
- "month" => null,
- "date" => 22,
- "hour" => 14,
- "minute" => null,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "19850412",
- array(
- "year" => 1985,
- "month" => 4,
- "date" => 12,
- "hour" => null,
- "minute" => null,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "1985-04",
- array(
- "year" => 1985,
- "month" => 04,
- "date" => null,
- "hour" => null,
- "minute" => null,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "1985",
- array(
- "year" => 1985,
- "month" => null,
- "date" => null,
- "hour" => null,
- "minute" => null,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "--0412",
- array(
- "year" => null,
- "month" => 4,
- "date" => 12,
- "hour" => null,
- "minute" => null,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "---12",
- array(
- "year" => null,
- "month" => null,
- "date" => 12,
- "hour" => null,
- "minute" => null,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "T102200",
- array(
- "year" => null,
- "month" => null,
- "date" => null,
- "hour" => 10,
- "minute" => 22,
- "second" => 0,
- "timezone" => null
- ),
- ),
- array(
- "T1022",
- array(
- "year" => null,
- "month" => null,
- "date" => null,
- "hour" => 10,
- "minute" => 22,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "T10",
- array(
- "year" => null,
- "month" => null,
- "date" => null,
- "hour" => 10,
- "minute" => null,
- "second" => null,
- "timezone" => null
- ),
- ),
- array(
- "T-2200",
- array(
- "year" => null,
- "month" => null,
- "date" => null,
- "hour" => null,
- "minute" => 22,
- "second" => 00,
- "timezone" => null
- ),
- ),
- array(
- "T--00",
- array(
- "year" => null,
- "month" => null,
- "date" => null,
- "hour" => null,
- "minute" => null,
- "second" => 00,
- "timezone" => null
- ),
- ),
- array(
- "T102200Z",
- array(
- "year" => null,
- "month" => null,
- "date" => null,
- "hour" => 10,
- "minute" => 22,
- "second" => 00,
- "timezone" => 'Z'
- ),
- ),
- array(
- "T102200-0800",
- array(
- "year" => null,
- "month" => null,
- "date" => null,
- "hour" => 10,
- "minute" => 22,
- "second" => 00,
- "timezone" => '-0800'
- ),
- ),
- // extended format
- array(
- "2012-11-29T15:10:53Z",
- array(
- "year" => 2012,
- "month" => 11,
- "date" => 29,
- "hour" => 15,
- "minute" => 10,
- "second" => 53,
- "timezone" => 'Z'
- ),
- ),
- // with milliseconds
- array(
- "20121129T151053.123Z",
- array(
- "year" => 2012,
- "month" => 11,
- "date" => 29,
- "hour" => 15,
- "minute" => 10,
- "second" => 53,
- "timezone" => 'Z'
- ),
- ),
- // extended format with milliseconds
- array(
- "2012-11-29T15:10:53.123Z",
- array(
- "year" => 2012,
- "month" => 11,
- "date" => 29,
- "hour" => 15,
- "minute" => 10,
- "second" => 53,
- "timezone" => 'Z'
- ),
- ),
- );
- }
- }
|