ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DateTimeParserTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\VObject;
4 
5 use DateInterval;
7 use DateTimeZone;
9 
10 class DateTimeParserTest extends TestCase {
11 
13 
14  $this->assertEquals('+1 weeks', DateTimeParser::parseDuration('P1W', true));
15  $this->assertEquals('+5 days', DateTimeParser::parseDuration('P5D', true));
16  $this->assertEquals('+5 days 3 hours 50 minutes 12 seconds', DateTimeParser::parseDuration('P5DT3H50M12S', true));
17  $this->assertEquals('-1 weeks 50 minutes', DateTimeParser::parseDuration('-P1WT50M', true));
18  $this->assertEquals('+50 days 3 hours 2 seconds', DateTimeParser::parseDuration('+P50DT3H2S', true));
19  $this->assertEquals('+0 seconds', DateTimeParser::parseDuration('+PT0S', true));
20  $this->assertEquals(new DateInterval('PT0S'), DateTimeParser::parseDuration('PT0S'));
21 
22  }
23 
25 
26  $expected = new DateInterval('P7D');
27  $this->assertEquals($expected, DateTimeParser::parseDuration('P1W'));
28  $this->assertEquals($expected, DateTimeParser::parse('P1W'));
29 
30  $expected = new DateInterval('PT3M');
31  $expected->invert = true;
32  $this->assertEquals($expected, DateTimeParser::parseDuration('-PT3M'));
33 
34  }
35 
40 
41  DateTimeParser::parseDuration('P1X', true);
42 
43  }
44 
46 
47  $dateTime = DateTimeParser::parseDateTime('20100316T141405');
48 
49  $compare = new DateTimeImmutable('2010-03-16 14:14:05', new DateTimeZone('UTC'));
50 
51  $this->assertEquals($compare, $dateTime);
52 
53  }
54 
60 
61  $dateTime = DateTimeParser::parseDateTime('20100316T141405 ');
62 
63  }
64 
70 
71  $dateTime = DateTimeParser::parseDateTime('20100316T251405');
72 
73  }
74 
79 
80  $dateTime = DateTimeParser::parseDateTime('20100316T141405Z');
81 
82  $compare = new DateTimeImmutable('2010-03-16 14:14:05', new DateTimeZone('UTC'));
83  $this->assertEquals($compare, $dateTime);
84 
85  }
86 
91 
92  $dateTime = DateTimeParser::parseDateTime('20101211T160000Z');
93 
94  $compare = new DateTimeImmutable('2010-12-11 16:00:00', new DateTimeZone('UTC'));
95  $this->assertEquals($compare, $dateTime);
96 
97  }
98 
103 
104  $dateTime = DateTimeParser::parseDateTime('20100316T141405', new DateTimeZone('Europe/Amsterdam'));
105 
106  $compare = new DateTimeImmutable('2010-03-16 14:14:05', new DateTimeZone('Europe/Amsterdam'));
107  $this->assertEquals($compare, $dateTime);
108 
109  }
110 
112 
113  $dateTime = DateTimeParser::parseDate('20100316');
114 
115  $expected = new DateTimeImmutable('2010-03-16 00:00:00', new DateTimeZone('UTC'));
116 
117  $this->assertEquals($expected, $dateTime);
118 
119  $dateTime = DateTimeParser::parse('20100316');
120  $this->assertEquals($expected, $dateTime);
121 
122  }
123 
128 
129  $dateTime = DateTimeParser::parseDate('45001231');
130 
131  $expected = new DateTimeImmutable('4500-12-31 00:00:00', new DateTimeZone('UTC'));
132 
133  $this->assertEquals($expected, $dateTime);
134 
135  $dateTime = DateTimeParser::parse('45001231');
136  $this->assertEquals($expected, $dateTime);
137 
138  }
139 
144 
145  $dateTime = DateTimeParser::parseDateTime('45001231T235959');
146 
147  $expected = new DateTimeImmutable('4500-12-31 23:59:59', new DateTimeZone('UTC'));
148 
149  $this->assertEquals($expected, $dateTime);
150 
151  $dateTime = DateTimeParser::parse('45001231T235959');
152  $this->assertEquals($expected, $dateTime);
153 
154  }
155 
161 
162  $dateTime = DateTimeParser::parseDate('20100316T141405');
163 
164  }
165 
171 
172  $dateTime = DateTimeParser::parseDate('20101331');
173 
174  }
175 
180 
181  $this->assertEquals(
182  $output,
184  );
185 
186  }
187 
191  function testBadVCardDate() {
192 
194 
195  }
196 
200  function testBadVCardTime() {
201 
202  DateTimeParser::parseVCardTime('23:12:166');
203 
204  }
205 
206  function vcardDates() {
207 
208  return [
209  [
210  "19961022T140000",
211  [
212  "year" => 1996,
213  "month" => 10,
214  "date" => 22,
215  "hour" => 14,
216  "minute" => 00,
217  "second" => 00,
218  "timezone" => null
219  ],
220  ],
221  [
222  "--1022T1400",
223  [
224  "year" => null,
225  "month" => 10,
226  "date" => 22,
227  "hour" => 14,
228  "minute" => 00,
229  "second" => null,
230  "timezone" => null
231  ],
232  ],
233  [
234  "---22T14",
235  [
236  "year" => null,
237  "month" => null,
238  "date" => 22,
239  "hour" => 14,
240  "minute" => null,
241  "second" => null,
242  "timezone" => null
243  ],
244  ],
245  [
246  "19850412",
247  [
248  "year" => 1985,
249  "month" => 4,
250  "date" => 12,
251  "hour" => null,
252  "minute" => null,
253  "second" => null,
254  "timezone" => null
255  ],
256  ],
257  [
258  "1985-04",
259  [
260  "year" => 1985,
261  "month" => 04,
262  "date" => null,
263  "hour" => null,
264  "minute" => null,
265  "second" => null,
266  "timezone" => null
267  ],
268  ],
269  [
270  "1985",
271  [
272  "year" => 1985,
273  "month" => null,
274  "date" => null,
275  "hour" => null,
276  "minute" => null,
277  "second" => null,
278  "timezone" => null
279  ],
280  ],
281  [
282  "--0412",
283  [
284  "year" => null,
285  "month" => 4,
286  "date" => 12,
287  "hour" => null,
288  "minute" => null,
289  "second" => null,
290  "timezone" => null
291  ],
292  ],
293  [
294  "---12",
295  [
296  "year" => null,
297  "month" => null,
298  "date" => 12,
299  "hour" => null,
300  "minute" => null,
301  "second" => null,
302  "timezone" => null
303  ],
304  ],
305  [
306  "T102200",
307  [
308  "year" => null,
309  "month" => null,
310  "date" => null,
311  "hour" => 10,
312  "minute" => 22,
313  "second" => 0,
314  "timezone" => null
315  ],
316  ],
317  [
318  "T1022",
319  [
320  "year" => null,
321  "month" => null,
322  "date" => null,
323  "hour" => 10,
324  "minute" => 22,
325  "second" => null,
326  "timezone" => null
327  ],
328  ],
329  [
330  "T10",
331  [
332  "year" => null,
333  "month" => null,
334  "date" => null,
335  "hour" => 10,
336  "minute" => null,
337  "second" => null,
338  "timezone" => null
339  ],
340  ],
341  [
342  "T-2200",
343  [
344  "year" => null,
345  "month" => null,
346  "date" => null,
347  "hour" => null,
348  "minute" => 22,
349  "second" => 00,
350  "timezone" => null
351  ],
352  ],
353  [
354  "T--00",
355  [
356  "year" => null,
357  "month" => null,
358  "date" => null,
359  "hour" => null,
360  "minute" => null,
361  "second" => 00,
362  "timezone" => null
363  ],
364  ],
365  [
366  "T102200Z",
367  [
368  "year" => null,
369  "month" => null,
370  "date" => null,
371  "hour" => 10,
372  "minute" => 22,
373  "second" => 00,
374  "timezone" => 'Z'
375  ],
376  ],
377  [
378  "T102200-0800",
379  [
380  "year" => null,
381  "month" => null,
382  "date" => null,
383  "hour" => 10,
384  "minute" => 22,
385  "second" => 00,
386  "timezone" => '-0800'
387  ],
388  ],
389 
390  // extended format
391  [
392  "2012-11-29T15:10:53Z",
393  [
394  "year" => 2012,
395  "month" => 11,
396  "date" => 29,
397  "hour" => 15,
398  "minute" => 10,
399  "second" => 53,
400  "timezone" => 'Z'
401  ],
402  ],
403 
404  // with milliseconds
405  [
406  "20121129T151053.123Z",
407  [
408  "year" => 2012,
409  "month" => 11,
410  "date" => 29,
411  "hour" => 15,
412  "minute" => 10,
413  "second" => 53,
414  "timezone" => 'Z'
415  ],
416  ],
417 
418  // extended format with milliseconds
419  [
420  "2012-11-29T15:10:53.123Z",
421  [
422  "year" => 2012,
423  "month" => 11,
424  "date" => 29,
425  "hour" => 15,
426  "minute" => 10,
427  "second" => 53,
428  "timezone" => 'Z'
429  ],
430  ],
431  ];
432 
433  }
434 
436 
438  '20150128',
439  [
440  'year' => '2015',
441  'month' => '01',
442  'date' => '28'
443  ]
444  );
445 
446  }
447 
449 
451  '2015-01',
452  [
453  'year' => '2015',
454  'month' => '01'
455  ]
456  );
457 
458  }
459 
461 
463  '--01',
464  [
465  'month' => '01'
466  ]
467  );
468 
469  }
470 
472 
474  '--0128',
475  [
476  'month' => '01',
477  'date' => '28'
478  ]
479  );
480 
481  }
482 
484 
486  '---28',
487  [
488  'date' => '28'
489  ]
490  );
491 
492  }
493 
495 
497  '13',
498  [
499  'hour' => '13'
500  ]
501  );
502 
503  }
504 
506 
508  '1353',
509  [
510  'hour' => '13',
511  'minute' => '53'
512  ]
513  );
514 
515  }
516 
518 
520  '135301',
521  [
522  'hour' => '13',
523  'minute' => '53',
524  'second' => '01'
525  ]
526 
527  );
528 
529  }
530 
532 
534  '-53',
535  [
536  'minute' => '53'
537  ]
538  );
539 
540  }
541 
543 
545  '-5301',
546  [
547  'minute' => '53',
548  'second' => '01'
549  ]
550  );
551 
552  }
553 
555 
556  $this->assertTrue(true);
557 
562  }
563 
565 
567  '--01Z',
568  [
569  'second' => '01',
570  'timezone' => 'Z'
571  ]
572  );
573 
574  }
575 
577 
579  '--01+1234',
580  [
581  'second' => '01',
582  'timezone' => '+1234'
583  ]
584  );
585 
586  }
587 
589 
591  '20150128T13',
592  [
593  'year' => '2015',
594  'month' => '01',
595  'date' => '28',
596  'hour' => '13'
597  ]
598  );
599 
600  }
601 
603 
605  '--0128T13',
606  [
607  'month' => '01',
608  'date' => '28',
609  'hour' => '13'
610  ]
611  );
612 
613  }
614 
616 
618  '---28T13',
619  [
620  'date' => '28',
621  'hour' => '13'
622  ]
623  );
624 
625  }
626 
628 
630  '---28T1353',
631  [
632  'date' => '28',
633  'hour' => '13',
634  'minute' => '53'
635  ]
636  );
637 
638  }
639 
641 
643  '---28T135301',
644  [
645  'date' => '28',
646  'hour' => '13',
647  'minute' => '53',
648  'second' => '01'
649  ]
650  );
651 
652  }
653 
655 
657  '---28T13Z',
658  [
659  'date' => '28',
660  'hour' => '13',
661  'timezone' => 'Z'
662  ]
663  );
664 
665  }
666 
668 
670  '---28T13+1234',
671  [
672  'date' => '28',
673  'hour' => '13',
674  'timezone' => '+1234'
675  ]
676  );
677 
678  }
679 
680  protected function assertDateAndOrTimeEqualsTo($date, $parts) {
681 
682  $this->assertSame(
684  array_merge(
685  [
686  'year' => null,
687  'month' => null,
688  'date' => null,
689  'hour' => null,
690  'minute' => null,
691  'second' => null,
692  'timezone' => null
693  ],
694  $parts
695  )
696  );
697 
698  }
699 
700 }
testVCardDate($input, $output)
vcardDates
static parse($date, $referenceTz=null)
Parses either a Date or DateTime, or Duration value.
static parseVCardTime($date)
This method parses a vCard TIME value.
static parseDateTime($dt, DateTimeZone $tz=null)
Parses an iCalendar (rfc5545) formatted datetime and returns a DateTimeImmutable object.
if($argc< 3) $input
testParseICalendarDateBadFormat()
testParseICalendarDate
testParseICalendarDateTimeGreaterThan4000()
Check if a datetime with year > 4000 will not throw an exception.
testParseICalendarDateTimeUTC()
testParseICalendarDateTime
static parseVCardDateAndOrTime($date)
This method parses a vCard date and or time value.
testParseICalendarDateTimeBadFormat()
testParseICalendarDateTime
testParseICalendarDateTimeCustomTimeZone()
testParseICalendarDateTime
testParseICalendarDateTimeInvalidTime()
testParseICalendarDateTime
static parseDate($date, DateTimeZone $tz=null)
Parses an iCalendar (rfc5545) formatted date and returns a DateTimeImmutable object.
testParseICalendarDateTimeUTC2()
testParseICalendarDateTime
static parseDuration($duration, $asString=false)
Parses an iCalendar (RFC5545) formatted duration value.
testParseICalendarDateGreaterThan4000()
TCheck if a date with year > 4000 will not throw an exception.
testParseICalendarDateInvalidDate()
testParseICalendarDate
static parseVCardDateTime($date)
This method parses a vCard date and or time value.