diff --git a/config/config.default.php b/config/config.default.php index 6c81968a..bee87e45 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -350,8 +350,8 @@ return [ // Goodies must be enabled to use this feature 'night_shifts' => [ 'enabled' => (bool) env('NIGHT_SHIFTS', true), // Disable to weigh every shift the same - 'start' => env('NIGHT_SHIFTS_START', 2), - 'end' => env('NIGHT_SHIFTS_END', 8), + 'start' => env('NIGHT_SHIFTS_START', 2), // Starting from hour + 'end' => env('NIGHT_SHIFTS_END', 8), // Ends at (without including) hour 'multiplier' => env('NIGHT_SHIFTS_MULTIPLIER', 2), ], diff --git a/includes/model/User_model.php b/includes/model/User_model.php index e1da6add..32db5ac5 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -128,8 +128,11 @@ function User_get_shifts_sum_query() ' COALESCE(SUM( (1 + ( - HOUR(shifts.start) > %1$d AND HOUR(shifts.start) < %2$d - OR HOUR(shifts.end) > %1$d AND HOUR(shifts.end) < %2$d + HOUR(shifts.start) >= %1$d AND HOUR(shifts.start) < %2$d + OR ( + HOUR(shifts.end) > %1$d + || HOUR(shifts.end) = %1$d AND MINUTE(shifts.end) > 0 + ) AND HOUR(shifts.end) <= %2$d OR HOUR(shifts.start) <= %1$d AND HOUR(shifts.end) >= %2$d )) * (UNIX_TIMESTAMP(shifts.end) - UNIX_TIMESTAMP(shifts.start)) diff --git a/src/Helpers/Shifts.php b/src/Helpers/Shifts.php index c75611e1..6cb9c8ed 100644 --- a/src/Helpers/Shifts.php +++ b/src/Helpers/Shifts.php @@ -18,8 +18,11 @@ class Shifts /** @see User_get_shifts_sum_query to keep it in sync */ return $config['enabled'] && ( - $start->hour > $config['start'] && $start->hour < $config['end'] - || $end->hour > $config['start'] && $end->hour < $config['end'] + $start->hour >= $config['start'] && $start->hour < $config['end'] + || ( + $end->hour > $config['start'] + || $end->hour == $config['start'] && $end->minute > 0 + ) && $end->hour <= $config['end'] || $start->hour <= $config['start'] && $end->hour >= $config['end'] ); } diff --git a/tests/Unit/Helpers/ShiftsTest.php b/tests/Unit/Helpers/ShiftsTest.php index 0b4f6756..e2863427 100644 --- a/tests/Unit/Helpers/ShiftsTest.php +++ b/tests/Unit/Helpers/ShiftsTest.php @@ -19,7 +19,7 @@ class ShiftsTest extends TestCase $config = new Config(['night_shifts' => [ 'enabled' => false, 'start' => 2, - 'end' => 6, + 'end' => 8, 'multiplier' => 2, ]]); $this->app->instance('config', $config); @@ -40,16 +40,24 @@ class ShiftsTest extends TestCase return [ // Is night shift ['2042-01-01 04:00', '2042-01-01 05:00', true], + // Is night shift + ['2042-01-01 02:00', '2042-01-01 02:15', true], + // Is night shift + ['2042-01-01 07:45', '2042-01-01 08:00', true], // Starts as night shift - ['2042-01-01 05:45', '2042-01-01 07:00', true], + ['2042-01-01 07:59', '2042-01-01 09:00', true], // Ends as night shift - ['2042-01-01 00:00', '2042-01-01 03:00', true], + ['2042-01-01 00:00', '2042-01-01 02:01', true], + // Equals night shift + ['2042-01-01 02:00', '2042-01-01 08:00', true], // Contains night shift ['2042-01-01 01:00', '2042-01-01 09:00', true], // Too early ['2042-01-01 00:00', '2042-01-01 02:00', false], // Too late - ['2042-01-01 06:00', '2042-01-01 10:00', false], + ['2042-01-01 08:00', '2042-01-01 10:00', false], + // Out of range + ['2042-01-01 23:00', '2042-01-02 01:00', false], ]; } @@ -62,7 +70,7 @@ class ShiftsTest extends TestCase $config = new Config(['night_shifts' => [ 'enabled' => true, 'start' => 2, - 'end' => 6, + 'end' => 8, 'multiplier' => 2, ]]); $this->app->instance('config', $config); @@ -78,7 +86,7 @@ class ShiftsTest extends TestCase $config = new Config(['night_shifts' => [ 'enabled' => true, 'start' => 2, - 'end' => 6, + 'end' => 8, 'multiplier' => 2, ]]); $this->app->instance('config', $config);