diff --git a/app/Facades/Utils.php b/app/Facades/Utils.php index 9245b19a..6fb1dd97 100644 --- a/app/Facades/Utils.php +++ b/app/Facades/Utils.php @@ -23,12 +23,16 @@ class Utils extends Facade public static function minutesToTimeParts($minutes): array { - return self::secondsToTimeParts(self::minutesToSeconds($minutes)); + $hours = floor($minutes / 60); + $minutes %= 60; + + return ['h' => $hours, 'm' => $minutes]; } public static function minutesToTimeString($minutes): string { - return self::secondsToTimeString(self::minutesToSeconds($minutes)); + $hm = self::minutesToTimeParts($minutes); + return $hm['h']. 'h '. $hm['m'] . 'm'; } /** @@ -38,8 +42,8 @@ class Utils extends Facade */ public static function secondsToTimeParts($seconds): array { - $dtF = new \DateTime('@0'); - $dtT = new \DateTime("@$seconds"); + $dtF = new \DateTime('@0', new \DateTimeZone('UTC')); + $dtT = new \DateTime("@$seconds", new \DateTimeZone('UTC')); $t = $dtF->diff($dtT); diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index a7e13d52..7969ef47 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -25,7 +25,8 @@ class UtilsTest extends TestCase $this->assertEquals(['h' => 0, 'm' => 1, 's' => 2], $t); } - public function testSecondsToTime() { + public function testSecondsToTime() + { $t = Utils::secondsToTimeString(3600); $this->assertEquals('1h 0m', $t); @@ -38,4 +39,16 @@ class UtilsTest extends TestCase $t = Utils::secondsToTimeString(3722, true); $this->assertEquals('1h 2m 2s', $t); } + + public function testMinutesToTime() + { + $t = Utils::minutesToTimeParts(65); + $this->assertEquals(['h' => 1, 'm' => 5], $t); + + $t = Utils::minutesToTimeString(65); + $this->assertEquals('1h 5m', $t); + + $t = Utils::minutesToTimeString(43200); + $this->assertEquals('720h 0m', $t); + } }