added floor and ceil rounding modes (#695)

This commit is contained in:
Simone Gasparini
2019-04-16 17:36:47 +02:00
committed by Kevin Papst
parent 136df3724f
commit 3251b26d9e
4 changed files with 460 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Timesheet\Rounding;
use App\Entity\Timesheet;
class CeilRounding implements RoundingInterface
{
/**
* @param Timesheet $record
* @param int $minutes
*/
public function roundBegin(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getBegin()->getTimestamp();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if (0 === $diff) {
return;
}
$record->getBegin()->setTimestamp($timestamp - $diff + $seconds);
}
/**
* @param Timesheet $record
* @param int $minutes
*/
public function roundEnd(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getEnd()->getTimestamp();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if (0 === $diff) {
return;
}
$record->getEnd()->setTimestamp($timestamp - $diff + $seconds);
}
/**
* @param Timesheet $record
* @param int $minutes
*/
public function roundDuration(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getDuration();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if (0 === $diff) {
return;
}
$record->setDuration($timestamp - $diff + $seconds);
}
}

View File

@@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Timesheet\Rounding;
use App\Entity\Timesheet;
class FloorRounding implements RoundingInterface
{
/**
* @param Timesheet $record
* @param int $minutes
*/
public function roundBegin(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getBegin()->getTimestamp();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if (0 === $diff) {
return;
}
$record->getBegin()->setTimestamp($timestamp - $diff);
}
/**
* @param Timesheet $record
* @param int $minutes
*/
public function roundEnd(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getEnd()->getTimestamp();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if (0 === $diff) {
return;
}
$record->getEnd()->setTimestamp($timestamp - $diff);
}
/**
* @param Timesheet $record
* @param int $minutes
*/
public function roundDuration(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getDuration();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if (0 === $diff) {
return;
}
$record->setDuration($timestamp - $diff);
}
}