From 24ecea0d65f550415a4051fc1197e5718bc1b7d5 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 27 Aug 2023 19:40:33 +0200 Subject: [PATCH] Shifts: Fix day marker on start of day --- includes/view/ShiftCalendarRenderer.php | 10 ++++++---- src/Helpers/Carbon.php | 14 +++++++++++++- tests/Unit/Helpers/CarbonTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/includes/view/ShiftCalendarRenderer.php b/includes/view/ShiftCalendarRenderer.php index ad8542bc..387b911d 100644 --- a/includes/view/ShiftCalendarRenderer.php +++ b/includes/view/ShiftCalendarRenderer.php @@ -2,6 +2,7 @@ namespace Engelsystem; +use Engelsystem\Helpers\Carbon; use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\ShiftEntry; use Illuminate\Support\Collection; @@ -212,20 +213,21 @@ class ShiftCalendarRenderer */ private function renderTick($time, $label = false) { + $time = Carbon::createFromTimestamp($time); $class = $label ? 'tick bg-' . theme_type() : 'tick '; - if ($time % (24 * 60 * 60) == 23 * 60 * 60) { + if ($time->isStartOfDay()) { if (!$label) { return div($class . ' day'); } return div($class . ' day', [ - date(__('m-d'), $time) . '
' . date(__('H:i'), $time), + $time->format(__('m-d')) . '
' . $time->format(__('H:i')), ]); - } elseif ($time % (60 * 60) == 0) { + } elseif ($time->isStartOfHour()) { if (!$label) { return div($class . ' hour'); } return div($class . ' hour', [ - date(__('m-d'), $time) . '
' . date(__('H:i'), $time), + $time->format(__('m-d')) . '
' . $time->format(__('H:i')), ]); } return div($class); diff --git a/src/Helpers/Carbon.php b/src/Helpers/Carbon.php index c12747f5..931a74a8 100644 --- a/src/Helpers/Carbon.php +++ b/src/Helpers/Carbon.php @@ -18,7 +18,7 @@ class Carbon extends \Carbon\Carbon /** * Parses HTML datetime-local and ISO date/time strings. * - * @return \Carbon\Carbon|null Carbon if parseable, else null + * @return self|null Carbon if parseable, else null * @see self::DATETIME_FORMATS */ public static function createFromDatetime(string $value): ?\Carbon\Carbon @@ -43,4 +43,16 @@ class Carbon extends \Carbon\Carbon $carbon = self::createFromDateTime($value); return $carbon === null ? null : $carbon->timestamp; } + + /** + * Check if the instance is at the start of an hour. + * + * @param bool $checkMicroseconds check time at microseconds precision + */ + public function isStartOfHour(bool $checkMicroseconds = false): bool + { + return $checkMicroseconds + ? $this->rawFormat('i:s.u') === '00:00.000000' + : $this->rawFormat('i:s') === '00:00'; + } } diff --git a/tests/Unit/Helpers/CarbonTest.php b/tests/Unit/Helpers/CarbonTest.php index 3566dd81..d1d8b308 100644 --- a/tests/Unit/Helpers/CarbonTest.php +++ b/tests/Unit/Helpers/CarbonTest.php @@ -65,4 +65,28 @@ class CarbonTest extends TestCase $timestamp = Carbon::createTimestampFromDatetime($value); self::assertNull($timestamp); } + + public function startOfHourDates(): array + { + return [ + ['2022-04-16 10:00:00.000000', true], + ['2022-04-16 10:00:00.000000', true, true], + ['2022-04-16 10:00:00.123456', true, false], + ['2022-04-16 23:00:00.000000', true, false], + ['2022-04-16 10:00:42.000000', false], + ['2022-04-16 10:23:00.000000', false], + ['2022-04-16 10:00:00.123456', false, true], + ]; + } + + /** + * @covers \Engelsystem\Helpers\Carbon::isStartOfHour + * @dataProvider startOfHourDates + */ + public function testIsStartOfHour(string $value, bool $expected, bool $checkMicroseconds = false): void + { + $date = Carbon::createFromFormat('Y-m-d H:i:s.u', $value); + + $this->assertEquals($expected, $date->isStartOfHour($checkMicroseconds)); + } }