Skip to content

Commit

Permalink
Disable Prayer Day in Denmark starting from 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Mar 24, 2024
1 parent a623e6d commit 62babf7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/Cmixin/BusinessDay/Calculator/CalculatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,25 @@ protected function consumeHolidayString($pattern, &$holiday, &$match = null)

protected function isIgnoredYear(&$holiday)
{
$since = 0;
$mode = 'since';
$cap = 0;
$every = 0;

if ($this->consumeHolidayString('/ of (even|odd|leap|non-leap) years?$/i', $holiday, $match)) {
if (substr($match[1], -4) === 'leap') {
return (!($this->year % 4) && ($this->year % 100 || !($this->year % 400))) !== ($match[1] === 'leap');
}

$since = $match[1] === 'even' ? 0 : 1;
$cap = $match[1] === 'even' ? 0 : 1;
$every = 2;
}

if ($this->consumeHolidayString('/ every (\d+) years since (\d{4})$/', $holiday, $match)) {
$since = $match[2];
if ($this->consumeHolidayString('/ every (\d+) years (since|until) (\d{4})$/', $holiday, $match)) {
$mode = $match[2];
$cap = $match[3];
$every = $match[1];
}

return $every && ($delta = $this->year - $since) >= 0 && $delta % $every;
return $every && ($delta = ($mode === 'since' ? 1 : -1) * ($this->year - $cap)) >= 0 && $delta % $every;
}
}
2 changes: 2 additions & 0 deletions src/Cmixin/BusinessDay/Calculator/HolidayCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ protected function getKeywordReplacements(): array
$equinox => [$this, 'interpolateEquinox'],
'/(easter|orthodox)/i' => [$this, 'interpolateFixedDate'],
'/\D-\d+\s*$/' => '$0 days',
'/(\D-\d+)\s+if/' => '$1 days if',
'/^(\d{1,2})-(\d{1,2})((\s[\s\S]*)?)$/' => [$this, 'padDate'],
'/(\s\d+)\s*$/' => '$1 days',
'/(\s\d+)\s+if/' => '$1 days if',
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Cmixin/Holidays/dk-national.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'easter-2' => '= easter -2',
'easter' => '= easter',
'easter-p1' => '= easter 1',
'easter-26' => '= easter 26',
'easter-26' => '= easter 26 if year <= 2023',
'easter-39' => '= easter 39',
'2nd-sunday-of-may' => '= second Sunday of May',
'06-05' => '06-05',
Expand Down
34 changes: 34 additions & 0 deletions tests/Cmixin/Holidays/DkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Cmixin\Holidays;

use Cmixin\BusinessDay;
use PHPUnit\Framework\TestCase;

class DkTest extends TestCase
{
const CARBON_CLASS = 'Carbon\Carbon';

protected function setUp(): void
{
BusinessDay::enable(static::CARBON_CLASS);
$carbon = static::CARBON_CLASS;
$carbon::resetHolidays();
}

public function testHolidays(): void
{
$carbon = static::CARBON_CLASS;
$carbon::resetHolidays();
$carbon::setHolidaysRegion('dk-national');

self::assertTrue($carbon::parse('2023-05-05')->isHoliday());
self::assertSame('easter-26', $carbon::parse('2023-05-05')->getHolidayId());
self::assertSame('Prayer Day', $carbon::parse('2023-05-05')->getHolidayName());
self::assertSame('Store Bededag', $carbon::parse('2023-05-05')->getHolidayName('da'));
self::assertFalse($carbon::parse('2024-04-26')->isHoliday());
self::assertFalse($carbon::parse('2024-04-26')->getHolidayId());
self::assertFalse($carbon::parse('2024-04-26')->getHolidayName());
self::assertFalse($carbon::parse('2024-04-26')->getHolidayName('da'));
}
}

0 comments on commit 62babf7

Please sign in to comment.