Merge "Add 'avoidhours' option to Language#formatTimePeriod"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 10 May 2019 16:59:58 +0000 (16:59 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 10 May 2019 16:59:58 +0000 (16:59 +0000)
RELEASE-NOTES-1.34
languages/Language.php
tests/phpunit/languages/LanguageTest.php

index 786dae2..7738adb 100644 (file)
@@ -39,7 +39,8 @@ For notes on 1.33.x and older releases, see HISTORY.
 * …
 
 === New developer features in 1.34 ===
-* …
+* Language::formatTimePeriod now supports the new 'avoidhours' option to output
+  strings like "5 days ago" instead of "5 days 13 hours ago".
 
 === External library changes in 1.34 ===
 
index 3a12439..2262fa7 100644 (file)
@@ -4658,6 +4658,7 @@ class Language {
         *
         * @param int|float $seconds
         * @param array $format An optional argument that formats the returned string in different ways:
+        *   If $format['avoid'] === 'avoidhours': don't show hours, just show days
         *   If $format['avoid'] === 'avoidseconds': don't show seconds if $seconds >= 1 hour,
         *   If $format['avoid'] === 'avoidminutes': don't show seconds/minutes if $seconds > 48 hours,
         *   If $format['noabbrevs'] is true: use 'seconds' and friends instead of 'seconds-abbrev'
@@ -4716,12 +4717,19 @@ class Language {
                        $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
                        $s .= ' ';
                        $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
-                       if ( !in_array( $format['avoid'], [ 'avoidseconds', 'avoidminutes' ] ) ) {
+                       if ( !in_array( $format['avoid'], [ 'avoidseconds', 'avoidminutes', 'avoidhours' ] ) ) {
                                $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
                        }
                } else {
                        $days = floor( $seconds / 86400 );
-                       if ( $format['avoid'] === 'avoidminutes' ) {
+                       if ( $format['avoid'] === 'avoidhours' ) {
+                               $hours = round( ( $seconds - $days * 86400 ) / 3600 );
+                               if ( $hours == 24 ) {
+                                       $hours = 0;
+                                       $days++;
+                               }
+                               $s = $daysMsg->params( $this->formatNum( $days ) )->text();
+                       } elseif ( $format['avoid'] === 'avoidminutes' ) {
                                $hours = round( ( $seconds - $days * 86400 ) / 3600 );
                                if ( $hours == 24 ) {
                                        $hours = 0;
index b9b8306..2f6fa39 100644 (file)
@@ -135,6 +135,18 @@ class LanguageTest extends LanguageClassesTestCase {
                                '48 hours 0 minutes',
                                'formatTimePeriod() rounding (=48h), avoidseconds'
                        ],
+                       [
+                               259199.55,
+                               'avoidhours',
+                               '3 d',
+                               'formatTimePeriod() rounding (>48h), avoidhours'
+                       ],
+                       [
+                               259199.55,
+                               [ 'avoid' => 'avoidhours', 'noabbrevs' => true ],
+                               '3 days',
+                               'formatTimePeriod() rounding (>48h), avoidhours'
+                       ],
                        [
                                259199.55,
                                'avoidminutes',