From 7646fa870c605faa38ce950e83ae4af18ab37a50 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Sun, 7 Nov 2010 21:05:01 +0000 Subject: [PATCH] Follow-up r65654: * Per Hashar: added test cases * Added some more round() to be really correct --- languages/Language.php | 8 ++-- .../tests/phpunit/languages/LanguageTest.php | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index 9a1a302a8a..73acd36523 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -2873,11 +2873,11 @@ class Language { } function formatTimePeriod( $seconds ) { - if ( $seconds < 10 ) { - return $this->formatNum( sprintf( "%.1f", $seconds ) ) . $this->getMessageFromDB( 'seconds-abbrev' ); - } elseif ( $seconds < 60 ) { + if ( round( $seconds * 10 ) < 100 ) { + return $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ) . $this->getMessageFromDB( 'seconds-abbrev' ); + } elseif ( round( $seconds ) < 60 ) { return $this->formatNum( round( $seconds ) ) . $this->getMessageFromDB( 'seconds-abbrev' ); - } elseif ( $seconds < 3600 ) { + } elseif ( round( $seconds ) < 3600 ) { $minutes = floor( $seconds / 60 ); $secondsPart = round( fmod( $seconds, 60 ) ); if ( $secondsPart == 60 ) { diff --git a/maintenance/tests/phpunit/languages/LanguageTest.php b/maintenance/tests/phpunit/languages/LanguageTest.php index f27df96edd..8e30259b17 100644 --- a/maintenance/tests/phpunit/languages/LanguageTest.php +++ b/maintenance/tests/phpunit/languages/LanguageTest.php @@ -20,4 +20,42 @@ class LanguageTest extends MediaWikiTestSetup { 'convertDoubleWidth() with the full alphabet and digits' ); } + + function testFormatTimePeriod() { + $this->assertEquals( + "9.5s", + $this->lang->formatTimePeriod( 9.45 ), + 'formatTimePeriod() rounding (<10s)' + ); + + $this->assertEquals( + "10s", + $this->lang->formatTimePeriod( 9.95 ), + 'formatTimePeriod() rounding (<10s)' + ); + + $this->assertEquals( + "1m 0s", + $this->lang->formatTimePeriod( 59.55 ), + 'formatTimePeriod() rounding (<60s)' + ); + + $this->assertEquals( + "2m 0s", + $this->lang->formatTimePeriod( 119.55 ), + 'formatTimePeriod() rounding (<1h)' + ); + + $this->assertEquals( + "1h 0m 0s", + $this->lang->formatTimePeriod( 3599.55 ), + 'formatTimePeriod() rounding (<1h)' + ); + + $this->assertEquals( + "2h 0m 0s", + $this->lang->formatTimePeriod( 7199.55 ), + 'formatTimePeriod() rounding (>=1h)' + ); + } } -- 2.20.1