Skip to content

Commit

Permalink
Add holidays for korea (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
jws1837 committed Apr 29, 2024
1 parent bf6bfca commit 9b1817b
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/Countries/Korea.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Spatie\Holidays\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Calendars\ChineseCalendar;

class Korea extends Country
{
use ChineseCalendar;

public function countryCode(): string
{
return 'kr';
}

protected function allHolidays(int $year): array
{
return array_merge([
'신정' => '01-01',
'3ㆍ1절' => '03-01',
'어린이날' => '05-05',
'현충일' => '06-06',
'광복절' => '08-15',
'개천절' => '10-03',
'한글날' => '10-09',
'크리스마스' => '12-15',
], $this->variableHolidays($year));
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
$this->setChineseCalendarTimezone('Asia/Seoul');

return array_merge(
$this->getLunarNewYearHoliday($year),
$this->getLunarBuddhasBirthday($year),
$this->getLunarChuseok($year),
);
}

/** @return array<string, CarbonImmutable> */
protected function getLunarNewYearHoliday(int $year): array
{
$firstOfJanInChineseCalendar = $this->chineseToGregorianDate('01-01', $year);

return [
'설날 연휴 1' => $firstOfJanInChineseCalendar->subDay(),
'설날' => $firstOfJanInChineseCalendar,
'설날 연휴 2' => $firstOfJanInChineseCalendar->addDay(),
];
}

/** @return array<string, CarbonImmutable> */
protected function getLunarBuddhasBirthday(int $year): array
{
$BuddhasBirthdayInChineseCalendar = $this->chineseToGregorianDate('04-08', $year);

return [
'부처님 오신날' => $BuddhasBirthdayInChineseCalendar,
];
}

/** @return array<string, CarbonImmutable> */
protected function getLunarChuseok(int $year): array
{
$chuseokInChineseCalendar = $this->chineseToGregorianDate('08-15', $year);

return [
'추석 연휴 1' => $chuseokInChineseCalendar->subDay(),
'추석' => $chuseokInChineseCalendar,
'추석 연휴 2' => $chuseokInChineseCalendar->addDay(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"name": "\uc2e0\uc815",
"date": "2024-01-01"
},
{
"name": "\uc124\ub0a0 \uc5f0\ud734 1",
"date": "2024-02-09"
},
{
"name": "\uc124\ub0a0",
"date": "2024-02-10"
},
{
"name": "\uc124\ub0a0 \uc5f0\ud734 2",
"date": "2024-02-11"
},
{
"name": "3\u318d1\uc808",
"date": "2024-03-01"
},
{
"name": "\uc5b4\ub9b0\uc774\ub0a0",
"date": "2024-05-05"
},
{
"name": "\ubd80\ucc98\ub2d8 \uc624\uc2e0\ub0a0",
"date": "2024-05-15"
},
{
"name": "\ud604\ucda9\uc77c",
"date": "2024-06-06"
},
{
"name": "\uad11\ubcf5\uc808",
"date": "2024-08-15"
},
{
"name": "\ucd94\uc11d \uc5f0\ud734 1",
"date": "2024-09-16"
},
{
"name": "\ucd94\uc11d",
"date": "2024-09-17"
},
{
"name": "\ucd94\uc11d \uc5f0\ud734 2",
"date": "2024-09-18"
},
{
"name": "\uac1c\ucc9c\uc808",
"date": "2024-10-03"
},
{
"name": "\ud55c\uae00\ub0a0",
"date": "2024-10-09"
},
{
"name": "\ud06c\ub9ac\uc2a4\ub9c8\uc2a4",
"date": "2024-12-15"
}
]
115 changes: 115 additions & 0 deletions tests/Countries/KoreaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Spatie\Holidays\Tests\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Holidays;

it('can calculate korean holidays', function () {
CarbonImmutable::setTestNow('2024-01-01');

$holidays = Holidays::for(country: 'kr')->get();

expect($holidays)
->toBeArray()
->not()->toBeEmpty();

expect(formatDates($holidays))->toMatchSnapshot();
});

it('can calculate the Lunar New Year holiday', function ($year, $expectedHoliday) {
CarbonImmutable::setTestNow("{$year}-01-01");

$holidays = Holidays::for(country: 'kr')->get();

expect($holidays)
->toBeArray()
->not()->toBeEmpty();

$lunarNewYearHoliday = array_map(fn($date) => $date['date'], formatDates($holidays));

expect($lunarNewYearHoliday)->toContain(...$expectedHoliday);
})->with([
[
'year' => 2024, 'expected_holiday' => [
'2024-02-09', '2024-02-10', '2024-02-11'
]
],
[
'year' => 2023, 'expected_holiday' => [
'2023-01-21', '2023-01-22', '2023-01-23'
]
],
[
'year' => 2022, 'expected_holiday' => [
'2022-01-31', '2022-02-01', '2022-02-02'
]
],
[
'year' => 2021, 'expected_holiday' => [
'2021-02-11', '2021-02-12', '2021-02-13'
]
],
[
'year' => 2020, 'expected_holiday' => [
'2020-01-24', '2020-01-25', '2020-01-26'
]
],
]);

it('can calculate the holiday of Lunar Buddha\'s Birthday', function ($year, $expectedHoliday) {
CarbonImmutable::setTestNow("{$year}-01-01");

$holidays = Holidays::for(country: 'kr')->get();
expect($holidays)
->toBeArray()
->not()->toBeEmpty();

$dates = array_map(fn($date) => $date['date'], formatDates($holidays));

expect($dates)->toContain(...$expectedHoliday);
})->with([
['year' => 2024, 'expected_holiday' => ['2024-05-15']],
['year' => 2023, 'expected_holiday' => ['2023-05-26']],
['year' => 2022, 'expected_holiday' => ['2022-05-08']]
]);

it('can calculate the Lunar Chuseok holiday', function ($year, $expectedHoliday) {
CarbonImmutable::setTestNow("{$year}-01-01");

$holidays = Holidays::for(country: 'kr')->get();

expect($holidays)
->toBeArray()
->not()->toBeEmpty();

$lunarNewYearHoliday = array_map(fn($date) => $date['date'], formatDates($holidays));

expect($lunarNewYearHoliday)->toContain(...$expectedHoliday);
})->with([
[
'year' => 2024, 'expected_holiday' => [
'2024-09-16', '2024-09-17', '2024-09-18'
]
],
[
'year' => 2023, 'expected_holiday' => [
'2023-09-28', '2023-09-29', '2023-09-30'
]
],
[
'year' => 2022, 'expected_holiday' => [
'2022-09-09', '2022-09-10', '2022-09-11'
]
],
[
'year' => 2021, 'expected_holiday' => [
'2021-09-20', '2021-09-21', '2021-09-22'
]
],
[
'year' => 2020, 'expected_holiday' => [
'2020-09-30', '2020-10-01', '2020-10-02'
]
]
]);

0 comments on commit 9b1817b

Please sign in to comment.