diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index 1778c83ea86e..1fd7bc7f5723 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -113,7 +113,7 @@ class TaskRepository extends BaseRepository foreach($time_log as $key => $value) { - $time_log[$key][1] = $this->roundTimeLog($time_log[$key][1]); + $time_log[$key][1] = $this->roundTimeLog($time_log[$key][0], $time_log[$key][1]); } if (isset($data['action'])) { @@ -252,17 +252,31 @@ class TaskRepository extends BaseRepository return $task; } - public function roundTimeLog(int $end_time): int + public function roundTimeLog(int $start_time, int $end_time): int { if($this->task_round_to_nearest == 1) return $end_time; - if($this->task_round_up) - return (int)ceil($end_time/$this->task_round_to_nearest)*$this->task_round_to_nearest; + $interval = $end_time - $start_time; + + if($this->task_round_up) + return $start_time + (int)ceil($interval/$this->task_round_to_nearest)*$this->task_round_to_nearest; + + return $start_time - (int)floor($interval/$this->task_round_to_nearest) * $this->task_round_to_nearest; - return (int)floor($end_time/$this->task_round_to_nearest) * $this->task_round_to_nearest; } + // public function roundTimeLog(int $end_time): int + // { + // if($this->task_round_to_nearest == 1) + // return $end_time; + + // if($this->task_round_up) + // return (int)ceil($end_time/$this->task_round_to_nearest)*$this->task_round_to_nearest; + + // return (int)floor($end_time/$this->task_round_to_nearest) * $this->task_round_to_nearest; + // } + public function stop(Task $task) { $this->init($task); @@ -272,7 +286,7 @@ class TaskRepository extends BaseRepository $last = end($log); if (is_array($last) && $last[1] === 0) { - $last[1] = $this->roundTimeLog(time()); + $last[1] = $this->roundTimeLog($last[0], time()); array_pop($log); $log = array_merge($log, [$last]);//check at this point, it may be prepending here. diff --git a/tests/Unit/TaskRoundingTest.php b/tests/Unit/TaskRoundingTest.php index 4ace3afa45df..96a8e1a9c4db 100644 --- a/tests/Unit/TaskRoundingTest.php +++ b/tests/Unit/TaskRoundingTest.php @@ -42,6 +42,79 @@ class TaskRoundingTest extends TestCase } + public function testRoundUp2() + { + + + + $start_time = 1715237056; + $end_time = $start_time + 60*7; + $this->task_round_to_nearest = 600; + + $rounded = $start_time + 60*10; + + $this->assertEquals($rounded, $this->roundTimeLog($start_time, $end_time)); + + + } + + public function testRoundUp3() + { + + + $start_time = 1715213100; + $end_time = $start_time + 60*15; + $this->task_round_to_nearest = 900; + + $rounded = $start_time + 60*15; + + $this->assertEquals($rounded, $this->roundTimeLog($start_time, $end_time)); + + +$s = \Carbon\Carbon::createFromTimestamp($start_time); + +$e = \Carbon\Carbon::createFromTimestamp($end_time); + + $x = \Carbon\Carbon::createFromTimestamp($rounded); + + +// echo $s->format('Y-m-d H:i:s').PHP_EOL; +// echo $e->format('Y-m-d H:i:s').PHP_EOL; +// echo $x->format('Y-m-d H:i:s').PHP_EOL; + + + } + +// public function testRoundUp4() +// { + + + +// $start_time = 1715238900; +// $end_time = 1715238000; +// // $end_time = $start_time + 60*15; +// $this->task_round_to_nearest = 900; + +// $rounded = $start_time + 60*15; + +// $this->assertEquals($rounded, $this->roundTimeLog($start_time, $end_time)); + + +// $s = \Carbon\Carbon::createFromTimestamp($start_time); + +// $e = \Carbon\Carbon::createFromTimestamp($end_time); + +// $x = \Carbon\Carbon::createFromTimestamp($rounded); + + +// echo $s->format('Y-m-d H:i:s').PHP_EOL; +// echo $e->format('Y-m-d H:i:s').PHP_EOL; +// echo $x->format('Y-m-d H:i:s').PHP_EOL; + + +// } + + public function testRoundDown() { $start_time = 1714942800;