jenkins-bot has submitted this change and it was merged.

Change subject: Show before and after time values in human readable form
......................................................................


Show before and after time values in human readable form

This reuses existing code. This is build on the convention that the
"before" and "after" values are meant to be read as "how many
precision intervals".

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

Approvals:
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/includes/formatters/TimeDetailsFormatter.php 
b/lib/includes/formatters/TimeDetailsFormatter.php
index 3d9e7a9..2e6a1a9 100644
--- a/lib/includes/formatters/TimeDetailsFormatter.php
+++ b/lib/includes/formatters/TimeDetailsFormatter.php
@@ -86,15 +86,15 @@
                // TODO: Provide "nice" rendering of precision, etc.
                $html .= $this->renderLabelValuePair(
                        'precision',
-                       $this->getPrecisionHtml( $value->getPrecision() )
+                       $this->getAmountAndPrecisionHtml( 
$value->getPrecision() )
                );
                $html .= $this->renderLabelValuePair(
                        'before',
-                       htmlspecialchars( $value->getBefore() )
+                       $this->getAmountAndPrecisionHtml( 
$value->getPrecision(), $value->getBefore() )
                );
                $html .= $this->renderLabelValuePair(
                        'after',
-                       htmlspecialchars( $value->getAfter() )
+                       $this->getAmountAndPrecisionHtml( 
$value->getPrecision(), $value->getAfter() )
                );
 
                $html .= Html::closeElement( 'table' );
@@ -108,10 +108,11 @@
         * @return string HTML
         */
        private function getTimezoneHtml( $timezone ) {
+               // Actual MINUS SIGN (U+2212) instead of HYPHEN-MINUS (U+002D)
                $sign = $timezone < 0 ? "\xE2\x88\x92" : '+';
                $hour = floor( abs( $timezone ) / 60 );
                $minute = abs( $timezone ) - $hour * 60;
-               return sprintf( '%s%02d:%02d', $sign, $hour, $minute );
+               return $sign . sprintf( '%02d:%02d', $hour, $minute );
        }
 
        /**
@@ -130,16 +131,12 @@
 
        /**
         * @param int $precision
+        * @param int $amount
         *
         * @return string HTML
         */
-       private function getPrecisionHtml( $precision ) {
-               if ( $precision > TimeValue::PRECISION_SECOND ) {
-                       return htmlspecialchars( $precision );
-               }
-
+       private function getAmountAndPrecisionHtml( $precision, $amount = 1 ) {
                $key = 'years';
-               $amount = 1;
 
                switch ( $precision ) {
                        case TimeValue::PRECISION_MONTH: $key = 'months'; break;
@@ -150,7 +147,13 @@
                }
 
                if ( $precision < TimeValue::PRECISION_YEAR ) {
-                       $amount = pow( 10, TimeValue::PRECISION_YEAR - 
$precision );
+                       // PRECISION_10a becomes 10 years, PRECISION_100a 
becomes 100 years, and so on.
+                       $precisionInYears = pow( 10, TimeValue::PRECISION_YEAR 
- $precision );
+                       $amount *= $precisionInYears;
+               } elseif ( $precision > TimeValue::PRECISION_SECOND ) {
+                       // Sub-second precisions become 0.1 second, 0.01 
second, and so on.
+                       $precisionInSeconds = pow( 10, $precision - 
TimeValue::PRECISION_SECOND );
+                       $amount /= $precisionInSeconds;
                }
 
                $lang = $this->getOption( ValueFormatter::OPT_LANG );
diff --git a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php 
b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
index 330d726..715eddb 100644
--- a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
+++ b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
@@ -50,8 +50,8 @@
                                                '<td[^<>]*>\+01:00</td>',
                                                '<td[^<>]*>Gregorian</td>',
                                                '<td[^<>]*>\(months: 1\)</td>',
-                                               '<td[^<>]*>0</td>',
-                                               '<td[^<>]*>1</td>',
+                                               '<td[^<>]*>\(months: 0\)</td>',
+                                               '<td[^<>]*>\(months: 1\)</td>',
                                        )
                                ) . '@s'
                        ),
@@ -75,6 +75,18 @@
                                new TimeValue( '+2001-01-01T00:00:00Z', 0, 0, 
0, TimeValue::PRECISION_Ga, $gregorian ),
                                '@.*<td[^<>]*precision">\(years: 
1000000000\)</td>.*@s'
                        ),
+                       array(
+                               new TimeValue( '+2001-01-01T00:00:00Z', 0, 2, 
0, TimeValue::PRECISION_YEAR, $gregorian ),
+                               '@.*<td[^<>]*before">\(years: 2\)</td>.*@s'
+                       ),
+                       array(
+                               new TimeValue( '+2001-01-01T00:00:00Z', 0, 0, 
5, TimeValue::PRECISION_10a, $gregorian ),
+                               '@.*<td[^<>]*after">\(years: 50\)</td>.*@s'
+                       ),
+                       array(
+                               new TimeValue( '+2001-01-01T00:00:00Z', 0, 0, 
125, $day, $gregorian ),
+                               '@.*<td[^<>]*after">\(days: 125\)</td>.*@s'
+                       ),
                );
        }
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I0bddb681840a911dfc6eef646dcb27f567999d38
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to