Add 'avoidhours' option to Language#formatTimePeriod
authorJack Phoenix <ashley@uncyclomedia.co>
Sat, 4 May 2019 09:27:04 +0000 (12:27 +0300)
committerJack Phoenix <ashley@uncyclomedia.co>
Fri, 10 May 2019 08:06:00 +0000 (11:06 +0300)
Example use case: in some skins we want to show how many *days* ago a page was edited, but we don't really care about the precise _hours_.
Thus we'll set [ 'avoid' => 'avoidhours' ] when calling Language#formatTimePeriod to output something like "Page last edited 60 days ago" instead of "Page last edited 60 days 9 hours ago".

Change-Id: I0a737aab14ccb2b8d4eccdc41e1eb9232eedcb8a

RELEASE-NOTES-1.34
languages/Language.php
tests/phpunit/languages/LanguageTest.php

index 5d46edd..2b22d92 100644 (file)
@@ -38,7 +38,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 ===
 ==== New external libraries ====
index 539bdf4..17018c7 100644 (file)
@@ -4683,6 +4683,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'
@@ -4741,12 +4742,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',