From ac5f7b3127a41d2fba63453c9465275621cdd461 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 27 Jun 2011 22:32:58 +0000 Subject: [PATCH] Added formatTimePeriod() tests for r90385 and made some fixes --- languages/Language.php | 52 ++++++++++++++++------- tests/phpunit/languages/LanguageTest.php | 54 ++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 15 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index 26fdbc5ac0..79397132d0 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -3413,16 +3413,16 @@ class Language { * @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 @@ class Language { $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 @@ class Language { $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; } /** diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index b95f0ecdd9..085420e3fe 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -56,6 +56,60 @@ class LanguageTest extends MediaWikiTestCase { $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)' + ); } /** -- 2.20.1