support weeks in number shortening

This commit is contained in:
Roland Gruber 2018-11-14 20:13:31 +01:00
parent 74e8fb830d
commit 085be08eea
2 changed files with 42 additions and 7 deletions

View File

@ -1485,9 +1485,11 @@ function formatSecondsToShortFormat($numSeconds) {
$minutes = ($minutes == 0) ? '' : $minutes . 'm'; $minutes = ($minutes == 0) ? '' : $minutes . 'm';
$hours = floor(($numSeconds % 86400) / 3600); $hours = floor(($numSeconds % 86400) / 3600);
$hours = ($hours == 0) ? '' : $hours . 'h'; $hours = ($hours == 0) ? '' : $hours . 'h';
$days = floor($numSeconds / 86400); $days = floor(($numSeconds % 604800) / 86400);
$days = ($days == 0) ? '' : $days . 'd'; $days = ($days == 0) ? '' : $days . 'd';
return $days . $hours . $minutes . $seconds; $weeks = floor($numSeconds / 604800);
$weeks = ($weeks == 0) ? '' : $weeks . 'w';
return $weeks . $days . $hours . $minutes . $seconds;
} }
/** /**
@ -1501,19 +1503,22 @@ function unformatShortFormatToSeconds($text) {
return $text; return $text;
} }
$matches = array(); $matches = array();
if (preg_match('/^(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?$/', $text, $matches)) { if (preg_match('/^(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?$/', $text, $matches)) {
$newValue = 0; $newValue = 0;
if (!empty($matches[2])) { if (!empty($matches[2])) {
$newValue += $matches[2] * 86400; $newValue += $matches[2] * 604800;
} }
if (!empty($matches[4])) { if (!empty($matches[4])) {
$newValue += $matches[4] * 3600; $newValue += $matches[4] * 86400;
} }
if (!empty($matches[6])) { if (!empty($matches[6])) {
$newValue += $matches[6] * 60; $newValue += $matches[6] * 3600;
} }
if (!empty($matches[8])) { if (!empty($matches[8])) {
$newValue += $matches[8]; $newValue += $matches[8] * 60;
}
if (!empty($matches[10])) {
$newValue += $matches[10];
} }
return $newValue; return $newValue;
} }

View File

@ -48,6 +48,7 @@ class AccountTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(7215, unformatShortFormatToSeconds('2h15s')); $this->assertEquals(7215, unformatShortFormatToSeconds('2h15s'));
$this->assertEquals(172815, unformatShortFormatToSeconds('2d15s')); $this->assertEquals(172815, unformatShortFormatToSeconds('2d15s'));
$this->assertEquals(173700, unformatShortFormatToSeconds('2d15m')); $this->assertEquals(173700, unformatShortFormatToSeconds('2d15m'));
$this->assertEquals(1209615, unformatShortFormatToSeconds('2w15s'));
} }
/** /**
@ -58,4 +59,33 @@ class AccountTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('', unformatShortFormatToSeconds('')); $this->assertEquals('', unformatShortFormatToSeconds(''));
} }
/**
* Tests formatShortFormatToSeconds() without characters.
*/
function testFormatSecondsToShortFormat_basic() {
$this->assertEquals("15s", formatSecondsToShortFormat('15'));
}
/**
* Tests formatShortFormatToSeconds() with characters.
*/
function testFormatSecondsToShortFormat_conversion() {
$this->assertEquals('12s', formatSecondsToShortFormat(12));
$this->assertEquals('3m', formatSecondsToShortFormat(180));
$this->assertEquals('2h', formatSecondsToShortFormat(7200));
$this->assertEquals('1d', formatSecondsToShortFormat(86400));
$this->assertEquals('2m15s', formatSecondsToShortFormat(135));
$this->assertEquals('2h15s', formatSecondsToShortFormat(7215));
$this->assertEquals('2d15s', formatSecondsToShortFormat(172815));
$this->assertEquals('2d15m', formatSecondsToShortFormat(173700));
$this->assertEquals('2w15s', formatSecondsToShortFormat(1209615));
}
/**
* Tests formatShortFormatToSeconds() with invalid values.
*/
function testFormatSecondsToShortFormat_invalidNumber() {
$this->assertEquals('', formatSecondsToShortFormat(''));
}
} }