Skip to content

Commit

Permalink
Merge pull request #9 from AntonLazarev/leading_zero
Browse files Browse the repository at this point in the history
Added leading zero for "9" minutes and seconds
  • Loading branch information
kevinkhill authored Sep 9, 2017
2 parents 39a5cb1 + b2b284c commit 10b170d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function parse($duration)
if (preg_match($this->daysRegex, $duration, $matches)) {
$this->days = (int) $matches[1];
}

if (preg_match($this->hoursRegex, $duration, $matches)) {
$this->hours = (int) $matches[1];
}
Expand Down Expand Up @@ -163,15 +163,15 @@ public function toMinutes($duration = null, $roundToInteger = false)
*/
public function formatted($duration = null)
{

if (! is_null($duration)) {
$this->parse($duration);
}

$hours = $this->hours + ($this->days * $this->hoursPerDay);

if ($this->seconds > 0) {
if ($this->seconds < 9 && ($this->minutes > 0 || $hours > 0)) {
if ($this->seconds <= 9 && ($this->minutes > 0 || $hours > 0)) {
$this->output .= '0' . $this->seconds;
} else {
$this->output .= $this->seconds;
Expand All @@ -183,7 +183,7 @@ public function formatted($duration = null)
}

if ($this->minutes > 0) {
if ($this->minutes < 9 && $hours > 0) {
if ($this->minutes <= 9 && $hours > 0) {
$this->output = '0' . $this->minutes . ':' . $this->output;
} else {
$this->output = $this->minutes . ':' . $this->output;
Expand Down
12 changes: 12 additions & 0 deletions test/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ public function testGettingValueFromDaySuffixes($intVal, $dayStr)
public function testConvertingSecondsToFormattedString()
{
$this->assertEquals('4', $this->d->formatted(4));
$this->assertEquals('9', $this->d->formatted(9));
$this->assertEquals('42', $this->d->formatted(42));
$this->assertEquals('1:02', $this->d->formatted(62));
$this->assertEquals('1:09', $this->d->formatted(69));
$this->assertEquals('1:42', $this->d->formatted(102));
$this->assertEquals('10:47', $this->d->formatted(647));
$this->assertEquals('1:00:00', $this->d->formatted(3600));
Expand All @@ -114,13 +116,16 @@ public function testConvertingSecondsToFormattedString()
$this->assertEquals('1:01:00', $this->d->formatted(3660));
$this->assertEquals('1:01:14', $this->d->formatted(3674));
$this->assertEquals('1:04:25', $this->d->formatted(3865));
$this->assertEquals('1:09:09', $this->d->formatted(4149));
}

public function testConvertingFormattedStringsToSeconds()
{
$this->assertEquals(4, $this->d->toSeconds('4'));
$this->assertEquals(9, $this->d->toSeconds('9'));
$this->assertEquals(42, $this->d->toSeconds('42'));
$this->assertEquals(62, $this->d->toSeconds('1:02'));
$this->assertEquals(69, $this->d->toSeconds('1:09'));
$this->assertEquals(102, $this->d->toSeconds('1:42'));
$this->assertEquals(647, $this->d->toSeconds('10:47'));
$this->assertEquals(3600, $this->d->toSeconds('1:00:00'));
Expand All @@ -129,13 +134,16 @@ public function testConvertingFormattedStringsToSeconds()
$this->assertEquals(3660, $this->d->toSeconds('1:01:00'));
$this->assertEquals(3674, $this->d->toSeconds('1:01:14'));
$this->assertEquals(3865, $this->d->toSeconds('1:04:25'));
$this->assertEquals(4149, $this->d->toSeconds('1:09:09'));
}

public function testConvertingFormattedStringsToMinutes()
{
$this->assertEquals(4/60, $this->d->toMinutes('4'));
$this->assertEquals(9/60, $this->d->toMinutes('9'));
$this->assertEquals(42/60, $this->d->toMinutes('42'));
$this->assertEquals(62/60, $this->d->toMinutes('1:02'));
$this->assertEquals(69/60, $this->d->toMinutes('1:09'));
$this->assertEquals(102/60, $this->d->toMinutes('1:42'));
$this->assertEquals(647/60, $this->d->toMinutes('10:47'));
$this->assertEquals(3600/60, $this->d->toMinutes('1:00:00'));
Expand All @@ -144,10 +152,13 @@ public function testConvertingFormattedStringsToMinutes()
$this->assertEquals(3660/60, $this->d->toMinutes('1:01:00'));
$this->assertEquals(3674/60, $this->d->toMinutes('1:01:14'));
$this->assertEquals(3865/60, $this->d->toMinutes('1:04:25'));
$this->assertEquals(4149/60, $this->d->toMinutes('1:09:09'));

$this->assertEquals(0, $this->d->toMinutes('4', true));
$this->assertEquals(0, $this->d->toMinutes('9', true));
$this->assertEquals(1, $this->d->toMinutes('42', true));
$this->assertEquals(1, $this->d->toMinutes('1:02', true));
$this->assertEquals(1, $this->d->toMinutes('1:09', true));
$this->assertEquals(2, $this->d->toMinutes('1:42', true));
$this->assertEquals(11, $this->d->toMinutes('10:47', true));
$this->assertEquals(60, $this->d->toMinutes('1:00:00', true));
Expand All @@ -157,6 +168,7 @@ public function testConvertingFormattedStringsToMinutes()
$this->assertEquals(61, $this->d->toMinutes('1:01:14', true));
$this->assertEquals(64, $this->d->toMinutes('1:04:25', true));
$this->assertEquals(65, $this->d->toMinutes('1:04:55', true));
$this->assertEquals(69, $this->d->toMinutes('1:09:09', true));
}

public function testConvertSecondsToHumanizedString()
Expand Down

0 comments on commit 10b170d

Please sign in to comment.