Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vietnamese holidays #115

Merged
merged 26 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d41c432
add Vietnamese holidays according to the standard calendar.
kevinb1989 Jan 18, 2024
5997d03
include Vietnamese holidays according to Chinese Calendar.
kevinb1989 Jan 19, 2024
95dc2b1
refactor the a Chinese calendar trait.
kevinb1989 Jan 19, 2024
145bff8
update the Lunar New Year's Eve.
kevinb1989 Jan 19, 2024
a7fdadc
re-add snapshots for countries other than Vietnam.
kevinb1989 Jan 19, 2024
6ad7eb8
use Vietnamese to name the holidays, English names commented.
kevinb1989 Jan 19, 2024
6ba1739
For Vietnam, use the country code of vn instead of vi.
kevinb1989 Jan 19, 2024
b7fe5c8
refactor according to PHPStan.
kevinb1989 Jan 19, 2024
2461db9
specify a type for the $timezone parameter.
kevinb1989 Jan 19, 2024
f6ca7c4
Refactor according to PHPStan.
kevinb1989 Jan 19, 2024
f07dccf
In Vietnam, a day before or after the Independence Day can be a publi…
kevinb1989 Jan 21, 2024
807e297
Merge branch 'main' into add-vietnamese-holidays
kevinb1989 Jan 21, 2024
adbedf9
work out some logics to calculate the extra day off for Independence …
kevinb1989 Jan 21, 2024
96f6c82
add a few test cases to make sure the Lunar New Year holiday in Vietn…
kevinb1989 Jan 22, 2024
873fa04
Add test cases for the holiday of independence.
kevinb1989 Jan 22, 2024
9dde2a8
add doc blocks.
kevinb1989 Jan 22, 2024
608bff9
remove empty line.
kevinb1989 Jan 22, 2024
16b7a4f
rename an instance variable.
kevinb1989 Jan 22, 2024
bd94b6d
add two more days to the Lunar Year Year holiday.
kevinb1989 Jan 24, 2024
4a6d3dc
refactor from Y/m/d to Y-m-d for Chinese Calendar.
kevinb1989 Jan 24, 2024
15569f1
Merge branch 'main' into add-vietnamese-holidays
kevinb1989 Jan 24, 2024
82dcfd9
add a few more test cases for Lunar New Year.
kevinb1989 Jan 24, 2024
5303d33
refactor the Lunar New Year holiday.
kevinb1989 Jan 24, 2024
6f20dd5
refactor and add a few more test cases.
kevinb1989 Jan 24, 2024
510b6f6
Merge branch 'main' into add-vietnamese-holidays
kevinb1989 Jan 27, 2024
f32f9a4
add translations for vietnam
kevinb1989 Jan 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Calendars/ChineseCalendar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Spatie\Holidays\Calendars;

use DateTimeZone;
use IntlDateFormatter;

trait ChineseCalendar
kevinb1989 marked this conversation as resolved.
Show resolved Hide resolved
{
protected $timezone = 'Asia/Shanghai';

protected function chineseToGregorianDate(string $input, int $year): string
{
$timeStamp = $this->getFormatter()->parse($year . '/' . $input);
$dateTime = date_create()->setTimeStamp($timeStamp)->setTimezone(new DateTimeZone($this->timezone));

return $dateTime->format('m-d');
}

protected function getFormatter(): IntlDateFormatter
{
return new IntlDateFormatter(
locale: 'zh-CN@calendar=chinese',
dateType: IntlDateFormatter::SHORT,
timeType: IntlDateFormatter::NONE,
timezone: $this->timezone,
calendar: IntlDateFormatter::TRADITIONAL
);
}
}
41 changes: 41 additions & 0 deletions src/Countries/Vietnam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Spatie\Holidays\Countries;

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

class Vietnam extends Country
{
use ChineseCalendar;

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

protected function allHolidays(int $year): array
{
return array_merge([
'New Year\'s day' => '01-01',
'Reunification Day' => '04-30',
'Labour Day' => '05-01',
'Independence Day' => '09-02',
], $this->variableHolidays($year));
}

/** @return array<string, CarbonImmutable> */
protected function variableHolidays(int $year): array
{
return [
'Lunar New Year\'s Eve' => $this->chineseToGregorianDate('12/30', $year - 1),
'Lunar New Year Day 1' => $this->chineseToGregorianDate('01/01', $year),
'Lunar New Year Day 2' => $this->chineseToGregorianDate('01/02', $year),
'Lunar New Year Day 3' => $this->chineseToGregorianDate('01/03', $year),
'Lunar New Year Day 4' => $this->chineseToGregorianDate('01/04', $year),
'Hung Kings\' Festival' => $this->chineseToGregorianDate('03/10', $year),
kevinb1989 marked this conversation as resolved.
Show resolved Hide resolved
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"name": "New Year's day",
"date": "2024-01-01"
},
{
"name": "Lunar New Year's Eve",
"date": "2024-02-09"
},
{
"name": "Lunar New Year Day 1",
"date": "2024-02-10"
},
{
"name": "Lunar New Year Day 2",
"date": "2024-02-11"
},
{
"name": "Lunar New Year Day 3",
"date": "2024-02-12"
},
{
"name": "Lunar New Year Day 4",
"date": "2024-02-13"
},
{
"name": "Hung Kings' Festival",
"date": "2024-04-18"
},
{
"name": "Reunification Day",
"date": "2024-04-30"
},
{
"name": "Labour Day",
"date": "2024-05-01"
},
{
"name": "Independence Day",
"date": "2024-09-02"
}
]
19 changes: 19 additions & 0 deletions tests/Countries/VietnamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\Holidays\Tests\Countries;

use Carbon\CarbonImmutable;
use Spatie\Holidays\Countries\Vietnam;
use Spatie\Holidays\Holidays;

it('can calculate vietnamese holidays', function () {
CarbonImmutable::setTestNowAndTimezone('2024-01-01', 'Asia/Ho_Chi_Minh');

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

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

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