diff --git a/README.md b/README.md index 721ffd0..16034b9 100755 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ The library can accept either in colon separated format, like 2:43 for 2 minutes OR written as human readable or abbreviated time, such as 6m21s for 6 minutes and 21 seconds. -Both can be converted into seconds and minutes for easy storage into a database. +Both can be converted into seconds and minutes with precision for easy storage into a database. Seconds, colon separated, abbreviated, all three can be parsed and interchanged. - - supports hours, minutes, and seconds + - supports hours, minutes, and seconds (with microseconds) - humanized input supports any form of the words "hour", "minute", "seconds" - Example, you could input 1h4m2s or 4 Hr. 32 Min. @@ -40,7 +40,8 @@ echo $duration->humanize(); // 7m 31s echo $duration->formatted(); // 7:31 echo $duration->toSeconds(); // 451 echo $duration->toMinutes(); // 7.5166 -echo $duration->toMinutes(null, true); // 8 +echo $duration->toMinutes(null, 0); // 8 +echo $duration->toMinutes(null, 2); // 7.52 ``` ```php @@ -50,7 +51,7 @@ echo $duration->humanize(); // 1h 2m 5s echo $duration->formatted(); // 1:02:05 echo $duration->toSeconds(); // 3725 echo $duration->toMinutes(); // 62.0833 -echo $duration->toMinutes(null, true); // 62 +echo $duration->toMinutes(null, 0); // 62 ``` ```php @@ -60,7 +61,7 @@ echo $duration->humanize(); // 1h 11m 33s echo $duration->formatted(); // 1:11:33 echo $duration->toSeconds(); // 4293 echo $duration->toMinutes(); // 71.55 -echo $duration->toMinutes(null, true); // 72 +echo $duration->toMinutes(null, 0); // 72 ``` # Note diff --git a/src/Duration.php b/src/Duration.php index 52607c4..a699c87 100755 --- a/src/Duration.php +++ b/src/Duration.php @@ -17,7 +17,6 @@ class Duration private $minutesRegex; private $secondsRegex; - /** * Duration constructor. * @@ -25,20 +24,20 @@ class Duration */ public function __construct($duration = null, $hoursPerDay = 24) { - $this->days = 0; - $this->hours = 0; + $this->days = 0; + $this->hours = 0; $this->minutes = 0; $this->seconds = 0; - $this->output = ''; - $this->daysRegex = '/([0-9]{1,2})\s?(?:d|D)/'; - $this->hoursRegex = '/([0-9]{1,2})\s?(?:h|H)/'; + $this->output = ''; + $this->daysRegex = '/([0-9]{1,2})\s?(?:d|D)/'; + $this->hoursRegex = '/([0-9]{1,2})\s?(?:h|H)/'; $this->minutesRegex = '/([0-9]{1,2})\s?(?:m|M)/'; - $this->secondsRegex = '/([0-9]{1,2})\s?(?:s|S)/'; + $this->secondsRegex = '/([0-9]{1,2}(\.\d+)?)\s?(?:s|S)/'; $this->hoursPerDay = $hoursPerDay; - if (! is_null($duration)) { + if (!is_null($duration)) { $this->parse($duration); } } @@ -46,7 +45,7 @@ public function __construct($duration = null, $hoursPerDay = 24) /** * Attempt to parse one of the forms of duration. * - * @param int|string $duration A string or number, representing a duration + * @param int|float|string $duration A string or number, representing a duration * @return self|bool returns the Duration object if successful, otherwise false */ public function parse($duration) @@ -54,63 +53,74 @@ public function parse($duration) $this->reset(); if (is_numeric($duration)) { - $this->seconds = (int) $duration; + $this->seconds = (float)$duration; if ($this->seconds >= 60) { - $this->minutes = (int) floor($this->seconds / 60); - $this->seconds = (int) ($this->seconds - ($this->minutes * 60)); + $this->minutes = (int)floor($this->seconds / 60); + + // count current precision + $precision = 0; + if (($delimiterPos = strpos($this->seconds, '.')) !== false) { + $precision = strlen(substr($this->seconds, $delimiterPos + 1)); + } + + $this->seconds = (float)round(($this->seconds - ($this->minutes * 60)), $precision); } if ($this->minutes >= 60) { - $this->hours = (int) floor($this->minutes / 60); - $this->minutes = (int) ($this->minutes - ($this->hours * 60)); + $this->hours = (int)floor($this->minutes / 60); + $this->minutes = (int)($this->minutes - ($this->hours * 60)); } if ($this->hours >= $this->hoursPerDay) { - $this->days = (int) floor($this->hours / $this->hoursPerDay); - $this->hours = (int) ($this->hours - ($this->days * $this->hoursPerDay)); + $this->days = (int)floor($this->hours / $this->hoursPerDay); + $this->hours = (int)($this->hours - ($this->days * $this->hoursPerDay)); } return $this; - } else if (preg_match('/\:/', $duration)) { + } + + if (preg_match('/\:/', $duration)) { $parts = explode(':', $duration); if (count($parts) == 2) { - $this->minutes = (int) $parts[0]; - $this->seconds = (int) $parts[1]; - } else if (count($parts) == 3) { - $this->hours = (int) $parts[0]; - $this->minutes = (int) $parts[1]; - $this->seconds = (int) $parts[2]; + $this->minutes = (int)$parts[0]; + $this->seconds = (float)$parts[1]; + } else { + if (count($parts) == 3) { + $this->hours = (int)$parts[0]; + $this->minutes = (int)$parts[1]; + $this->seconds = (float)$parts[2]; + } } return $this; - } else if (preg_match($this->daysRegex, $duration) || - preg_match($this->hoursRegex, $duration) || - preg_match($this->minutesRegex, $duration) || - preg_match($this->secondsRegex, $duration)) - { + } + if (preg_match($this->daysRegex, $duration) || + preg_match($this->hoursRegex, $duration) || + preg_match($this->minutesRegex, $duration) || + preg_match($this->secondsRegex, $duration)) { if (preg_match($this->daysRegex, $duration, $matches)) { - $this->days = (int) $matches[1]; + $this->days = (int)$matches[1]; } if (preg_match($this->hoursRegex, $duration, $matches)) { - $this->hours = (int) $matches[1]; + $this->hours = (int)$matches[1]; } if (preg_match($this->minutesRegex, $duration, $matches)) { - $this->minutes = (int) $matches[1]; + $this->minutes = (int)$matches[1]; } if (preg_match($this->secondsRegex, $duration, $matches)) { - $this->seconds = (int) $matches[1]; + $this->seconds = (float)$matches[1]; } return $this; - } else { - return false; } + + return false; } /** @@ -118,39 +128,44 @@ public function parse($duration) * * For example, one hour and 42 minutes would be "6120" * - * @param int|string $duration A string or number, representing a duration - * @return int + * @param int|float|string $duration A string or number, representing a duration + * @param int|bool $precision Number of decimal digits to round to. If set to false, the number is not rounded. + * @return int|float */ - public function toSeconds($duration = null) + public function toSeconds($duration = null, $precision = false) { - if (! is_null($duration)) { + if (!is_null($duration)) { $this->parse($duration); } - $this->output = ($this->days * $this->hoursPerDay * 60 * 60) + ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds; - return (int) $this->output(); + return $precision !== false ? round($this->output, $precision) : $this->output; } /** - * Returns the duration as an amount of seconds. + * Returns the duration as an amount of minutes. * * For example, one hour and 42 minutes would be "102" minutes * - * @param int|string $duration A string or number, representing a duration - * @param boolean $roundToInteger Should the number be rounded and returned as integer - * @return int + * @param int|float|string $duration A string or number, representing a duration + * @param int|bool $precision Number of decimal digits to round to. If set to false, the number is not rounded. + * @return int|float */ - public function toMinutes($duration = null, $roundToInteger = false) + public function toMinutes($duration = null, $precision = false) { - if (! is_null($duration)) { + if (!is_null($duration)) { $this->parse($duration); } + // backward compatibility, true = round to integer + if ($precision === true) { + $precision = 0; + } + $this->output = ($this->days * $this->hoursPerDay * 60 * 60) + ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds; $result = intval($this->output()) / 60; - return $roundToInteger ? intval(round($result, 0)) : $result; + return $precision !== false ? round($result, $precision) : $result; } /** @@ -161,21 +176,21 @@ public function toMinutes($duration = null, $roundToInteger = false) * - 42 minutes would be "0:42:00" * - 28 seconds would be "0:00:28" * - * @param int|string $duration A string or number, representing a duration + * @param int|float|string $duration A string or number, representing a duration * @param bool $zeroFill A boolean, to force zero-fill result or not (see example) * @return string */ public function formatted($duration = null, $zeroFill = false) { - if (! is_null($duration)) { + 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 || $zeroFill)) { + if ($this->seconds > 0) { + if ($this->seconds < 10 && ($this->minutes > 0 || $hours > 0 || $zeroFill)) { $this->output .= '0' . $this->seconds; } else { $this->output .= $this->seconds; @@ -214,12 +229,12 @@ public function formatted($duration = null, $zeroFill = false) * * For example, one hour and 42 minutes would be "1h 42m" * - * @param int|string $duration A string or number, representing a duration + * @param int|float|string $duration A string or number, representing a duration * @return string */ public function humanize($duration = null) { - if (! is_null($duration)) { + if (!is_null($duration)) { $this->parse($duration); } @@ -251,11 +266,11 @@ public function humanize($duration = null) */ private function reset() { - $this->output = ''; + $this->output = ''; $this->seconds = 0; $this->minutes = 0; - $this->hours = 0; - $this->days = 0; + $this->hours = 0; + $this->days = 0; } /** diff --git a/test/DurationTest.php b/test/DurationTest.php index 4873796..9b5f8d3 100755 --- a/test/DurationTest.php +++ b/test/DurationTest.php @@ -14,34 +14,37 @@ public function setUp() public function secondsSampleData() { return array( - array( 1, '1 s'), - array( 1, '1 sec'), - array( 3, '3S'), - array( 7, '7 S'), + array(1, '1 s'), + array(1, '1 sec'), + array(3, '3S'), + array(7, '7 S'), array(51, '51seconds'), - array( 4, '4 Sec.'), - array(15, '15 SEcONDs') + array(4, '4 Sec.'), + array(15, '15 SEcONDs'), + array(1, '1.0 s'), + array(1.5689, '1.5689 S'), + array(1.00342, '1.00342 S'), ); } /** * @dataProvider secondsSampleData */ - public function testGettingValueFromSecondSuffixes($intVal, $secStr) + public function testGettingValueFromSecondSuffixes($expectedSeconds, $secStr) { $this->d->parse($secStr); - $this->assertEquals($intVal, $this->d->seconds); + $this->assertEquals($expectedSeconds, $this->d->seconds); } public function minutesSampleData() { return array( - array( 1, '1 m'), - array( 4, '4 min'), - array( 6, '6M'), + array(1, '1 m'), + array(4, '4 min'), + array(6, '6M'), array(14, '14 Ms'), array(31, '31 minutes'), - array( 9, '9Min.'), + array(9, '9Min.'), array(11, '11 MINUTE') ); } @@ -58,12 +61,12 @@ public function testGettingValueFromMinuteSuffixes($intVal, $minStr) public function hoursSampleData() { return array( - array( 1, '1 h'), - array( 1, '1 hr'), - array( 1, '1H'), + array(1, '1 h'), + array(1, '1 hr'), + array(1, '1H'), array(24, '24 Hrs'), - array( 3, '3hours'), - array( 6, '6HoUr'), + array(3, '3hours'), + array(6, '6HoUr'), array(14, '14 HOURs'), array(36, '36h') ); @@ -81,12 +84,12 @@ public function testGettingValueFromHourSuffixes($intVal, $hrStr) public function daysSampleData() { return array( - array( 1, '1 d'), - array( 1, '1 D'), - array( 1, '1D'), + array(1, '1 d'), + array(1, '1 D'), + array(1, '1D'), array(24, '24 ds'), - array( 3, '3days'), - array( 6, '6DaY'), + array(3, '3days'), + array(6, '6DaY'), array(14, '14 DAYs') ); } @@ -103,13 +106,13 @@ 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('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)); $this->assertEquals('1:00:01', $this->d->formatted(3601)); $this->assertEquals('1:00:11', $this->d->formatted(3611)); @@ -117,6 +120,22 @@ public function testConvertingSecondsToFormattedString() $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)); + + // microseconds + $this->assertEquals('4.987', $this->d->formatted(4.987)); + $this->assertEquals('9.123', $this->d->formatted(9.123)); + $this->assertEquals('42.672', $this->d->formatted(42.672)); + $this->assertEquals('1:02.23', $this->d->formatted(62.23)); + $this->assertEquals('1:09.9', $this->d->formatted(69.9)); + $this->assertEquals('1:42.62394', $this->d->formatted(102.62394)); + $this->assertEquals('10:47.5', $this->d->formatted(647.5)); + $this->assertEquals('1:00:00.954', $this->d->formatted(3600.954)); + $this->assertEquals('1:00:01.5123', $this->d->formatted(3601.5123)); + $this->assertEquals('1:00:11.0412368456', $this->d->formatted(3611.0412368456)); + $this->assertEquals('1:01:00.56945', $this->d->formatted(3660.56945)); + $this->assertEquals('1:01:14.3', $this->d->formatted(3674.3)); + $this->assertEquals('1:04:25.0005598', $this->d->formatted(3865.0005598)); + $this->assertEquals('1:09:09.123', $this->d->formatted(4149.123)); } public function testConvertingSecondsToFormattedStringZeroFilled() @@ -135,17 +154,21 @@ public function testConvertingSecondsToFormattedStringZeroFilled() $this->assertEquals('1:01:14', $this->d->formatted(3674, true)); $this->assertEquals('1:04:25', $this->d->formatted(3865, true)); $this->assertEquals('1:09:09', $this->d->formatted(4149, true)); + + // microseconds + $this->assertEquals('0:00:04.542', $this->d->formatted(4.542, true)); + $this->assertEquals('1:09:09.0987', $this->d->formatted(4149.0987, true)); } 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(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')); $this->assertEquals(3601, $this->d->toSeconds('1:00:01')); $this->assertEquals(3611, $this->d->toSeconds('1:00:11')); @@ -153,31 +176,64 @@ public function testConvertingFormattedStringsToSeconds() $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')); + + // microseconds + $this->assertEquals(4.6, $this->d->toSeconds('4.6')); + $this->assertEquals(9.5, $this->d->toSeconds('9.5')); + $this->assertEquals(42.1, $this->d->toSeconds('42.1')); + $this->assertEquals(62.96, $this->d->toSeconds('1:02.96')); + $this->assertEquals(69.23, $this->d->toSeconds('1:09.23')); + $this->assertEquals(102.55, $this->d->toSeconds('1:42.55')); + $this->assertEquals(647.999, $this->d->toSeconds('10:47.999')); + $this->assertEquals(3600.9987, $this->d->toSeconds('1:00:00.9987')); + $this->assertEquals(3601.000111, $this->d->toSeconds('1:00:01.000111')); + $this->assertEquals(3611.0999, $this->d->toSeconds('1:00:11.0999')); + $this->assertEquals(3660.500001, $this->d->toSeconds('1:01:00.500001')); + $this->assertEquals(3674.00001, $this->d->toSeconds('1:01:14.00001')); + $this->assertEquals(3865.499999, $this->d->toSeconds('1:04:25.499999')); + $this->assertEquals(4149.499999, $this->d->toSeconds('1:09:09.499999')); + + // precision + $this->assertEquals(5, $this->d->toSeconds('4.6', 0)); + $this->assertEquals(10, $this->d->toSeconds('9.5', 0)); + $this->assertEquals(42.1, $this->d->toSeconds('42.1', 1)); + $this->assertEquals(63, $this->d->toSeconds('1:02.96', 1)); + $this->assertEquals(69.23, $this->d->toSeconds('1:09.23')); + $this->assertEquals(102.55, $this->d->toSeconds('1:42.55', 2)); + $this->assertEquals(648, $this->d->toSeconds('10:47.999', 2)); + $this->assertEquals(3601, $this->d->toSeconds('1:00:00.9987', 2)); + $this->assertEquals(3601, $this->d->toSeconds('1:00:01.000111', 3)); + $this->assertEquals(3611.0999, $this->d->toSeconds('1:00:11.0999', 4)); + $this->assertEquals(3660.5, $this->d->toSeconds('1:01:00.500001', 2)); + $this->assertEquals(3674, $this->d->toSeconds('1:01:14.00001', 2)); + $this->assertEquals(3865.5, $this->d->toSeconds('1:04:25.499999', 3)); + $this->assertEquals(4149.499997, $this->d->toSeconds('1:09:09.4999971', 6)); } 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')); - $this->assertEquals(3601/60, $this->d->toMinutes('1:00:01')); - $this->assertEquals(3611/60, $this->d->toMinutes('1:00:11')); - $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(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')); + $this->assertEquals(3601 / 60, $this->d->toMinutes('1:00:01')); + $this->assertEquals(3611 / 60, $this->d->toMinutes('1:00:11')); + $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')); + + // to integer - BC + $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)); $this->assertEquals(60, $this->d->toMinutes('1:00:01', true)); @@ -187,22 +243,58 @@ public function testConvertingFormattedStringsToMinutes() $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)); + + // precision + $this->assertEquals(0, $this->d->toMinutes('4', 0)); + $this->assertEquals(0, $this->d->toMinutes('9', 0)); + $this->assertEquals(1, $this->d->toMinutes('42', 0)); + $this->assertEquals(1, $this->d->toMinutes('1:02', 0)); + $this->assertEquals(1, $this->d->toMinutes('1:09', 0)); + $this->assertEquals(2, $this->d->toMinutes('1:42', 0)); + $this->assertEquals(11, $this->d->toMinutes('10:47', 0)); + $this->assertEquals(60, $this->d->toMinutes('1:00:00', 0)); + $this->assertEquals(60, $this->d->toMinutes('1:00:01', 0)); + $this->assertEquals(60, $this->d->toMinutes('1:00:11', 0)); + $this->assertEquals(61, $this->d->toMinutes('1:01:00', 0)); + $this->assertEquals(61, $this->d->toMinutes('1:01:14', 0)); + $this->assertEquals(64, $this->d->toMinutes('1:04:25', 0)); + $this->assertEquals(65, $this->d->toMinutes('1:04:55', 0)); + $this->assertEquals(69, $this->d->toMinutes('1:09:09', 0)); + + $this->assertEquals(0.1, $this->d->toMinutes('4', 1)); + $this->assertEquals(0.15, $this->d->toMinutes('9', 2)); + $this->assertEquals(0.7, $this->d->toMinutes('42', 3)); + $this->assertEquals(1, $this->d->toMinutes('1:02', 1)); + $this->assertEquals(1.15, $this->d->toMinutes('1:09', 2)); + $this->assertEquals(1.7, $this->d->toMinutes('1:42', 3)); + $this->assertEquals(10.8, $this->d->toMinutes('10:47', 1)); + $this->assertEquals(60, $this->d->toMinutes('1:00:00', 2)); + $this->assertEquals(60.017, $this->d->toMinutes('1:00:01', 3)); + $this->assertEquals(60.2, $this->d->toMinutes('1:00:11', 1)); + $this->assertEquals(61, $this->d->toMinutes('1:01:00', 2)); + $this->assertEquals(61.233, $this->d->toMinutes('1:01:14', 3)); + $this->assertEquals(64.42, $this->d->toMinutes('1:04:25', 2)); + $this->assertEquals(64.92, $this->d->toMinutes('1:04:55', 2)); + $this->assertEquals(69.15, $this->d->toMinutes('1:09:09', 2)); } public function testConvertSecondsToHumanizedString() { - $this->assertEquals('4s', $this->d->humanize(4)); - $this->assertEquals('42s', $this->d->humanize(42)); - $this->assertEquals('1m 2s', $this->d->humanize(62)); - $this->assertEquals('1m 42s', $this->d->humanize(102)); - $this->assertEquals('10m 47s', $this->d->humanize(647)); - $this->assertEquals('1h', $this->d->humanize(3600)); - $this->assertEquals('1h 5s', $this->d->humanize(3605)); - $this->assertEquals('1h 1m', $this->d->humanize(3660)); - $this->assertEquals('1h 1m 5s', $this->d->humanize(3665)); - $this->assertEquals('3d', $this->d->humanize(259200)); + $this->assertEquals('4s', $this->d->humanize(4)); + $this->assertEquals('42s', $this->d->humanize(42)); + $this->assertEquals('1m 2s', $this->d->humanize(62)); + $this->assertEquals('1m 42s', $this->d->humanize(102)); + $this->assertEquals('10m 47s', $this->d->humanize(647)); + $this->assertEquals('1h', $this->d->humanize(3600)); + $this->assertEquals('1h 5s', $this->d->humanize(3605)); + $this->assertEquals('1h 1m', $this->d->humanize(3660)); + $this->assertEquals('1h 1m 5s', $this->d->humanize(3665)); + $this->assertEquals('3d', $this->d->humanize(259200)); $this->assertEquals('2d 11h 30m', $this->d->humanize(214200)); + $this->assertEquals('4.0596s', $this->d->humanize(4.0596)); + $this->assertEquals('2d 11h 30m 0.9542s', $this->d->humanize(214200.9542)); + } /** @@ -212,16 +304,16 @@ public function testConvertSecondsToHumanizedString() */ public function testConvertHumanizedStringToSeconds() { - $this->assertEquals(4, $this->d->toSeconds('4s')); - $this->assertEquals(42, $this->d->toSeconds('42s')); - $this->assertEquals(72, $this->d->toSeconds('1m 12s')); - $this->assertEquals(102, $this->d->toSeconds('1m 42s')); - $this->assertEquals(647, $this->d->toSeconds('10m 47s')); - $this->assertEquals(3600, $this->d->toSeconds('1h')); - $this->assertEquals(3605, $this->d->toSeconds('1h 5s')); - $this->assertEquals(3660, $this->d->toSeconds('1h 1m')); - $this->assertEquals(3665, $this->d->toSeconds('1h 1m 5s')); - $this->assertEquals(86400, $this->d->toSeconds('1d')); + $this->assertEquals(4, $this->d->toSeconds('4s')); + $this->assertEquals(42, $this->d->toSeconds('42s')); + $this->assertEquals(72, $this->d->toSeconds('1m 12s')); + $this->assertEquals(102, $this->d->toSeconds('1m 42s')); + $this->assertEquals(647, $this->d->toSeconds('10m 47s')); + $this->assertEquals(3600, $this->d->toSeconds('1h')); + $this->assertEquals(3605, $this->d->toSeconds('1h 5s')); + $this->assertEquals(3660, $this->d->toSeconds('1h 1m')); + $this->assertEquals(3665, $this->d->toSeconds('1h 1m 5s')); + $this->assertEquals(86400, $this->d->toSeconds('1d')); $this->assertEquals(214200, $this->d->toSeconds('2d 11h 30m')); $this->assertEquals(214214, $this->d->toSeconds('2d 11h 30m 14s')); } @@ -229,7 +321,7 @@ public function testConvertHumanizedStringToSeconds() public function testConvertHumanizedStringToSeconds7HourDay() { $d = new Duration(null, 7); - $this->assertEquals(25200, $d->toSeconds('1d')); + $this->assertEquals(25200, $d->toSeconds('1d')); $this->assertEquals(91800, $d->toSeconds('2d 11h 30m')); }