Skip to content

Commit

Permalink
added getHoliday() and fixed some timezone bugs (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreyrose authored Dec 26, 2022
1 parent dce017a commit e3b9cb0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
30 changes: 27 additions & 3 deletions src/USHolidays/Carbon.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ private function compareDate($a, $b)
*/
public function getHolidaysByYear($name, $year=null): array
{
$this->setTimezone('UTC');
$this->setTime(0,0);
// this is primarily for isBankHoliday() can get a list of holidays without a loop
$bankHolidayCheck = true;
if($name == 'no-bank-check') {
Expand Down Expand Up @@ -209,13 +211,13 @@ public function getHolidaysByYear($name, $year=null): array
*/
public function getHolidaysInDays($days, $holidays=null)
{
$this->setTime(0,0,0);
$this->setTimezone('UTC');
$this->setTime(0,0);

if($holidays === null || $holidays === 'all') {
$holidays = $this->holidayArray;
}



if($days > 0) {
$searchStartDate = $this->copy();
$searchEndDate = $this->copy()->addDays($days)->year;
Expand Down Expand Up @@ -251,6 +253,9 @@ public function getHolidaysInDays($days, $holidays=null)
*/
public function getHolidaysInYears($years, $holidays=null)
{
$this->setTimezone('UTC');
$this->setTime(0,0);

if($years > 0) {
$days = $this->diffInDays($this->copy()->addYears($years));
} else {
Expand Down Expand Up @@ -284,6 +289,25 @@ public function isHoliday()
return $isHoliday;
}

/**
* Get the holiday(s) complete object
*
* @return array|null
*/
public function getHoliday(): ?array
{
$holidays = $this->getHolidaysByYear('all');
$theHolidays = [];

foreach ($holidays as $holiday) {
if ($this->isBirthday($holiday->date)) {
$theHolidays[] = $holiday;
}
}

return $theHolidays;
}

/**
* Set Business Holidays
*
Expand Down
4 changes: 2 additions & 2 deletions src/USHolidays/Traits/Holiday.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private function holidays( $year = null ) {
},
'bank_holiday' => true,
'federal_holiday' => true,
'start_year' => 1776,
'start_year' => 1777,
'end_year' => null,
'bank_holiday_start_year' => 1941,
'bank_holiday_end_year' => null,
Expand Down Expand Up @@ -405,7 +405,7 @@ private function holidays( $year = null ) {
},
'bank_holiday' => true,
'federal_holiday' => true,
'start_year' => 1865,
'start_year' => 1866,
'end_year' => null,
'bank_holiday_start_year' => 2021,
'bank_holiday_end_year' => null,
Expand Down
12 changes: 12 additions & 0 deletions tests/CarbonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public function testHolidayName()

}

public function testGetHoliday()
{
// 07/04 - Saturday
$carbon = new Carbon();
$holiday = Carbon::create(2020, 1, 1)->getIndependenceDayHoliday()->date;

$this->assertEquals("Independence Day", $holiday->getHoliday()[0]->name);
$this->assertEquals(Carbon::create(2020, 1, 1)->getIndependenceDayHoliday()->date, $holiday->getHoliday()[0]->date);


}

public function testIsHoliday()
{
$carbon = new Carbon();
Expand Down

0 comments on commit e3b9cb0

Please sign in to comment.