From 3fa1ea1f4f3daf33edb9f562ba78225f8dcf6eaf Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 25 Jun 2013 18:19:15 +0200 Subject: [PATCH] Add TimeCode::fromSeconds method --- src/FFMpeg/Coordinate/TimeCode.php | 26 +++++++++++++++++++ .../FFMpeg/Tests/Coordinate/TimeCodeTest.php | 22 ++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/FFMpeg/Coordinate/TimeCode.php b/src/FFMpeg/Coordinate/TimeCode.php index 7ae7fdb..3c27fa9 100644 --- a/src/FFMpeg/Coordinate/TimeCode.php +++ b/src/FFMpeg/Coordinate/TimeCode.php @@ -63,4 +63,30 @@ class TimeCode return new static($hours, $minutes, $seconds, $frames); } + + /** + * Creates timecode from number of seconds. + * + * @param float $seconds + * + * @return TimeCode + */ + public static function fromSeconds($quantity) + { + $minutes = $hours = $frames = 0; + + $frames = round(100 * ($quantity - floor($quantity))); + $seconds = floor($quantity); + + if ($seconds > 59) { + $minutes = floor($seconds / 60); + $seconds = $seconds % 60; + } + if ($minutes > 59) { + $hours = floor($minutes / 60); + $minutes = $minutes % 60; + } + + return new static($hours, $minutes, $seconds, $frames); + } } diff --git a/tests/FFMpeg/Tests/Coordinate/TimeCodeTest.php b/tests/FFMpeg/Tests/Coordinate/TimeCodeTest.php index 241ee47..894f947 100644 --- a/tests/FFMpeg/Tests/Coordinate/TimeCodeTest.php +++ b/tests/FFMpeg/Tests/Coordinate/TimeCodeTest.php @@ -35,4 +35,26 @@ class TimeCodeTest extends TestCase { TimeCode::fromString('lalali lala'); } + + /** + * @dataProvider provideSeconds + */ + public function testFromSeconds($seconds, $expected) + { + $tc = TimeCode::fromSeconds($seconds); + $this->assertEquals($expected, (string) $tc); + } + + public function provideSeconds() + { + return array( + array(0.467, '00:00:00.47'), + array(12.467, '00:00:12.47'), + array(59.867, '00:00:59.87'), + array(72.467, '00:01:12.47'), + array(3599.467, '00:59:59.47'), + array(3600.467, '01:00:00.47'), + array(86422.467, '24:00:22.47'), + ); + } }