* @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 > 48 hours
+ * @param $noAbbrevs Bool If true (or true-ish, recommend using 'noabbrevs' for clarity), use 'seconds' and friends instead of 'seconds-abbrev' and friends
* @return string
*/
- function formatTimePeriod( $seconds, $format = false ) {
+ function formatTimePeriod( $seconds, $format = false, $noAbbrevs = false ) {
+ $secondsMsg = wfMessage( $noAbbrevs ? 'seconds' : 'seconds-abbrev' )->inLanguage( $this );
+ $minutesMsg = wfMessage( $noAbbrevs ? 'minutes' : 'minutes-abbrev' )->inLanguage( $this );
+ $hoursMsg = wfMessage( $noAbbrevs ? 'hours' : 'hours-abbrev' )->inLanguage( $this );
+ $daysMsg = wfMessage( $noAbbrevs ? 'days' : 'days-abbrev' )->inLanguage( $this );
if ( round( $seconds * 10 ) < 100 ) {
$s = $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) );
- $s .= $this->getMessageFromDB( 'seconds-abbrev' );
+ $s = $secondsMsg->params( $s )->text();
} elseif ( round( $seconds ) < 60 ) {
$s = $this->formatNum( round( $seconds ) );
- $s .= $this->getMessageFromDB( 'seconds-abbrev' );
+ $s = $secondsMsg->params( $s )->text();
} elseif ( round( $seconds ) < 3600 ) {
$minutes = floor( $seconds / 60 );
$secondsPart = round( fmod( $seconds, 60 ) );
$secondsPart = 0;
$minutes++;
}
- $s = $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
+ $s = $minutesMsg->params( $this->formatNum( $minutes ) )->text();
$s .= ' ';
- $s .= $this->formatNum( $secondsPart ) . $this->getMessageFromDB( 'seconds-abbrev' );
+ $s .= $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
} elseif ( round( $seconds ) <= 2 * 86400 ) {
$hours = floor( $seconds / 3600 );
$minutes = floor( ( $seconds - $hours * 3600 ) / 60 );
$minutes = 0;
$hours++;
}
- $s = $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
+ $s = $hoursMsg->params( $this->formatNum( $hours ) )->text();
$s .= ' ';
- $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
+ $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
if ( !in_array( $format, array( 'avoidseconds', 'avoidminutes' ) ) ) {
- $s .= ' ' . $this->formatNum( $secondsPart ) .
- $this->getMessageFromDB( 'seconds-abbrev' );
+ $s .= ' ' . $secondsMsg->params( $this->formatNum( $secondsPart ) )->text();
}
} else {
$days = floor( $seconds / 86400 );
$hours = 0;
$days++;
}
- $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
+ $s = $daysMsg->params( $this->formatNum( $days ) )->text();
$s .= ' ';
- $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
+ $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
} elseif ( $format === 'avoidseconds' ) {
$hours = floor( ( $seconds - $days * 86400 ) / 3600 );
$minutes = round( ( $seconds - $days * 86400 - $hours * 3600 ) / 60 );
$hours = 0;
$days++;
}
- $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
+ $s = $daysMsg->params( $this->formatNum( $days ) )->text();
$s .= ' ';
- $s .= $this->formatNum( $hours ) . $this->getMessageFromDB( 'hours-abbrev' );
+ $s .= $hoursMsg->params( $this->formatNum( $hours ) )->text();
$s .= ' ';
- $s .= $this->formatNum( $minutes ) . $this->getMessageFromDB( 'minutes-abbrev' );
+ $s .= $minutesMsg->params( $this->formatNum( $minutes ) )->text();
} else {
- $s = $this->formatNum( $days ) . $this->getMessageFromDB( 'days-abbrev' );
+ $s = $daysMsg->params( $this->formatNum( $days ) )->text();
$s .= ' ';
- $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format );
+ $s .= $this->formatTimePeriod( $seconds - $days * 86400, $format, $noAbbrevs );
}
}
return $s;
}
/** @dataProvider provideFormattableTimes */
- function testFormatTimePeriod( $seconds, $avoid, $expected, $desc ) {
- $this->assertEquals( $expected, $this->lang->formatTimePeriod( $seconds, $avoid ), $desc );
+ function testFormatTimePeriod( $seconds, $avoid, $noAbbrevs, $expected, $desc ) {
+ $this->assertEquals( $expected, $this->lang->formatTimePeriod( $seconds, $avoid, $noAbbrevs ), $desc );
}
function provideFormattableTimes() {
return array(
- array( 9.45, false, '9.5s', 'formatTimePeriod() rounding (<10s)' ),
- array( 9.95, false, '10s', 'formatTimePeriod() rounding (<10s)' ),
- array( 59.55, false, '1m 0s', 'formatTimePeriod() rounding (<60s)' ),
- array( 119.55, false, '2m 0s', 'formatTimePeriod() rounding (<1h)' ),
- array( 3599.55, false, '1h 0m 0s', 'formatTimePeriod() rounding (<1h)' ),
- array( 7199.55, false, '2h 0m 0s', 'formatTimePeriod() rounding (>=1h)' ),
- array( 7199.55, 'avoidseconds', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
- array( 7199.55, 'avoidminutes', '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
- array( 172799.55, 'avoidseconds', '48h 0m', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
- array( 259199.55, 'avoidminutes', '3d 0h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
- array( 176399.55, 'avoidseconds', '2d 1h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
- array( 176399.55, 'avoidminutes', '2d 1h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
- array( 259199.55, 'avoidseconds', '3d 0h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
- array( 172801.55, 'avoidseconds', '2d 0h 0m', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
- array( 176460.55, false, '2d 1h 1m 1s', 'formatTimePeriod() rounding, recursion, (>48h)' ),
+ array( 9.45, false, false, '9.5s', 'formatTimePeriod() rounding (<10s)' ),
+ array( 9.45, false, 'noabbrevs', '9.5 seconds', 'formatTimePeriod() rounding (<10s)' ),
+ array( 9.95, false, false, '10s', 'formatTimePeriod() rounding (<10s)' ),
+ array( 9.95, false, 'noabbrevs', '10 seconds', 'formatTimePeriod() rounding (<10s)' ),
+ array( 59.55, false, false, '1m 0s', 'formatTimePeriod() rounding (<60s)' ),
+ array( 59.55, false, 'noabbrevs', '1 minute 0 seconds', 'formatTimePeriod() rounding (<60s)' ),
+ array( 119.55, false, false, '2m 0s', 'formatTimePeriod() rounding (<1h)' ),
+ array( 119.55, false, 'noabbrevs', '2 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
+ array( 3599.55, false, false, '1h 0m 0s', 'formatTimePeriod() rounding (<1h)' ),
+ array( 3599.55, false, 'noabbrevs', '1 hour 0 minutes 0 seconds', 'formatTimePeriod() rounding (<1h)' ),
+ array( 7199.55, false, false, '2h 0m 0s', 'formatTimePeriod() rounding (>=1h)' ),
+ array( 7199.55, false, 'noabbrevs', '2 hours 0 minutes 0 seconds', 'formatTimePeriod() rounding (>=1h)' ),
+ array( 7199.55, 'avoidseconds', false, '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
+ array( 7199.55, 'avoidseconds', 'noabbrevs', '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidseconds' ),
+ array( 7199.55, 'avoidminutes', false, '2h 0m', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
+ array( 7199.55, 'avoidminutes', 'noabbrevs', '2 hours 0 minutes', 'formatTimePeriod() rounding (>=1h), avoidminutes' ),
+ array( 172799.55, 'avoidseconds', false, '48h 0m', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
+ array( 172799.55, 'avoidseconds', 'noabbrevs', '48 hours 0 minutes', 'formatTimePeriod() rounding (=48h), avoidseconds' ),
+ array( 259199.55, 'avoidminutes', false, '3d 0h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
+ array( 259199.55, 'avoidminutes', 'noabbrevs', '3 days 0 hours', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
+ array( 176399.55, 'avoidseconds', false, '2d 1h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
+ array( 176399.55, 'avoidseconds', 'noabbrevs', '2 days 1 hour 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
+ array( 176399.55, 'avoidminutes', false, '2d 1h', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
+ array( 176399.55, 'avoidminutes', 'noabbrevs', '2 days 1 hour', 'formatTimePeriod() rounding (>48h), avoidminutes' ),
+ array( 259199.55, 'avoidseconds', false, '3d 0h 0m', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
+ array( 259199.55, 'avoidseconds', 'noabbrevs', '3 days 0 hours 0 minutes', 'formatTimePeriod() rounding (>48h), avoidseconds' ),
+ array( 172801.55, 'avoidseconds', false, '2d 0h 0m', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
+ array( 172801.55, 'avoidseconds', 'noabbrevs', '2 days 0 hours 0 minutes', 'formatTimePeriod() rounding, (>48h), avoidseconds' ),
+ array( 176460.55, false, false, '2d 1h 1m 1s', 'formatTimePeriod() rounding, recursion, (>48h)' ),
+ array( 176460.55, false, 'noabbrevs', '2 days 1 hour 1 minute 1 second', 'formatTimePeriod() rounding, recursion, (>48h)' ),
);
}