Addshore has uploaded a new change for review. https://gerrit.wikimedia.org/r/119038
Change subject: Support 1 2 and 3 digit years in DateTimeParser ...................................................................... Support 1 2 and 3 digit years in DateTimeParser Change-Id: I304a0b8a254dc98703a9c9edab8f18c6a729d898 --- M lib/includes/parsers/DateTimeParser.php M lib/tests/phpunit/parsers/DateTimeParserTest.php M lib/tests/phpunit/parsers/TimeParserTest.php 3 files changed, 19 insertions(+), 8 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/38/119038/1 diff --git a/lib/includes/parsers/DateTimeParser.php b/lib/includes/parsers/DateTimeParser.php index 0f0643c..f96a9b7 100644 --- a/lib/includes/parsers/DateTimeParser.php +++ b/lib/includes/parsers/DateTimeParser.php @@ -42,6 +42,7 @@ * @return TimeValue */ protected function stringParse( $value ) { + $value = trim( $value ); $calendarModelParser = new CalendarModelParser(); $options = $this->getOptions(); try{ @@ -54,7 +55,13 @@ //PHP's DateTime object does not accept spaces as separators between year, month and day, //e.g. dates like 20 12 2012, but we want to support them. //See http://de1.php.net/manual/en/datetime.formats.date.php - $value = preg_replace( '/\s+/', '.', trim( $value ) ); + $value = preg_replace( '/\s+/', '.', $value ); + + //PHP's DateTime object also cant handel smaller than 4 digit years + //e.g. instead of 12 it needs 0012 etc. + if( preg_match( '/^(.*[^\d])(\d{1,3})$/', $value, $matches ) ) { + $value = $matches[1] . str_pad( $matches[2], 4, '0', STR_PAD_LEFT ); + } //Parse using the DateTime object (this will allow us to format the date in a nicer way) //TODO try to match and remove BCE etc. before putting the value into the DateTime object to get - dates! diff --git a/lib/tests/phpunit/parsers/DateTimeParserTest.php b/lib/tests/phpunit/parsers/DateTimeParserTest.php index 67e6503..ba13d1a 100644 --- a/lib/tests/phpunit/parsers/DateTimeParserTest.php +++ b/lib/tests/phpunit/parsers/DateTimeParserTest.php @@ -46,7 +46,7 @@ $valid = array( '10/10/10' => - array( '+0000000000002010-10-10T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + array( '+0000000000000010-10-10T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '10/10/2010' => array( '+0000000000002010-10-10T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '10.10.2010' => @@ -56,17 +56,15 @@ '10/10/0010' => array( '+0000000000000010-10-10T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '1/1/1' => - array( '+0000000000002001-01-01T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + array( '+0000000000000001-01-01T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '1-1-1' => - array( '+0000000000002001-01-01T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + array( '+0000000000000001-01-01T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '1 July 2013' => array( '+0000000000002013-07-01T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '1 Jul 2013' => array( '+0000000000002013-07-01T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '9 Jan 09' => - array( '+0000000000002009-01-09T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), - '2009-01-09' => - array( '+0000000000002009-01-09T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + array( '+0000000000000009-01-09T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), 'January 9 1920' => array( '+0000000000001920-01-09T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), 'Feb 11 1930' => @@ -85,6 +83,12 @@ array( '+0000000000002013-07-03T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '4th July 2013' => array( '+0000000000002013-07-04T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + '4th July 11' => + array( '+0000000000000011-07-04T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + '4th July 111' => + array( '+0000000000000111-07-04T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + '4th July 1' => + array( '+0000000000000001-07-04T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), ); diff --git a/lib/tests/phpunit/parsers/TimeParserTest.php b/lib/tests/phpunit/parsers/TimeParserTest.php index c2cc3ba..0b861c5 100644 --- a/lib/tests/phpunit/parsers/TimeParserTest.php +++ b/lib/tests/phpunit/parsers/TimeParserTest.php @@ -75,7 +75,7 @@ //Wikibase\Lib\DateTimeParser '10/10/10' => - array( '+0000000000002010-10-10T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), + array( '+0000000000000010-10-10T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '1 July 2013' => array( '+0000000000002013-07-01T00:00:00Z', 0 , 0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ), '1 Jul 2013' => -- To view, visit https://gerrit.wikimedia.org/r/119038 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I304a0b8a254dc98703a9c9edab8f18c6a729d898 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits