Skip to content

Commit

Permalink
Add Malaysia holiday (#17)
Browse files Browse the repository at this point in the history
* add Malaysia holiday date

* run test on Malaysia

* fix typo and add pesta kaamatan second day

* add region support

* update malaysia test snapshot

* update code to meet PHPStan analysis

* add Malaysia region validation

* update fix holiday to string and easter function

* update Malaysia test with region and invalid region

* refactor new year holiday into function

* update invalid region exception

* refactor chinese calendar to use trait

* update invalid region exception

* refactor CNY and Aidilfitri holiday function

* refactor new year holiday to string

* refactor islamic calendar to trait

* refactor chinese function to use trait

* refactor function placeholder

---------

Co-authored-by: Niels Vanpachtenbeke <[email protected]>
  • Loading branch information
pisyek and Nielsvanpach authored Oct 1, 2024
1 parent fb05c76 commit 109649a
Show file tree
Hide file tree
Showing 21 changed files with 1,948 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/Calendars/IslamicCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

namespace Spatie\Holidays\Calendars;

use Carbon\CarbonImmutable;
use DateTime;
use DateTimeZone;
use IntlCalendar;
use IntlDateFormatter;
use Carbon\CarbonPeriod;
use Carbon\CarbonImmutable;
use Carbon\Exceptions\InvalidFormatException;
use Spatie\Holidays\Countries\Country;
use Spatie\Holidays\Exceptions\InvalidYear;

/** @mixin Country */
trait IslamicCalendar
{
protected string $islamicCalendarTimezone = 'UTC';

/** @return array<CarbonPeriod> */
public function eidAlFitr(int $year, int $totalDays = 3): array
{
Expand Down Expand Up @@ -138,4 +144,47 @@ protected function getOverlapping(array $collection, int $year, int $totalDays):

return null;
}

public function setIslamicCalendarTimezone(string $islamicCalendarTimezone): static
{
$this->islamicCalendarTimezone = $islamicCalendarTimezone;

return $this;
}

protected function getIslamicCalendarFormatter(): IntlDateFormatter
{
return new IntlDateFormatter(
'en_US@calendar=islamic-civil',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'UTC',
IntlDateFormatter::TRADITIONAL,
'yyyy-MM-dd'
);
}

protected function getHijriYear(int $year, bool $nextYear = false): int
{
$formatter = $this->getIslamicCalendarFormatter();
$formatter->setPattern('yyyy');
$dateTime = DateTime::createFromFormat('d/m/Y', '01/01/' . ($nextYear ? $year + 1 : $year));

if (!$dateTime) {
throw InvalidYear::invalidHijriYear();
}

return (int) $formatter->format($dateTime->getTimestamp());
}

protected function islamicToGregorianDate(string $input, int $year, bool $nextYear = false): CarbonImmutable
{
$hijriYear = $this->getHijriYear(year: $year, nextYear: $nextYear);
$formatter = $this->getIslamicCalendarFormatter();
$timestamp = (int) $formatter->parse(sprintf('%s-%s', $hijriYear, $input));

return (new CarbonImmutable())
->setTimeStamp($timestamp)
->setTimezone(new DateTimeZone($this->islamicCalendarTimezone));
}
}
Loading

0 comments on commit 109649a

Please sign in to comment.