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

Change subject: Also Format 10years+ precisions
......................................................................


Also Format 10years+ precisions

Change-Id: Icb6dbc87aa01743773dbf623d54983a5779de55a
---
M lib/includes/formatters/MwTimeIsoFormatter.php
M lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
2 files changed, 181 insertions(+), 41 deletions(-)

Approvals:
  WikidataJenkins: Verified
  Adrian Lang: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/includes/formatters/MwTimeIsoFormatter.php 
b/lib/includes/formatters/MwTimeIsoFormatter.php
index af11674..52031c6 100644
--- a/lib/includes/formatters/MwTimeIsoFormatter.php
+++ b/lib/includes/formatters/MwTimeIsoFormatter.php
@@ -10,7 +10,6 @@
 use ValueFormatters\ValueFormatterBase;
 
 /**
- *
  * @since 0.4
  *
  * @licence GNU GPL v2+
@@ -56,32 +55,58 @@
                 * $matches for +00000002013-07-16T01:02:03Z
                 * [0] => +00000002013-07-16T00:00:00Z
                 * [1] => +
-                * [2] => 0000000
-                * [3] => 2013
-                * [4] => 07
-                * [5] => 16
-                * [6] => 01
-                * [7] => 02
-                * [8] => 03
+                * [2] => 00000002013
+                * [3] => 0000000
+                * [4] => 2013
+                * [5] => 07
+                * [6] => 16
+                * [7] => 01
+                * [8] => 02
+                * [9] => 03
                 */
-               $regexSuccess = preg_match( 
'/^(\+|\-)(\d{7})?(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/',
+               $regexSuccess = preg_match( 
'/^(\+|\-)((\d{7})?(\d{4}))-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/',
                        $extendedIsoTimestamp, $matches );
 
-               //TODO: format values with - or more precise than a year
-               if( !$regexSuccess || $matches[1] === '-' || $precision < 
TimeValue::PRECISION_YEAR ) {
+               //TODO: format values with -
+               if( !$regexSuccess || $matches[1] === '-') {
                        return $extendedIsoTimestamp;
                }
 
                // Positive 4-digit year allows using Language object.
-               $fourDigitYearTimestamp = str_replace( '+' . $matches[2], '', 
$extendedIsoTimestamp );
+               $fourDigitYearTimestamp = str_replace( '+' . $matches[3], '', 
$extendedIsoTimestamp );
                $timestamp = wfTimestamp( TS_MW, $fourDigitYearTimestamp );
+
+               $localisedDate = $this->language->sprintfDate(
+                       $this->getDateFormat( $precision ),
+                       $timestamp
+               );
+
+               //If we cant reliably fix the year return the full timestamp,
+               //  this should never happen as sprintfDate should always 
return a 4 digit year
+               if( substr_count( $localisedDate, $matches[4] ) !== 1 ) {
+                       return $extendedIsoTimestamp;
+               }
+
+               $localisedDate = str_replace(
+                       $matches[4],
+                       $this->formatYear( $matches[2], $precision ),
+                       $localisedDate
+               );
+
+               return $localisedDate;
+       }
+
+       /**
+        * Get the dateformat string for the given precision to be used by 
sprintfDate
+        * @param integer $precision
+        * @return string dateFormat to be used by sprintfDate
+        */
+       private function getDateFormat( $precision ) {
                $dateFormat = $this->language->getDateFormatString(
                        'date',
                        $this->language->getDefaultDateFormat()
                );
 
-               // TODO: Implement more sophisticated replace algorithm since 
characters may be escaped
-               //  or, even better, find a way to avoid having to do 
replacements.
                if( $precision < TimeValue::PRECISION_DAY ) {
                        // Remove day placeholder:
                        $dateFormat = preg_replace( '/((x\w{1})?(j|t)|d)/', '', 
$dateFormat );
@@ -91,26 +116,79 @@
                        // Remove month placeholder:
                        $dateFormat = preg_replace( '/((x\w{1})?(F|n)|m)/', '', 
$dateFormat );
                }
+               return trim( $dateFormat );
+       }
 
-               $localisedDate = $this->language->sprintfDate( trim( 
$dateFormat ), $timestamp );
+       /**
+        * @param string $fullYear
+        * @param integer $precision
+        *
+        * @return string the formatted year
+        */
+       private function formatYear( $fullYear, $precision ) {
+               $beforeYear = '';
+               $afterYear = '';
 
-               //If we cant reliably fix the year return the full timestamp,
-               //  this should never happen as sprintfDate should always 
return a 4 digit year
-               if( substr_count( $localisedDate, $matches[3] ) !== 1 ) {
-                       return $extendedIsoTimestamp;
+               //todo i18n below
+               switch( $precision ) {
+                       case TimeValue::PRECISION_Ga:
+                               $fullYear = round( $fullYear, -9 );
+                               $fullYear = substr( $fullYear, 0, -9 );
+                               $beforeYear = 'in ';
+                               $afterYear = ' billion years';
+                               break;
+                       case TimeValue::PRECISION_100Ma:
+                               $fullYear = round( $fullYear, -8 );
+                               $fullYear = substr( $fullYear, 0, -6 );
+                               $beforeYear = 'in ';
+                               $afterYear = ' million years';
+                               break;
+                       case TimeValue::PRECISION_10Ma:
+                               $fullYear = round( $fullYear, -7 );
+                               $fullYear = substr( $fullYear, 0, -6 );
+                               $beforeYear = 'in ';
+                               $afterYear = ' million years';
+                               break;
+                       case TimeValue::PRECISION_Ma:
+                               $fullYear = round( $fullYear, -6 );
+                               $fullYear = substr( $fullYear, 0, -6 );
+                               $beforeYear = 'in ';
+                               $afterYear = ' million years';
+                               break;
+                       case TimeValue::PRECISION_100ka:
+                               $fullYear = round( $fullYear, -5 );
+                               $beforeYear = 'in ';
+                               $afterYear = ' years';
+                               break;
+                       case TimeValue::PRECISION_10ka:
+                               $fullYear = round( $fullYear, -4 );
+                               $beforeYear = 'in ';
+                               $afterYear = ' years';
+                               break;
+                       case TimeValue::PRECISION_ka:
+                               $fullYear = round( $fullYear, -3 );
+                               $fullYear = substr( $fullYear, 0, -3 );
+                               $afterYear = '.millennium';
+                               break;
+                       case TimeValue::PRECISION_100a:
+                               $fullYear = round( $fullYear, -2 );
+                               $fullYear = substr( $fullYear, 0, -2 );
+                               $afterYear = '.century';
+                               break;
+                       case TimeValue::PRECISION_10a:
+                               $fullYear = round( $fullYear, -1 );
+                               $afterYear = 's';
+                               break;
+                       default:
+                               //If not one of the above make sure the year 
have at least 4 digits
+                               $fullYear = ltrim( $fullYear, '0' );
+                               $fullYearLength = strlen( $fullYear );
+                               if( $fullYearLength < 4 ) {
+                                       $fullYear = str_repeat( '0', 4 - 
$fullYearLength ) . $fullYear;
+                               }
+                               break;
                }
-
-               //todo optional trimming through options?
-               $fullYear = $matches[2] . $matches[3];
-               $fullYear = ltrim( $fullYear, '0' );
-               $fullYearLength = strlen( $fullYear );
-               if( $fullYearLength < 4 ) {
-                       $fullYear = str_repeat( '0', 4 - $fullYearLength ) . 
$fullYear;
-               }
-
-               $localisedDate = str_replace( $matches[3], $fullYear, 
$localisedDate );
-
-               return $localisedDate;
+               return $beforeYear . $fullYear . $afterYear;
        }
 
 }
diff --git a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php 
b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
index a24a8a2..cfc36ab 100644
--- a/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
+++ b/lib/tests/phpunit/formatters/MwTimeIsoFormatterTest.php
@@ -10,8 +10,6 @@
 /**
  * @covers ValueFormatters\TimeFormatter
  *
- * @since 0.4
- *
  * @group ValueFormatters
  * @group DataValueExtensions
  * @group WikibaseLib
@@ -25,8 +23,6 @@
 
        /**
         * Returns an array of test parameters.
-        *
-        * @since 0.4
         *
         * @return array
         */
@@ -68,12 +64,80 @@
                                '+12342222013-07-16T00:10:00Z',
                                TimeValue::PRECISION_YEAR,
                        ),
-
-                       //The below still return the full timestamp
-                       '+00000002013-07-16T00:00:00Z' => array(
-                               '+00000002013-07-16T00:00:00Z',
+                       //stepping through precisions
+                       '12345678910s' => array(
+                               '+12345678912-01-01T01:01:01Z',
                                TimeValue::PRECISION_10a,
                        ),
+                       '12345678920s' => array(
+                               '+12345678919-01-01T01:01:01Z',
+                               TimeValue::PRECISION_10a,
+                       ),
+                       '123456789.century' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_100a,
+                       ),
+                       '123456790.century' => array(
+                               '+12345678992-01-01T01:01:01Z',
+                               TimeValue::PRECISION_100a,
+                       ),
+                       '12345678.millennium' => array(
+                               '+12345678112-01-01T01:01:01Z',
+                               TimeValue::PRECISION_ka,
+                       ),
+                       '12345679.millennium' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_ka,
+                       ),
+                       'in 12345670000 years' => array(
+                               '+12345671912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_10ka,
+                       ),
+                       'in 12345680000 years' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_10ka,
+                       ),
+                       'in 12345600000 years' => array(
+                               '+12345618912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_100ka,
+                       ),
+                       'in 12345700000 years' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_100ka,
+                       ),
+                       'in 12345 million years' => array(
+                               '+12345178912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_Ma,
+                       ),
+                       'in 12346 million years' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_Ma,
+                       ),
+                       'in 12340 million years' => array(
+                               '+12341678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_10Ma,
+                       ),
+                       'in 12350 million years' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_10Ma,
+                       ),
+                       'in 12300 million years' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_100Ma,
+                       ),
+                       'in 12400 million years' => array(
+                               '+12375678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_100Ma,
+                       ),
+                       'in 12 billion years' => array(
+                               '+12345678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_Ga,
+                       ),
+                       'in 13 billion years' => array(
+                               '+12545678912-01-01T01:01:01Z',
+                               TimeValue::PRECISION_Ga,
+                       ),
+                       //The below still return the full timestamp
                        '-00000000001-01-01T00:00:00Z' => array(
                                '-00000000001-01-01T00:00:00Z',
                                TimeValue::PRECISION_DAY,
@@ -91,8 +155,6 @@
 
        /**
         * @dataProvider formatDateProvider
-        *
-        * @since 0.4
         *
         * @param string $expected
         * @param string $extendedIsoString

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Icb6dbc87aa01743773dbf623d54983a5779de55a
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Adrian Lang <adrian.l...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de>
Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to