Skip to content

Commit

Permalink
Merge pull request #17 from Calendart/fix-dates
Browse files Browse the repository at this point in the history
Fix an issue with start and end timezones
  • Loading branch information
Taluu committed Apr 5, 2016
2 parents a34c72a + cc8310c commit 2bad58a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,26 @@ public static function hydrate(array $data, Calendar $calendar = null)
$event->createdAt = new Datetime($data['createdDateTime']);
$event->updatedAt = new Datetime($data['lastModifiedDateTime']);

if (isset($data['start']) && isset($data['start']['dateTime'])) {
$event->start = new Datetime($data['start']['dateTime']);
}

if (isset($data['end']) && isset($data['end']['dateTime'])) {
$event->end = new Datetime($data['end']['dateTime']);
}

$windowsTimezone = new WindowsTimezone();
$startTimeZone = new DateTimeZone('UTC');
$endTimeZone = new DateTimeZone('UTC');

try {
$event->end->setTimezone(new DateTimeZone($windowsTimezone->getTimezone($data['end']['timeZone'])));
$endTimeZone = new DateTimeZone($windowsTimezone->getTimezone($data['end']['timeZone']));
} catch (Exception $e) { }

try {
$event->start->setTimezone(new DateTimeZone($windowsTimezone->getTimezone($data['start']['timeZone'])));
$startTimeZone = new DateTimeZone($windowsTimezone->getTimezone($data['start']['timeZone']));
} catch (Exception $e) { }

if (isset($data['start']) && isset($data['start']['dateTime'])) {
$event->start = new Datetime($data['start']['dateTime'], $startTimeZone);
}

if (isset($data['end']) && isset($data['end']['dateTime'])) {
$event->end = new Datetime($data['end']['dateTime'], $endTimeZone);
}

$event->recurrence = $data['recurrence'];
$event->allDay = true === $data['isAllDay'];
$event->cancelled = true === $data['isCancelled'];
Expand All @@ -303,9 +305,10 @@ public static function hydrate(array $data, Calendar $calendar = null)
$event->owner->addEvent($event);

$event->participations = new ArrayCollection;
$attendees = isset($data['attendees']) ? $data['attendees'] : [];

//now the fun stuff : the attendees
foreach ($data['attendees'] ?: [] as $attendee) {
foreach ($attendees as $attendee) {
// a resource is not an attendee
if ('resource' === $attendee['type']) {
continue;
Expand Down
39 changes: 39 additions & 0 deletions test/Model/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace CalendArt\Adapter\Office365\Model;

class EventTest extends \PHPUnit_Framework_TestCase
{
public function testHydrateDates()
{
$event = Event::hydrate([
'id' => 1,
'changeKey' => "foo",
'createdDateTime' => "2016-03-30T13:16:46.2214781Z",
'lastModifiedDateTime' => "2016-03-30T13:16:51.9873431Z",
'start' => [
"dateTime" => "2016-04-05T12:00:00.0000000",
"timeZone" => "UTC",
],
'end' => [
"dateTime" => "2016-04-05T15:00:00.0000000",
"timeZone" => "UTC",
],
"recurrence" => null,
"isAllDay" => false,
"isCancelled" => false,
"categories" => [],
"importance" => "normal",
"showAs" => "busy",
"type" => "singleInstance",
"organizer" => [
"emailAddress" => [
"name" => "John Doe",
"address" => "[email protected]",
],
],
]);

$this->assertEquals('2016-04-05T12:00:00+00:00', $event->getStart()->format('c'));
}
}

0 comments on commit 2bad58a

Please sign in to comment.