From 0455f04245683d4122362e3af4aedd56d9f87864 Mon Sep 17 00:00:00 2001 From: Pisyek Date: Mon, 29 Jan 2024 23:29:29 +0800 Subject: [PATCH] refactor chinese calendar to use trait --- src/Countries/Malaysia.php | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/Countries/Malaysia.php b/src/Countries/Malaysia.php index d2d916061..b26043eb7 100644 --- a/src/Countries/Malaysia.php +++ b/src/Countries/Malaysia.php @@ -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 $regions */ @@ -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), @@ -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(