Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/191640

Change subject: Always pad year to 4 digits in DateTime diffs
......................................................................

Always pad year to 4 digits in DateTime diffs

Change-Id: I6f017772fb30324d3039cf89ead3d840e3a4bf0a
---
M lib/includes/formatters/TimeDetailsFormatter.php
M lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
2 files changed, 12 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/40/191640/1

diff --git a/lib/includes/formatters/TimeDetailsFormatter.php 
b/lib/includes/formatters/TimeDetailsFormatter.php
index 95d9538..c733ac2 100644
--- a/lib/includes/formatters/TimeDetailsFormatter.php
+++ b/lib/includes/formatters/TimeDetailsFormatter.php
@@ -96,12 +96,19 @@
        }
 
        /**
-        * @param string $time
+        * @param string $t^ime
         *
         * @return string HTML
         */
        private function getTimeHtml( $time ) {
-               return preg_replace( '/(?<=\d)Z$/i', '', $time );
+               // Loose check if the "ISO" string contains at least year, 
month, day and hour.
+               if ( !preg_match( '/^([-+])(\d+)(-\d+-\d+T\d+(?::\d+)*?)Z$/i', 
$time, $matches ) ) {
+                       return $time;
+               }
+
+               // Warning, never cast the year to integer to not run into 
32-bit integer overflows!
+               $year = ltrim( $matches[2], '0' );
+               return $matches[1] . str_pad( $year, 4, '0', STR_PAD_LEFT ) . 
$matches[3];
        }
 
        /**
diff --git a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php 
b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
index b522815..2cd60e1 100644
--- a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
+++ b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
@@ -57,11 +57,11 @@
                        ),
                        array(
                                new TimeValue( '+999-01-01T00:00:00Z', 0, 0, 0, 
$day, $gregorian ),
-                               
'@.*<td[^<>]*isotime">\+999-01-01T00:00:00</td>.*@s'
+                               
'@.*<td[^<>]*isotime">\+0999-01-01T00:00:00</td>.*@s'
                        ),
                        array(
-                               new TimeValue( '+099999-01-01T00:00:00Z', 0, 0, 
0, $day, $gregorian ),
-                               
'@.*<td[^<>]*isotime">\+099999-01-01T00:00:00</td>.*@s'
+                               new TimeValue( '-099999-01-01T00:00:00Z', 0, 0, 
0, $day, $gregorian ),
+                               
'@.*<td[^<>]*isotime">-99999-01-01T00:00:00</td>.*@s'
                        ),
                        array(
                                new TimeValue( '+2001-01-01T00:00:00Z', 0, 0, 
0, $day, TimeFormatter::CALENDAR_JULIAN ),

-- 
To view, visit https://gerrit.wikimedia.org/r/191640
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6f017772fb30324d3039cf89ead3d840e3a4bf0a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to