https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114321

Revision: 114321
Author:   jeroendedauw
Date:     2012-03-20 22:47:50 +0000 (Tue, 20 Mar 2012)
Log Message:
-----------
handle cases where an empty array is provided by using this as default, also 
correctly handle cases where the smallest unit is not the second and the result 
is 0 smallestunit

Modified Paths:
--------------
    trunk/phase3/languages/Language.php

Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2012-03-20 22:34:20 UTC (rev 114320)
+++ trunk/phase3/languages/Language.php 2012-03-20 22:47:50 UTC (rev 114321)
@@ -1919,7 +1919,7 @@
         *
         * @return string
         */
-       public function formatDuration( $seconds, array $chosenIntervals = 
array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 
'minutes', 'seconds' ) ) {
+       public function formatDuration( $seconds, array $chosenIntervals = 
array() ) {
                $intervals = array(
                        'millennia' => 1000 * 31557600,
                        'centuries' => 100 * 31557600,
@@ -1932,16 +1932,20 @@
                        'seconds' => 1,
                );
 
-               if ( !empty( $chosenIntervals ) ) {
-                       $intervals = array_intersect_key( $intervals, 
array_flip( $chosenIntervals ) );
+               if ( empty( $chosenIntervals ) ) {
+                       $chosenIntervals = array( 'millennia', 'centuries', 
'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
                }
 
+               $intervals = array_intersect_key( $intervals, array_flip( 
$chosenIntervals ) );
+               $sortedNames = array_keys( $intervals );
+               $smallestInterval = array_pop( $sortedNames );
+
                $segments = array();
 
                foreach ( $intervals as $name => $length ) {
                        $value = floor( $seconds / $length );
 
-                       if ( $value > 0 || ( $name == 'seconds' && empty( 
$segments ) ) ) {
+                       if ( $value > 0 || ( $name == $smallestInterval && 
empty( $segments ) ) ) {
                                $seconds -= $value * $length;
                                $message = new Message( 'duration-' . $name, 
array( $value ) );
                                $segments[] = $message->inLanguage( $this 
)->escaped();


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to