Skip to content

Commit

Permalink
refactor chinese calendar to use trait
Browse files Browse the repository at this point in the history
  • Loading branch information
pisyek committed Jan 29, 2024
1 parent 45ef871 commit 0455f04
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions src/Countries/Malaysia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
use Carbon\CarbonImmutable;
use DateTime;
use IntlDateFormatter;
use Spatie\Holidays\Calendars\ChineseCalendar;
use Spatie\Holidays\Exceptions\InvalidRegion;
use Spatie\Holidays\Exceptions\InvalidYear;

class Malaysia extends Country
{
use ChineseCalendar;

protected string $timezone = 'Asia/Kuala_Lumpur';

/** @var array<int, string> $regions */
Expand Down Expand Up @@ -57,9 +60,11 @@ protected function allHolidays(int $year): array
*/
protected function variableHolidays(int $year): array
{
$this->setChineseCalendarTimezone($this->timezone);

$variableHolidays = [
'Tahun Baru Cina' => $this->chineseCalendar('01-01', $year),
'Tahun Baru Cina Hari Kedua' => $this->chineseCalendar('01-02', $year),
'Tahun Baru Cina' => $this->chineseToGregorianDate('01-01', $year),
'Tahun Baru Cina Hari Kedua' => $this->chineseToGregorianDate('01-02', $year),
'Hari Raya Aidilfitri' => $this->islamicCalendar('01/10', $year),
'Hari Raya Aidilfitri Hari Kedua' => $this->islamicCalendar('02/10', $year),
'Hari Wesak' => $this->hariWesak($year),
Expand Down Expand Up @@ -412,24 +417,6 @@ private function newYear(): string
return '01-01';
}

/**
* $input as in 'month-day' string; '12-31'
*/
protected function chineseCalendar(string $input, int $year): CarbonImmutable
{
$formatter = new IntlDateFormatter(
locale: 'zh-CN@calendar=chinese',
dateType: IntlDateFormatter::SHORT,
timeType: IntlDateFormatter::NONE,
timezone: $this->timezone,
calendar: IntlDateFormatter::TRADITIONAL
);

$timestamp = (int) $formatter->parse(sprintf('%s-%s', $year, $input));

return CarbonImmutable::createFromTimestamp($timestamp, $this->timezone);
}

protected function getIslamicFormatter(): IntlDateFormatter
{
return new IntlDateFormatter(
Expand Down

0 comments on commit 0455f04

Please sign in to comment.