http://www.mediawiki.org/wiki/Special:Code/MediaWiki/76269

Revision: 76269
Author:   ialex
Date:     2010-11-07 21:05:01 +0000 (Sun, 07 Nov 2010)
Log Message:
-----------
Follow-up r65654:
* Per Hashar: added test cases
* Added some more round() to be really correct

Modified Paths:
--------------
    trunk/phase3/languages/Language.php
    trunk/phase3/maintenance/tests/phpunit/languages/LanguageTest.php

Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2010-11-07 21:00:22 UTC (rev 76268)
+++ trunk/phase3/languages/Language.php 2010-11-07 21:05:01 UTC (rev 76269)
@@ -2873,11 +2873,11 @@
        }
 
        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 ) {

Modified: trunk/phase3/maintenance/tests/phpunit/languages/LanguageTest.php
===================================================================
--- trunk/phase3/maintenance/tests/phpunit/languages/LanguageTest.php   
2010-11-07 21:00:22 UTC (rev 76268)
+++ trunk/phase3/maintenance/tests/phpunit/languages/LanguageTest.php   
2010-11-07 21:05:01 UTC (rev 76269)
@@ -20,4 +20,42 @@
                        '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)'
+               );
+       }
 }


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to