r90915 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90914‎ | r90915 | r90916 >
Date:22:32, 27 June 2011
Author:aaron
Status:resolved (Comments)
Tags:
Comment:
Added formatTimePeriod() tests for r90385 and made some fixes
Modified paths:
  • /trunk/phase3/languages/Language.php (modified) (history)
  • /trunk/phase3/tests/phpunit/languages/LanguageTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/languages/LanguageTest.php
@@ -56,6 +56,60 @@
5757 $this->lang->formatTimePeriod( 7199.55 ),
5858 'formatTimePeriod() rounding (>=1h)'
5959 );
 60+
 61+ $this->assertEquals(
 62+ "2h 0m",
 63+ $this->lang->formatTimePeriod( 7199.55, 'avoidseconds' ),
 64+ 'formatTimePeriod() rounding (>=1h), avoidseconds'
 65+ );
 66+
 67+ $this->assertEquals(
 68+ "2h 0m",
 69+ $this->lang->formatTimePeriod( 7199.55, 'avoidminutes' ),
 70+ 'formatTimePeriod() rounding (>=1h), avoidminutes'
 71+ );
 72+
 73+ $this->assertEquals(
 74+ "48h 0m",
 75+ $this->lang->formatTimePeriod( 172799.55, 'avoidseconds' ),
 76+ 'formatTimePeriod() rounding (=48h), avoidseconds'
 77+ );
 78+
 79+ $this->assertEquals(
 80+ "3d 0h",
 81+ $this->lang->formatTimePeriod( 259199.55, 'avoidminutes' ),
 82+ 'formatTimePeriod() rounding (>48h), avoidminutes'
 83+ );
 84+
 85+ $this->assertEquals(
 86+ "2d 1h 0m",
 87+ $this->lang->formatTimePeriod( 176399.55, 'avoidseconds' ),
 88+ 'formatTimePeriod() rounding (>48h), avoidseconds'
 89+ );
 90+
 91+ $this->assertEquals(
 92+ "2d 1h",
 93+ $this->lang->formatTimePeriod( 176399.55, 'avoidminutes' ),
 94+ 'formatTimePeriod() rounding (>48h), avoidminutes'
 95+ );
 96+
 97+ $this->assertEquals(
 98+ "3d 0h 0m",
 99+ $this->lang->formatTimePeriod( 259199.55, 'avoidseconds' ),
 100+ 'formatTimePeriod() rounding (>48h), avoidminutes'
 101+ );
 102+
 103+ $this->assertEquals(
 104+ "2d 0h 0m",
 105+ $this->lang->formatTimePeriod( 172801.55, 'avoidseconds' ),
 106+ 'formatTimePeriod() rounding, (>48h), avoidseconds'
 107+ );
 108+
 109+ $this->assertEquals(
 110+ "2d 1h 1m 1s",
 111+ $this->lang->formatTimePeriod( 176460.55 ),
 112+ 'formatTimePeriod() rounding, recursion, (>48h)'
 113+ );
60114 }
61115
62116 /**
Index: trunk/phase3/languages/Language.php
@@ -3413,16 +3413,16 @@
34143414 * @param $seconds int|float
34153415 * @param $format String Optional, one of ("avoidseconds","avoidminutes"):
34163416 * "avoidseconds" - don't mention seconds if $seconds >= 1 hour
3417 - * "avoidminutes" - don't mention seconds/minutes if $seconds > 2 days
 3417+ * "avoidminutes" - don't mention seconds/minutes if $seconds > 48 hours
34183418 * @return string
34193419 */
34203420 function formatTimePeriod( $seconds, $format = false ) {
34213421 if ( round( $seconds * 10 ) < 100 ) {
3422 - return $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ) .
3423 - $this->getMessageFromDB( 'seconds-abbrev' );
 3422+ $s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
 3423+ $s .= $this->getMessageFromDB( 'seconds-abbrev' );
34243424 } elseif ( round( $seconds ) < 60 ) {
3425 - return $this->formatNum( round( $seconds ) ) .
3426 - $this->getMessageFromDB( 'seconds-abbrev' );
 3425+ $s = $this->formatNum( round( $seconds ) );
 3426+ $s .= $this->getMessageFromDB( 'seconds-abbrev' );
34273427 } elseif ( round( $seconds ) < 3600 ) {
34283428 $minutes = floor( $seconds / 60 );
34293429 $secondsPart = round( fmod( $seconds, 60 ) );
@@ -3430,9 +3430,9 @@
34313431 $secondsPart = 0;
34323432 $minutes++;
34333433 }
3434 - return $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' ) .
3435 - ' ' .
3436 - $this->formatNum( $secondsPart ) . $this->getMessageFromDB( 'seconds-abbrev' );
 3434+ $s = $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
 3435+ $s .= ' ';
 3436+ $s .= $this->formatNum( $secondsPart ) . $this->getMessageFromDB( 'seconds-abbrev' );
34373437 } elseif ( round( $seconds ) <= 2*86400 ) {
34383438 $hours = floor( $seconds / 3600 );
34393439 $minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
@@ -3445,25 +3445,47 @@
34463446 $minutes = 0;
34473447 $hours++;
34483448 }
3449 - $s = $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' ) .
3450 - ' ' .
3451 - $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
3452 - if ( $format !== 'avoidseconds' ) {
 3449+ $s = $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
 3450+ $s .= ' ';
 3451+ $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
 3452+ if ( !in_array( $format, array( 'avoidseconds', 'avoidminutes' ) ) ) {
34533453 $s .= ' ' . $this->formatNum( $secondsPart ) .
34543454 $this->getMessageFromDB( 'seconds-abbrev' );
34553455 }
3456 - return $s;
34573456 } else {
34583457 $days = floor( $seconds / 86400 );
3459 - $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' ) . ' ';
34603458 if ( $format === 'avoidminutes' ) {
 3459+ $hours = round( ( $seconds - $days * 86400 ) / 3600 );
 3460+ if ( $hours == 24 ) {
 3461+ $hours = 0;
 3462+ $days++;
 3463+ }
 3464+ $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
 3465+ $s .= ' ';
 3466+ $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
 3467+ } elseif ( $format === 'avoidseconds' ) {
34613468 $hours = floor( ( $seconds - $days * 86400 ) / 3600 );
 3469+ $minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
 3470+ if ( $minutes == 60 ) {
 3471+ $minutes = 0;
 3472+ $hours++;
 3473+ }
 3474+ if ( $hours == 24 ) {
 3475+ $hours = 0;
 3476+ $days++;
 3477+ }
 3478+ $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
 3479+ $s .= ' ';
34623480 $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
 3481+ $s .= ' ';
 3482+ $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
34633483 } else {
 3484+ $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
 3485+ $s .= ' ';
34643486 $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
34653487 }
3466 - return $s;
34673488 }
 3489+ return $s;
34683490 }
34693491
34703492 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r95071Per CR on r90915, fix the description of one of the assertionscatrope09:59, 20 August 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90385* Added (and use) $format param to formatTimePeriod() to make output less ver...aaron07:25, 19 June 2011

Comments

#Comment by Nikerabbit (talk | contribs)   06:08, 28 June 2011

This doesn't look right:

+               $this->assertEquals(
+                       "3d 0h 0m",
+                       $this->lang->formatTimePeriod( 259199.55, 'avoidseconds' ),
+                       'formatTimePeriod() rounding (>48h), avoidminutes'
+               );
#Comment by Catrope (talk | contribs)   09:59, 20 August 2011

avoidminutes/avoidseconds discrepancy fixed in r95071.

Status & tagging log