From b704c9f4de840f17a5e6222a82f56e2436e3eef8 Mon Sep 17 00:00:00 2001 From: Jack Phoenix Date: Sat, 4 May 2019 12:27:04 +0300 Subject: [PATCH] Add 'avoidhours' option to Language#formatTimePeriod 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 | 3 ++- languages/Language.php | 12 ++++++++++-- tests/phpunit/languages/LanguageTest.php | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/RELEASE-NOTES-1.34 b/RELEASE-NOTES-1.34 index 5d46edd8c2..2b22d92bb8 100644 --- a/RELEASE-NOTES-1.34 +++ b/RELEASE-NOTES-1.34 @@ -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 ==== diff --git a/languages/Language.php b/languages/Language.php index 539bdf4547..17018c7d36 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -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; diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index b9b8306e0c..2f6fa39b36 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -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', -- 2.20.1