Aude has uploaded a new change for review.
https://gerrit.wikimedia.org/r/130827
Change subject: Fix formatting of TimeValues with month or day = '00' in
timestamp
......................................................................
Fix formatting of TimeValues with month or day = '00' in timestamp
These timestamps are considered valid, but there is an issue
in the way DateTime handles them. In formatting, timestamps a
DateTime object gets constructed in order to localise dates.
DateTime mishandles such timestamps and we need to 'normalise'
and adjust for this.
Bug: 64659
Change-Id: I8c455b02508d23e5086eff4afb4672354c44f4ac
(cherry picked from commit e977226133a8e1874627b6f7023b33b129822442)
---
M lib/includes/formatters/MwTimeIsoFormatter.php
M lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
2 files changed, 42 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/27/130827/1
diff --git a/lib/includes/formatters/MwTimeIsoFormatter.php
b/lib/includes/formatters/MwTimeIsoFormatter.php
index 8524943..43694ec 100644
--- a/lib/includes/formatters/MwTimeIsoFormatter.php
+++ b/lib/includes/formatters/MwTimeIsoFormatter.php
@@ -79,7 +79,12 @@
STR_PAD_LEFT
);
+ if ( $precision <= TimeValue::PRECISION_YEAR ) {
+ $fourDigitYearTimestamp =
$this->normaliseMwTimestampInput( $fourDigitYearTimestamp );
+ }
+
$timestamp = wfTimestamp( TS_MW, $fourDigitYearTimestamp );
+
$localisedDate = $this->language->sprintfDate(
$this->getDateFormat( $precision ),
$timestamp
@@ -104,6 +109,31 @@
}
/**
+ * Normalize so that MWTimestamp, which does new DateTime( $timestamp ),
+ * can handle timestamp strings with '00' for month and/or '00' for day.
+ * We 'round' it to '01' and '01' for formatting purposes.
+ *
+ * Without this, '+00000001995-00-00T00:00:00Z' gets becomes
'1994-11-30 00:00:00'
+ * in the DateTime object. Then '1994' != '1995' comparison in
$this->canFormatYear()
+ * fails and a timestamp is returned on failure. (see bug: 64659)
+ *
+ * @param string $fourDigitYearTimestamp
+ *
+ * @return string
+ */
+ private function normaliseMwTimestampInput( $fourDigitYearTimestamp ) {
+ if ( substr( $fourDigitYearTimestamp, 5, 2 ) === '00' ) {
+ $fourDigitYearTimestamp = substr_replace(
$fourDigitYearTimestamp, '01', 5, 2 );
+ }
+
+ if ( substr( $fourDigitYearTimestamp, 8, 2 ) === '00' ) {
+ $fourDigitYearTimestamp = substr_replace(
$fourDigitYearTimestamp, '01', 8, 2 );
+ }
+
+ return $fourDigitYearTimestamp;
+ }
+
+ /**
* @param string $date
*
* @return string
diff --git a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
index feb0913..69dacf9 100644
--- a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
+++ b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
@@ -72,6 +72,18 @@
'+00000002013-07-16T00:00:00Z',
TimeValue::PRECISION_YEAR,
),
+ '1995' => array(
+ '+00000001995-00-00T00:00:00Z',
+ TimeValue::PRECISION_YEAR,
+ ),
+ '1996' => array(
+ '+00000001996-01-00T00:00:00Z',
+ TimeValue::PRECISION_YEAR,
+ ),
+ '1997' => array(
+ '+00000001997-00-01T00:00:00Z',
+ TimeValue::PRECISION_YEAR,
+ ),
'13' => array(
'+00000000013-07-16T00:00:00Z',
TimeValue::PRECISION_YEAR,
--
To view, visit https://gerrit.wikimedia.org/r/130827
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8c455b02508d23e5086eff4afb4672354c44f4ac
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: mw1.24-wmf2
Gerrit-Owner: Aude <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits