http://www.mediawiki.org/wiki/Special:Code/MediaWiki/90915
Revision: 90915
Author: aaron
Date: 2011-06-27 22:32:58 +0000 (Mon, 27 Jun 2011)
Log Message:
-----------
Added formatTimePeriod() tests for r90385 and made some fixes
Modified Paths:
--------------
trunk/phase3/languages/Language.php
trunk/phase3/tests/phpunit/languages/LanguageTest.php
Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2011-06-27 22:19:46 UTC (rev 90914)
+++ trunk/phase3/languages/Language.php 2011-06-27 22:32:58 UTC (rev 90915)
@@ -3413,16 +3413,16 @@
* @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 > 2 days
+ * "avoidminutes" - don't mention seconds/minutes if
$seconds > 48 hours
* @return string
*/
function formatTimePeriod( $seconds, $format = false ) {
if ( round( $seconds * 10 ) < 100 ) {
- return $this->formatNum( sprintf( "%.1f", round(
$seconds * 10 ) / 10 ) ) .
- $this->getMessageFromDB( 'seconds-abbrev' );
+ $s = $this->formatNum( sprintf( "%.1f", round( $seconds
* 10 ) / 10 ) );
+ $s .= $this->getMessageFromDB( 'seconds-abbrev' );
} elseif ( round( $seconds ) < 60 ) {
- return $this->formatNum( round( $seconds ) ) .
- $this->getMessageFromDB( 'seconds-abbrev' );
+ $s = $this->formatNum( round( $seconds ) );
+ $s .= $this->getMessageFromDB( 'seconds-abbrev' );
} elseif ( round( $seconds ) < 3600 ) {
$minutes = floor( $seconds / 60 );
$secondsPart = round( fmod( $seconds, 60 ) );
@@ -3430,9 +3430,9 @@
$secondsPart = 0;
$minutes++;
}
- return $this->formatNum( $minutes ) .
$this->getMessageFromDB( 'minutes-abbrev' ) .
- ' ' .
- $this->formatNum( $secondsPart ) .
$this->getMessageFromDB( 'seconds-abbrev' );
+ $s = $this->formatNum( $minutes ) .
$this->getMessageFromDB( 'minutes-abbrev' );
+ $s .= ' ';
+ $s .= $this->formatNum( $secondsPart ) .
$this->getMessageFromDB( 'seconds-abbrev' );
} elseif ( round( $seconds ) <= 2*86400 ) {
$hours = floor( $seconds / 3600 );
$minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
@@ -3445,25 +3445,47 @@
$minutes = 0;
$hours++;
}
- $s = $this->formatNum( $hours ) .
$this->getMessageFromDB( 'hours-abbrev' ) .
- ' ' .
- $this->formatNum( $minutes ) .
$this->getMessageFromDB( 'minutes-abbrev' );
- if ( $format !== 'avoidseconds' ) {
+ $s = $this->formatNum( $hours ) .
$this->getMessageFromDB( 'hours-abbrev' );
+ $s .= ' ';
+ $s .= $this->formatNum( $minutes ) .
$this->getMessageFromDB( 'minutes-abbrev' );
+ if ( !in_array( $format, array( 'avoidseconds',
'avoidminutes' ) ) ) {
$s .= ' ' . $this->formatNum( $secondsPart ) .
$this->getMessageFromDB(
'seconds-abbrev' );
}
- return $s;
} else {
$days = floor( $seconds / 86400 );
- $s = $this->formatNum( $days ) .
$this->getMessageFromDB( 'days-abbrev' ) . ' ';
if ( $format === 'avoidminutes' ) {
+ $hours = round( ( $seconds - $days * 86400 ) /
3600 );
+ if ( $hours == 24 ) {
+ $hours = 0;
+ $days++;
+ }
+ $s = $this->formatNum( $days ) .
$this->getMessageFromDB( 'days-abbrev' );
+ $s .= ' ';
+ $s .= $this->formatNum( $hours ) .
$this->getMessageFromDB( 'hours-abbrev' );
+ } elseif ( $format === 'avoidseconds' ) {
$hours = floor( ( $seconds - $days * 86400 ) /
3600 );
+ $minutes = round( ( $seconds - $days * 86400 -
$hours * 3600 ) / 60 );
+ if ( $minutes == 60 ) {
+ $minutes = 0;
+ $hours++;
+ }
+ if ( $hours == 24 ) {
+ $hours = 0;
+ $days++;
+ }
+ $s = $this->formatNum( $days ) .
$this->getMessageFromDB( 'days-abbrev' );
+ $s .= ' ';
$s .= $this->formatNum( $hours ) .
$this->getMessageFromDB( 'hours-abbrev' );
+ $s .= ' ';
+ $s .= $this->formatNum( $minutes ) .
$this->getMessageFromDB( 'minutes-abbrev' );
} else {
+ $s = $this->formatNum( $days ) .
$this->getMessageFromDB( 'days-abbrev' );
+ $s .= ' ';
$s .= $this->formatTimePeriod( $seconds - $days
* 86400, $format );
}
- return $s;
}
+ return $s;
}
/**
Modified: trunk/phase3/tests/phpunit/languages/LanguageTest.php
===================================================================
--- trunk/phase3/tests/phpunit/languages/LanguageTest.php 2011-06-27
22:19:46 UTC (rev 90914)
+++ trunk/phase3/tests/phpunit/languages/LanguageTest.php 2011-06-27
22:32:58 UTC (rev 90915)
@@ -56,6 +56,60 @@
$this->lang->formatTimePeriod( 7199.55 ),
'formatTimePeriod() rounding (>=1h)'
);
+
+ $this->assertEquals(
+ "2h 0m",
+ $this->lang->formatTimePeriod( 7199.55, 'avoidseconds'
),
+ 'formatTimePeriod() rounding (>=1h), avoidseconds'
+ );
+
+ $this->assertEquals(
+ "2h 0m",
+ $this->lang->formatTimePeriod( 7199.55, 'avoidminutes'
),
+ 'formatTimePeriod() rounding (>=1h), avoidminutes'
+ );
+
+ $this->assertEquals(
+ "48h 0m",
+ $this->lang->formatTimePeriod( 172799.55,
'avoidseconds' ),
+ 'formatTimePeriod() rounding (=48h), avoidseconds'
+ );
+
+ $this->assertEquals(
+ "3d 0h",
+ $this->lang->formatTimePeriod( 259199.55,
'avoidminutes' ),
+ 'formatTimePeriod() rounding (>48h), avoidminutes'
+ );
+
+ $this->assertEquals(
+ "2d 1h 0m",
+ $this->lang->formatTimePeriod( 176399.55,
'avoidseconds' ),
+ 'formatTimePeriod() rounding (>48h), avoidseconds'
+ );
+
+ $this->assertEquals(
+ "2d 1h",
+ $this->lang->formatTimePeriod( 176399.55,
'avoidminutes' ),
+ 'formatTimePeriod() rounding (>48h), avoidminutes'
+ );
+
+ $this->assertEquals(
+ "3d 0h 0m",
+ $this->lang->formatTimePeriod( 259199.55,
'avoidseconds' ),
+ 'formatTimePeriod() rounding (>48h), avoidminutes'
+ );
+
+ $this->assertEquals(
+ "2d 0h 0m",
+ $this->lang->formatTimePeriod( 172801.55,
'avoidseconds' ),
+ 'formatTimePeriod() rounding, (>48h), avoidseconds'
+ );
+
+ $this->assertEquals(
+ "2d 1h 1m 1s",
+ $this->lang->formatTimePeriod( 176460.55 ),
+ 'formatTimePeriod() rounding, recursion, (>48h)'
+ );
}
/**
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs