From 0e52aee608efba794154a21fd0d74b2c03c1947c Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Sat, 24 Sep 2011 15:44:43 +0000 Subject: [PATCH] Per CR on r97962, introduce an array parameter for formatTimePeriod() rather than adding more boolean params. --- languages/Language.php | 37 +++++++++----- tests/phpunit/languages/LanguageTest.php | 64 ++++++++++++------------ 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index b2ad81915e..c262bf18d2 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -3549,17 +3549,28 @@ class Language { /** * @todo Document * @param $seconds int|float - * @param $format String Optional, one of ("avoidseconds","avoidminutes"): - * "avoidseconds" - don't mention seconds if $seconds >= 1 hour - * "avoidminutes" - don't mention seconds/minutes if $seconds > 48 hours - * @param $noAbbrevs Bool If true (or true-ish, recommend using 'noabbrevs' for clarity), use 'seconds' and friends instead of 'seconds-abbrev' and friends + * @param $format Array Optional + * If $format['avoid'] == 'avoidseconds' - don't mention seconds if $seconds >= 1 hour + * If $format['avoid'] == 'avoidminutes' - don't mention seconds/minutes if $seconds > 48 hours + * If $format['noabbrevs'] is true - use 'seconds' and friends instead of 'seconds-abbrev' and friends + * For backwards compatibility, $format may also be one of the strings 'avoidseconds' or 'avoidminutes' * @return string */ - function formatTimePeriod( $seconds, $format = false, $noAbbrevs = false ) { - $secondsMsg = wfMessage( $noAbbrevs ? 'seconds' : 'seconds-abbrev' )->inLanguage( $this ); - $minutesMsg = wfMessage( $noAbbrevs ? 'minutes' : 'minutes-abbrev' )->inLanguage( $this ); - $hoursMsg = wfMessage( $noAbbrevs ? 'hours' : 'hours-abbrev' )->inLanguage( $this ); - $daysMsg = wfMessage( $noAbbrevs ? 'days' : 'days-abbrev' )->inLanguage( $this ); + function formatTimePeriod( $seconds, $format = array() ) { + if ( !is_array( $format ) ) { + $format = array( 'avoid' => $format, 'noabbrevs' => false ); // For backwards compatibility + } + if ( !isset( $format['avoid'] ) ) { + $format['avoid'] = false; + } + if ( !isset( $format['noabbrevs' ] ) ) { + $format['noabbrevs'] = false; + } + $secondsMsg = wfMessage( $format['noabbrevs'] ? 'seconds' : 'seconds-abbrev' )->inLanguage( $this ); + $minutesMsg = wfMessage( $format['noabbrevs'] ? 'minutes' : 'minutes-abbrev' )->inLanguage( $this ); + $hoursMsg = wfMessage( $format['noabbrevs'] ? 'hours' : 'hours-abbrev' )->inLanguage( $this ); + $daysMsg = wfMessage( $format['noabbrevs'] ? 'days' : 'days-abbrev' )->inLanguage( $this ); + if ( round( $seconds * 10 ) < 100 ) { $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ); $s = $secondsMsg->params( $s )->text(); @@ -3591,12 +3602,12 @@ class Language { $s = $hoursMsg->params( $this->formatNum( $hours ) )->text(); $s .= ' '; $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text(); - if ( !in_array( $format, array( 'avoidseconds', 'avoidminutes' ) ) ) { + if ( !in_array( $format['avoid'], array( 'avoidseconds', 'avoidminutes' ) ) ) { $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text(); } } else { $days = floor( $seconds / 86400 ); - if ( $format === 'avoidminutes' ) { + if ( $format['avoid'] === 'avoidminutes' ) { $hours = round( ( $seconds - $days * 86400 ) / 3600 ); if ( $hours == 24 ) { $hours = 0; @@ -3605,7 +3616,7 @@ class Language { $s = $daysMsg->params( $this->formatNum( $days ) )->text(); $s .= ' '; $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text(); - } elseif ( $format === 'avoidseconds' ) { + } elseif ( $format['avoid'] === 'avoidseconds' ) { $hours = floor( ( $seconds - $days * 86400 ) / 3600 ); $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 ); if ( $minutes == 60 ) { @@ -3624,7 +3635,7 @@ class Language { } else { $s = $daysMsg->params( $this->formatNum( $days ) )->text(); $s .= ' '; - $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format, $noAbbrevs ); + $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format ); } } return $s; diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index a0272847af..a32a5fdde4 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -21,42 +21,42 @@ class LanguageTest extends MediaWikiTestCase { } /** @dataProvider provideFormattableTimes */ - function testFormatTimePeriod( $seconds, $avoid, $noAbbrevs, $expected, $desc ) { - $this->assertEquals( $expected, $this->lang->formatTimePeriod( $seconds, $avoid, $noAbbrevs ), $desc ); + function testFormatTimePeriod( $seconds, $format, $expected, $desc ) { + $this->assertEquals( $expected, $this->lang->formatTimePeriod( $seconds, $format ), $desc ); } function provideFormattableTimes() { return array( - array( 9.45, false, false, '9.5s', 'formatTimePeriod() rounding (<10s)' ), - array( 9.45, false, 'noabbrevs', '9.5 seconds', 'formatTimePeriod() rounding (<10s)' ), - array( 9.95, false, false, '10s', 'formatTimePeriod() rounding (<10s)' ), - array( 9.95, false, 'noabbrevs', '10 seconds', 'formatTimePeriod() rounding (<10s)' ), - array( 59.55, false, false, '1m 0s', 'formatTimePeriod() rounding (<60s)' ), - array( 59.55, false, 'noabbrevs', '1 minute 0 seconds', 'formatTimePeriod() rounding (<60s)' ), - array( 119.55, false, false, '2m 0s', 'formatTimePeriod() rounding (<1h)' ), - array( 119.55, false, 'noabbrevs', '2 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ), - array( 3599.55, false, false, '1h 0m 0s', 'formatTimePeriod() rounding (<1h)' ), - array( 3599.55, false, 'noabbrevs', '1 hour 0 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ), - array( 7199.55, false, false, '2h 0m 0s', 'formatTimePeriod() rounding (>=1h)' ), - array( 7199.55, false, 'noabbrevs', '2 hours 0 minutes 0 seconds', 'formatTimePeriod() rounding (>=1h)' ), - array( 7199.55, 'avoidseconds', false, '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidseconds' ), - array( 7199.55, 'avoidseconds', 'noabbrevs', '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidseconds' ), - array( 7199.55, 'avoidminutes', false, '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidminutes' ), - array( 7199.55, 'avoidminutes', 'noabbrevs', '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidminutes' ), - array( 172799.55, 'avoidseconds', false, '48h 0m', 'formatTimePeriod() rounding (=48h), avoidseconds' ), - array( 172799.55, 'avoidseconds', 'noabbrevs', '48 hours 0 minutes', 'formatTimePeriod() rounding (=48h), avoidseconds' ), - array( 259199.55, 'avoidminutes', false, '3d 0h', 'formatTimePeriod() rounding (>48h), avoidminutes' ), - array( 259199.55, 'avoidminutes', 'noabbrevs', '3 days 0 hours', 'formatTimePeriod() rounding (>48h), avoidminutes' ), - array( 176399.55, 'avoidseconds', false, '2d 1h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ), - array( 176399.55, 'avoidseconds', 'noabbrevs', '2 days 1 hour 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ), - array( 176399.55, 'avoidminutes', false, '2d 1h', 'formatTimePeriod() rounding (>48h), avoidminutes' ), - array( 176399.55, 'avoidminutes', 'noabbrevs', '2 days 1 hour', 'formatTimePeriod() rounding (>48h), avoidminutes' ), - array( 259199.55, 'avoidseconds', false, '3d 0h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ), - array( 259199.55, 'avoidseconds', 'noabbrevs', '3 days 0 hours 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ), - array( 172801.55, 'avoidseconds', false, '2d 0h 0m', 'formatTimePeriod() rounding, (>48h), avoidseconds' ), - array( 172801.55, 'avoidseconds', 'noabbrevs', '2 days 0 hours 0 minutes', 'formatTimePeriod() rounding, (>48h), avoidseconds' ), - array( 176460.55, false, false, '2d 1h 1m 1s', 'formatTimePeriod() rounding, recursion, (>48h)' ), - array( 176460.55, false, 'noabbrevs', '2 days 1 hour 1 minute 1 second', 'formatTimePeriod() rounding, recursion, (>48h)' ), + array( 9.45, array(), '9.5s', 'formatTimePeriod() rounding (<10s)' ), + array( 9.45, array( 'noabbrevs' => true ), '9.5 seconds', 'formatTimePeriod() rounding (<10s)' ), + array( 9.95, array(), '10s', 'formatTimePeriod() rounding (<10s)' ), + array( 9.95, array( 'noabbrevs' => true ), '10 seconds', 'formatTimePeriod() rounding (<10s)' ), + array( 59.55, array(), '1m 0s', 'formatTimePeriod() rounding (<60s)' ), + array( 59.55, array( 'noabbrevs' => true ), '1 minute 0 seconds', 'formatTimePeriod() rounding (<60s)' ), + array( 119.55, array(), '2m 0s', 'formatTimePeriod() rounding (<1h)' ), + array( 119.55, array( 'noabbrevs' => true ), '2 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ), + array( 3599.55, array(), '1h 0m 0s', 'formatTimePeriod() rounding (<1h)' ), + array( 3599.55, array( 'noabbrevs' => true ), '1 hour 0 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ), + array( 7199.55, array(), '2h 0m 0s', 'formatTimePeriod() rounding (>=1h)' ), + array( 7199.55, array( 'noabbrevs' => true ), '2 hours 0 minutes 0 seconds', 'formatTimePeriod() rounding (>=1h)' ), + array( 7199.55, 'avoidseconds', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidseconds' ), + array( 7199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidseconds' ), + array( 7199.55, 'avoidminutes', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidminutes' ), + array( 7199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidminutes' ), + array( 172799.55, 'avoidseconds', '48h 0m', 'formatTimePeriod() rounding (=48h), avoidseconds' ), + array( 172799.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '48 hours 0 minutes', 'formatTimePeriod() rounding (=48h), avoidseconds' ), + array( 259199.55, 'avoidminutes', '3d 0h', 'formatTimePeriod() rounding (>48h), avoidminutes' ), + array( 259199.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '3 days 0 hours', 'formatTimePeriod() rounding (>48h), avoidminutes' ), + array( 176399.55, 'avoidseconds', '2d 1h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ), + array( 176399.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 1 hour 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ), + array( 176399.55, 'avoidminutes', '2d 1h', 'formatTimePeriod() rounding (>48h), avoidminutes' ), + array( 176399.55, array( 'avoid' => 'avoidminutes', 'noabbrevs' => true ), '2 days 1 hour', 'formatTimePeriod() rounding (>48h), avoidminutes' ), + array( 259199.55, 'avoidseconds', '3d 0h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ), + array( 259199.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '3 days 0 hours 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ), + array( 172801.55, 'avoidseconds', '2d 0h 0m', 'formatTimePeriod() rounding, (>48h), avoidseconds' ), + array( 172801.55, array( 'avoid' => 'avoidseconds', 'noabbrevs' => true ), '2 days 0 hours 0 minutes', 'formatTimePeriod() rounding, (>48h), avoidseconds' ), + array( 176460.55, array(), '2d 1h 1m 1s', 'formatTimePeriod() rounding, recursion, (>48h)' ), + array( 176460.55, array( 'noabbrevs' => true ), '2 days 1 hour 1 minute 1 second', 'formatTimePeriod() rounding, recursion, (>48h)' ), ); } -- 2.20.1