Skip to content

Commit

Permalink
Merge pull request #16 from bobbybouwmann/feature/timezones
Browse files Browse the repository at this point in the history
Add the posibility to set a timezone
  • Loading branch information
sebastiandedeyne authored Nov 9, 2016
2 parents 96d8d0d + 1492998 commit 66c8ede
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/OpeningHours.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DateTimeInterface;
use DateTimeZone;
use Spatie\OpeningHours\Exceptions\Exception;
use Spatie\OpeningHours\Exceptions\InvalidDate;
use Spatie\OpeningHours\Exceptions\InvalidDayName;
Expand All @@ -17,8 +18,13 @@ class OpeningHours
/** @var array */
protected $exceptions = [];

public function __construct()
/** @var DateTimeZone|null */
protected $timezone;

public function __construct($timezone = null)
{
$this->timezone = $timezone ? new DateTimeZone($timezone) : null;

$this->openingHours = Day::mapDays(function () {
return new OpeningHoursForDay();
});
Expand Down Expand Up @@ -77,6 +83,8 @@ public function forDay(string $day): OpeningHoursForDay

public function forDate(DateTimeInterface $date): OpeningHoursForDay
{
$date = $this->applyTimezone($date);

return $this->exceptions[$date->format('Y-m-d')] ?? $this->forDay(Day::onDateTime($date));
}

Expand All @@ -97,6 +105,8 @@ public function isClosedOn(string $day): bool

public function isOpenAt(DateTimeInterface $dateTime): bool
{
$dateTime = $this->applyTimezone($dateTime);

$openingHoursForDay = $this->forDate($dateTime);

return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime));
Expand All @@ -117,6 +127,11 @@ public function isClosed(): bool
return $this->isClosedAt(new DateTime());
}

public function setTimezone($timezone)
{
$this->timezone = new DateTimeZone($timezone);
}

protected function parseOpeningHoursAndExceptions(array $data): array
{
$exceptions = Arr::pull($data, 'exceptions', []);
Expand Down Expand Up @@ -159,4 +174,13 @@ protected function normalizeDayName(string $day)

return $day;
}

protected function applyTimezone(DateTimeInterface $date)
{
if ($this->timezone) {
$date = $date->setTimezone($this->timezone);
}

return $date;
}
}
32 changes: 32 additions & 0 deletions tests/OpeningHoursTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\OpeningHours\Test;

use DateTime;
use DateTimeZone;
use Spatie\OpeningHours\OpeningHours;

class OpeningHoursTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -136,4 +137,35 @@ public function it_can_determine_that_its_open_at_a_certain_date_and_time_on_an_
$this->assertFalse($openingHours->isOpenAt($shouldBeClosed));
$this->assertTrue($openingHours->isClosedAt($shouldBeClosed));
}

/** @test */
public function it_can_set_the_timezone_on_the_openings_hours_object()
{
$openingHours = new OpeningHours('Europe/Amsterdam');
$openingHours->fill([
'monday' => ['09:00-18:00'],
'exceptions' => [
'2016-11-14' => ['09:00-13:00'],
],
]);

$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 10:00')));
$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 15:59')));
$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 08:00')));
$this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00')));

$this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 06:00', new DateTimeZone('Europe/Amsterdam'))));
$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:00', new DateTimeZone('Europe/Amsterdam'))));
$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 17:59', new DateTimeZone('Europe/Amsterdam'))));

$this->assertFalse($openingHours->isOpenAt(new DateTime('2016-11-14 17:59', new DateTimeZone('Europe/Amsterdam'))));
$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-11-14 12:59', new DateTimeZone('Europe/Amsterdam'))));

$this->assertFalse($openingHours->isOpenAt(new DateTime('2016-11-14 15:59', new DateTimeZone('America/Denver'))));
$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:59', new DateTimeZone('America/Denver'))));

date_default_timezone_set('America/Denver');
$this->assertTrue($openingHours->isOpenAt(new DateTime('2016-10-10 09:59')));
$this->assertFalse($openingHours->isOpenAt(new DateTime('2016-10-10 10:00')));
}
}

0 comments on commit 66c8ede

Please sign in to comment.