Skip to content

Commit

Permalink
Util: Implement roundToNearestThirtyMinutes()
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed May 24, 2024
1 parent 9306896 commit 30f0a16
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
25 changes: 4 additions & 21 deletions library/Notifications/Widget/Calendar/BaseGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ final protected function yieldFlowingEntries(Traversable $entries): Generator
/** @var SplObjectStorage<Entry, int[][]> $occupiedCells */
$occupiedCells = new SplObjectStorage();
foreach ($entries as $entry) {
$actualStart = $this->roundToNearestThirtyMinute($entry->getStart());
$actualStart = Util::roundToNearestThirtyMinute($entry->getStart());
if ($actualStart < $gridStartsAt) {
$entryStartPos = 0;
} else {
$entryStartPos = Util::diffHours($gridStartsAt, $actualStart) * 2;
}

$actualEnd = $this->roundToNearestThirtyMinute($entry->getEnd());
$actualEnd = Util::roundToNearestThirtyMinute($entry->getEnd());
if ($actualEnd > $gridEndsAt) {
$entryEndPos = $amountOfDays * 48;
} else {
Expand Down Expand Up @@ -449,14 +449,14 @@ final protected function yieldFixedEntries(Traversable $entries): Generator
$lastRow = $rowStart;
}

$actualStart = $this->roundToNearestThirtyMinute($entry->getStart());
$actualStart = Util::roundToNearestThirtyMinute($entry->getStart());
if ($actualStart < $gridStartsAt) {
$colStart = 0;
} else {
$colStart = Util::diffHours($gridStartsAt, $actualStart) * 2;
}

$actualEnd = $this->roundToNearestThirtyMinute($entry->getEnd());
$actualEnd = Util::roundToNearestThirtyMinute($entry->getEnd());
if ($actualEnd > $gridEndsAt) {
$colEnd = $gridBorderAt;
} else {
Expand Down Expand Up @@ -643,23 +643,6 @@ protected function assembleEntry(BaseHtmlElement $html, Entry $entry, ?string $c
$entryContainer->addHtml($content);
}

protected function roundToNearestThirtyMinute(DateTime $time): DateTime
{
$hour = (int) $time->format('H');
$minute = (int) $time->format('i');

$time = clone $time;
if ($minute < 15) {
$time->setTime($hour, 0);
} elseif ($minute >= 45) {
$time->setTime($hour + 1, 0);
} else {
$time->setTime($hour, 30);
}

return $time;
}

/**
* Get the given attendee's color with the given transparency suitable for CSS
*
Expand Down
17 changes: 17 additions & 0 deletions library/Notifications/Widget/Calendar/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,21 @@ public static function diffHours(DateTime $from, DateTime $to)

return $hours;
}

public static function roundToNearestThirtyMinute(DateTime $time): DateTime
{
$hour = (int) $time->format('H');
$minute = (int) $time->format('i');

$time = clone $time;
if ($minute < 15) {
$time->setTime($hour, 0);
} elseif ($minute >= 45) {
$time->setTime($hour + 1, 0);
} else {
$time->setTime($hour, 30);
}

return $time;
}
}

0 comments on commit 30f0a16

Please sign in to comment.