Follow-up r65654:
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sun, 7 Nov 2010 21:05:01 +0000 (21:05 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sun, 7 Nov 2010 21:05:01 +0000 (21:05 +0000)
* Per Hashar: added test cases
* Added some more round() to be really correct

languages/Language.php
maintenance/tests/phpunit/languages/LanguageTest.php

index 9a1a302..73acd36 100644 (file)
@@ -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 ) {
index f27df96..8e30259 100644 (file)
@@ -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)'
+               );
+       }
 }