[Pkg-owncloud-commits] [php-sabre-vobject] 247/341: Add the parseVCardDateAndOrTime static method.
David Prévot
taffit at moszumanska.debian.org
Tue Aug 11 13:35:53 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabre-vobject.
commit 50db424693fcd98dc2e4300e81f449e2e5fa684d
Author: Ivan Enderlin <ivan.enderlin at hoa-project.net>
Date: Thu Jan 29 10:46:46 2015 +0100
Add the parseVCardDateAndOrTime static method.
The date and or time formats mentionned in RFC6350 (vCard) are defined
in ISO.8601.2004. Unfortunately, this document is not free. However, in
RFC6351 (xCard), we have a Relax NG Schema. We use this as a reference
because date and time formats seem to be exactly the same.
---
lib/DateTimeParser.php | 122 ++++++++++++++++
tests/VObject/DateTimeParserTest.php | 270 +++++++++++++++++++++++++++++++++++
2 files changed, 392 insertions(+)
diff --git a/lib/DateTimeParser.php b/lib/DateTimeParser.php
index 7189cf8..059d810 100644
--- a/lib/DateTimeParser.php
+++ b/lib/DateTimeParser.php
@@ -438,4 +438,126 @@ class DateTimeParser {
return $result;
}
+
+ /**
+ * This method parses a vCard date and or time value.
+ *
+ * This can be used for the DATE, DATE-TIME and
+ * DATE-AND-OR-TIME value.
+ *
+ * This method returns an array, not a DateTime value.
+ * The elements in the array are in the following order:
+ * year, month, date, hour, minute, second, timezone
+ * Almost any part of the string may be omitted. It's for example legal to
+ * just specify seconds, leave out the year, etc.
+ *
+ * Timezone is either returned as 'Z' or as '+0800'
+ *
+ * For any non-specified values null is returned.
+ *
+ * List of date formats that are supported:
+ * 20150128
+ * 2015-01
+ * --01
+ * --0128
+ * ---28
+ *
+ * List of supported time formats:
+ * 13
+ * 1353
+ * 135301
+ * -53
+ * -5301
+ * --01 (unreachable, see the tests)
+ * --01Z
+ * --01+1234
+ *
+ * List of supported date-time formats:
+ * 20150128T13
+ * --0128T13
+ * ---28T13
+ * ---28T1353
+ * ---28T135301
+ * ---28T13Z
+ * ---28T13+1234
+ *
+ * See the regular expressions for all the possible patterns.
+ *
+ * Times may be postfixed by a timezone offset. This can be either 'Z' for
+ * UTC, or a string like -0500 or +1100.
+ *
+ * @param string $date
+ * @return array
+ */
+ static function parseVCardDateAndOrTime($date) {
+
+ // \d{8}|\d{4}-\d\d|--\d\d(\d\d)?|---\d\d
+ $valueDate = '/^(?J)(?:' .
+ '(?<year>\d{4})(?<month>\d\d)(?<date>\d\d)' .
+ '|(?<year>\d{4})-(?<month>\d\d)' .
+ '|--(?<month>\d\d)(?<date>\d\d)?' .
+ '|---(?<date>\d\d)' .
+ ')$/';
+
+ // (\d\d(\d\d(\d\d)?)?|-\d\d(\d\d)?|--\d\d)(Z|[+\-]\d\d(\d\d)?)?
+ $valueTime = '/^(?J)(?:' .
+ '((?<hour>\d\d)((?<minute>\d\d)(?<second>\d\d)?)?' .
+ '|-(?<minute>\d\d)(?<second>\d\d)?' .
+ '|--(?<second>\d\d))' .
+ '(?<timezone>(Z|[+\-]\d\d(\d\d)?))?' .
+ ')$/';
+
+ // (\d{8}|--\d{4}|---\d\d)T\d\d(\d\d(\d\d)?)?(Z|[+\-]\d\d(\d\d?)?
+ $valueDateTime = '/^(?:' .
+ '((?<year0>\d{4})(?<month0>\d\d)(?<date0>\d\d)' .
+ '|--(?<month1>\d\d)(?<date1>\d\d)'.
+ '|---(?<date2>\d\d))' .
+ 'T' .
+ '(?<hour>\d\d)((?<minute>\d\d)(?<second>\d\d)?)?' .
+ '(?<timezone>(Z|[+\-]\d\d(\d\d?)))?' .
+ ')$/';
+
+ // date-and-or-time is date | date-time | time
+ // in this strict order.
+
+ if ( 0 === preg_match($valueDate, $date, $matches)
+ && 0 === preg_match($valueDateTime, $date, $matches)
+ && 0 === preg_match($valueTime, $date, $matches)) {
+ throw new InvalidArgumentException('Invalid vCard date-time string: ' . $date);
+ }
+
+ $parts = [
+ 'year' => null,
+ 'month' => null,
+ 'date' => null,
+ 'hour' => null,
+ 'minute' => null,
+ 'second' => null,
+ 'timezone' => null
+ ];
+
+ // The $valueDateTime expression has a bug with (?J) so we simulate it.
+ $parts['date0'] = &$parts['date'];
+ $parts['date1'] = &$parts['date'];
+ $parts['date2'] = &$parts['date'];
+ $parts['month0'] = &$parts['month'];
+ $parts['month1'] = &$parts['month'];
+ $parts['year0'] = &$parts['year'];
+
+ foreach ($parts as $part => &$value) {
+ if (!empty($matches[$part])) {
+ $value = $matches[$part];
+ }
+ }
+
+ unset($parts['date0']);
+ unset($parts['date1']);
+ unset($parts['date2']);
+ unset($parts['month0']);
+ unset($parts['month1']);
+ unset($parts['year0']);
+
+ return $parts;
+
+ }
}
diff --git a/tests/VObject/DateTimeParserTest.php b/tests/VObject/DateTimeParserTest.php
index 952bee4..4cea812 100644
--- a/tests/VObject/DateTimeParserTest.php
+++ b/tests/VObject/DateTimeParserTest.php
@@ -385,5 +385,275 @@ class DateTimeParserTest extends \PHPUnit_Framework_TestCase {
}
+ function testDateAndOrTime_DateWithYearMonthDay() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '20150128',
+ [
+ 'year' => '2015',
+ 'month' => '01',
+ 'date' => '28'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateWithYearMonth() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '2015-01',
+ [
+ 'year' => '2015',
+ 'month' => '01'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateWithMonth() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '--01',
+ [
+ 'month' => '01'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateWithMonthDay() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '--0128',
+ [
+ 'month' => '01',
+ 'date' => '28'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateWithDay() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '---28',
+ [
+ 'date' => '28'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_TimeWithHour() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '13',
+ [
+ 'hour' => '13'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_TimeWithHourMinute() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '1353',
+ [
+ 'hour' => '13',
+ 'minute' => '53'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_TimeWithHourSecond() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '135301',
+ [
+ 'hour' => '13',
+ 'minute' => '53',
+ 'second' => '01'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_TimeWithMinute() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '-53',
+ [
+ 'minute' => '53'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_TimeWithMinuteSecond() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '-5301',
+ [
+ 'minute' => '53',
+ 'second' => '01'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_TimeWithSecond() {
+
+ $this->assertTrue(true);
+
+ /**
+ * This is unreachable due to a conflict between date and time pattern.
+ * This is an error in the specification, not in the our implementation.
+ $this->assertDateAndOrTimeEqualsTo(
+ '--01',
+ [
+ 'second' => '01'
+ ]
+ );
+ */
+
+ }
+
+ function testDateAndOrTime_TimeWithSecondZ() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '--01Z',
+ [
+ 'second' => '01',
+ 'timezone' => 'Z'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_TimeWithSecondTZ() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '--01+1234',
+ [
+ 'second' => '01',
+ 'timezone' => '+1234'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateTimeWithYearMonthDayHour() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '20150128T13',
+ [
+ 'year' => '2015',
+ 'month' => '01',
+ 'date' => '28',
+ 'hour' => '13'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateTimeWithMonthDayHour() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '--0128T13',
+ [
+ 'month' => '01',
+ 'date' => '28',
+ 'hour' => '13'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateTimeWithDayHour() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '---28T13',
+ [
+ 'date' => '28',
+ 'hour' => '13'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateTimeWithDayHourMinute() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '---28T1353',
+ [
+ 'date' => '28',
+ 'hour' => '13',
+ 'minute' => '53'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateTimeWithDayHourMinuteSecond() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '---28T135301',
+ [
+ 'date' => '28',
+ 'hour' => '13',
+ 'minute' => '53',
+ 'second' => '01'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateTimeWithDayHourZ() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '---28T13Z',
+ [
+ 'date' => '28',
+ 'hour' => '13',
+ 'timezone' => 'Z'
+ ]
+ );
+
+ }
+
+ function testDateAndOrTime_DateTimeWithDayHourTZ() {
+
+ $this->assertDateAndOrTimeEqualsTo(
+ '---28T13+1234',
+ [
+ 'date' => '28',
+ 'hour' => '13',
+ 'timezone' => '+1234'
+ ]
+ );
+
+ }
+
+ protected function assertDateAndOrTimeEqualsTo($date, $parts) {
+
+ $this->assertSame(
+ DateTimeParser::parseVCardDateAndOrTime($date),
+ array_merge(
+ [
+ 'year' => null,
+ 'month' => null,
+ 'date' => null,
+ 'hour' => null,
+ 'minute' => null,
+ 'second' => null,
+ 'timezone' => null
+ ],
+ $parts
+ )
+ );
+
+ }
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git
More information about the Pkg-owncloud-commits
mailing list