Shifts: Fix day marker on start of day

This commit is contained in:
Igor Scheller 2023-08-27 19:40:33 +02:00 committed by Michael Weimann
parent 5e702cd177
commit 24ecea0d65
3 changed files with 43 additions and 5 deletions

View File

@ -2,6 +2,7 @@
namespace Engelsystem; namespace Engelsystem;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Models\Shifts\Shift; use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftEntry; use Engelsystem\Models\Shifts\ShiftEntry;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -212,20 +213,21 @@ class ShiftCalendarRenderer
*/ */
private function renderTick($time, $label = false) private function renderTick($time, $label = false)
{ {
$time = Carbon::createFromTimestamp($time);
$class = $label ? 'tick bg-' . theme_type() : 'tick '; $class = $label ? 'tick bg-' . theme_type() : 'tick ';
if ($time % (24 * 60 * 60) == 23 * 60 * 60) { if ($time->isStartOfDay()) {
if (!$label) { if (!$label) {
return div($class . ' day'); return div($class . ' day');
} }
return div($class . ' day', [ return div($class . ' day', [
date(__('m-d'), $time) . '<br>' . date(__('H:i'), $time), $time->format(__('m-d')) . '<br>' . $time->format(__('H:i')),
]); ]);
} elseif ($time % (60 * 60) == 0) { } elseif ($time->isStartOfHour()) {
if (!$label) { if (!$label) {
return div($class . ' hour'); return div($class . ' hour');
} }
return div($class . ' hour', [ return div($class . ' hour', [
date(__('m-d'), $time) . '<br>' . date(__('H:i'), $time), $time->format(__('m-d')) . '<br>' . $time->format(__('H:i')),
]); ]);
} }
return div($class); return div($class);

View File

@ -18,7 +18,7 @@ class Carbon extends \Carbon\Carbon
/** /**
* Parses HTML datetime-local and ISO date/time strings. * 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 * @see self::DATETIME_FORMATS
*/ */
public static function createFromDatetime(string $value): ?\Carbon\Carbon public static function createFromDatetime(string $value): ?\Carbon\Carbon
@ -43,4 +43,16 @@ class Carbon extends \Carbon\Carbon
$carbon = self::createFromDateTime($value); $carbon = self::createFromDateTime($value);
return $carbon === null ? null : $carbon->timestamp; 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';
}
} }

View File

@ -65,4 +65,28 @@ class CarbonTest extends TestCase
$timestamp = Carbon::createTimestampFromDatetime($value); $timestamp = Carbon::createTimestampFromDatetime($value);
self::assertNull($timestamp); 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));
}
} }