Skip to content

Commit

Permalink
Add toggle setting for applying time zone definitions when parsing re…
Browse files Browse the repository at this point in the history
…currence rule data
  • Loading branch information
u01jmg3 committed Dec 16, 2016
1 parent 9e0ef48 commit 4de3963
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ $ curl -s https://getcomposer.org/installer | php

#### Configurable Constants / Variables

| Name | Type | Description |
|---------------------|----------|------------------------------------------------------------|
| `UNIX_MIN_YEAR` | Constant | Minimum UNIX year to use |
| `DATE_FORMAT` | Constant | Default date format to use |
| `TIME_FORMAT` | Constant | Default time format to use |
| `DATE_TIME_FORMAT` | Constant | Default datetime format to use |
| `$todoCount` | Variable | Track the number of todos in the current iCal feed |
| `$eventCount` | Variable | Track the number of events in the current iCal feed |
| `$freebusyCount` | Variable | Track the freebusy count in the current iCal feed |
| `$cal` | Variable | The parsed calendar |
| `$defaultSpan` | Variable | The value in years to use for indefinite, recurring events |
| `$defaultWeekStart` | Variable | The two letter representation of the first day of the week |
| Name | Type | Description |
|--------------------------|----------|--------------------------------------------------------------------|
| `UNIX_MIN_YEAR` | Constant | Minimum UNIX year to use |
| `DATE_FORMAT` | Constant | Default date format to use |
| `TIME_FORMAT` | Constant | Default time format to use |
| `DATE_TIME_FORMAT` | Constant | Default datetime format to use |
| `$todoCount` | Variable | Track the number of todos in the current iCal feed |
| `$eventCount` | Variable | Track the number of events in the current iCal feed |
| `$freebusyCount` | Variable | Track the freebusy count in the current iCal feed |
| `$cal` | Variable | The parsed calendar |
| `$defaultSpan` | Variable | The value in years to use for indefinite, recurring events |
| `$defaultWeekStart` | Variable | The two letter representation of the first day of the week |
| `$useTimeZoneWithRRules` | Variable | Toggle whether to use time zone info when parsing recurrence rules |

#### Functions

Expand Down
23 changes: 15 additions & 8 deletions src/ICal/ICal.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class ICal
*/
public $defaultWeekStart = 'MO';

/**
* Toggle whether to use time zone info when parsing recurrence rules
*
* @var boolean
*/
public $useTimeZoneWithRRules = false;

/**
* Variable to track the previous keyword
*
Expand Down Expand Up @@ -627,7 +634,7 @@ public function processRecurrences()

$isAllDayEvent = strlen($anEvent['DTSTART_array'][1]) === 8 ? true : false;

$initialStart = new \DateTime($anEvent['DTSTART_array'][1], isset($initialStartTimeZone) ? new \DateTimeZone($initialStartTimeZone) : null);
$initialStart = new \DateTime($anEvent['DTSTART_array'][1], ($this->useTimeZoneWithRRules && isset($initialStartTimeZone)) ? new \DateTimeZone($initialStartTimeZone) : null);
$initialStartOffset = $initialStart->getOffset();
$initialStartTimeZoneName = $initialStart->getTimezone()->getName();

Expand All @@ -638,7 +645,7 @@ public function processRecurrences()
unset($initialEndTimeZone);
}

$initialEnd = new \DateTime($anEvent['DTEND_array'][1], isset($initialEndTimeZone) ? new \DateTimeZone($initialEndTimeZone) : null);
$initialEnd = new \DateTime($anEvent['DTEND_array'][1], ($this->useTimeZoneWithRRules && isset($initialEndTimeZone)) ? new \DateTimeZone($initialEndTimeZone) : null);
$initialEndOffset = $initialEnd->getOffset();
$initialEndTimeZoneName = $initialEnd->getTimezone()->getName();
} else {
Expand Down Expand Up @@ -751,7 +758,7 @@ public function processRecurrences()

// Adjust timezone from initial event
$recurringTimeZone = \DateTime::createFromFormat('U', $dayRecurringTimestamp);
$timezoneOffset = $initialStart->getTimezone()->getOffset($recurringTimeZone);
$timezoneOffset = ($this->useTimeZoneWithRRules) ? $initialStart->getTimezone()->getOffset($recurringTimeZone) : 0;
$dayRecurringTimestamp += ($timezoneOffset !== $initialStartOffset) ? $initialStartOffset - $timezoneOffset : 0;

// Add event
Expand Down Expand Up @@ -831,7 +838,7 @@ public function processRecurrences()

// Adjust timezone from initial event
$dayRecurringTimeZone = \DateTime::createFromFormat('U', $dayRecurringTimestamp);
$timezoneOffset = $initialStart->getTimezone()->getOffset($dayRecurringTimeZone);
$timezoneOffset = ($this->useTimeZoneWithRRules) ? $initialStart->getTimezone()->getOffset($dayRecurringTimeZone) : 0;
$dayRecurringTimestamp += ($timezoneOffset !== $initialStartOffset) ? $initialStartOffset - $timezoneOffset : 0;

foreach ($weekdays as $day) {
Expand Down Expand Up @@ -927,7 +934,7 @@ public function processRecurrences()

// Adjust timezone from initial event
$recurringTimeZone = \DateTime::createFromFormat('U', $monthRecurringTimestamp);
$timezoneOffset = $initialStart->getTimezone()->getOffset($recurringTimeZone);
$timezoneOffset = ($this->useTimeZoneWithRRules) ? $initialStart->getTimezone()->getOffset($recurringTimeZone) : 0;
$monthRecurringTimestamp += ($timezoneOffset !== $initialStartOffset) ? $initialStartOffset - $timezoneOffset : 0;

// Add event
Expand Down Expand Up @@ -979,7 +986,7 @@ public function processRecurrences()

// Adjust timezone from initial event
$recurringTimeZone = \DateTime::createFromFormat('U', $monthRecurringTimestamp);
$timezoneOffset = $initialStart->getTimezone()->getOffset($recurringTimeZone);
$timezoneOffset = ($this->useTimeZoneWithRRules) ? $initialStart->getTimezone()->getOffset($recurringTimeZone) : 0;
$monthRecurringTimestamp += ($timezoneOffset !== $initialStartOffset) ? $initialStartOffset - $timezoneOffset : 0;

$eventStartDesc = "{$this->dayOrdinals[$dayNumber]} {$this->weekdays[$weekDay]} of " . gmdate('F Y H:i:s', $monthRecurringTimestamp);
Expand Down Expand Up @@ -1051,7 +1058,7 @@ public function processRecurrences()

// Adjust timezone from initial event
$recurringTimeZone = \DateTime::createFromFormat('U', $yearRecurringTimestamp);
$timezoneOffset = $initialStart->getTimezone()->getOffset($recurringTimeZone);
$timezoneOffset = ($this->useTimeZoneWithRRules) ? $initialStart->getTimezone()->getOffset($recurringTimeZone) : 0;
$yearRecurringTimestamp += ($timezoneOffset !== $initialStartOffset) ? $initialStartOffset - $timezoneOffset : 0;

$eventStartDesc = "{$this->dayOrdinals[$dayNumber]} {$this->weekdays[$weekDay]}"
Expand Down Expand Up @@ -1108,7 +1115,7 @@ public function processRecurrences()

// Adjust timezone from initial event
$recurringTimeZone = \DateTime::createFromFormat('U', $yearRecurringTimestamp);
$timezoneOffset = $initialStart->getTimezone()->getOffset($recurringTimeZone);
$timezoneOffset = ($this->useTimeZoneWithRRules) ? $initialStart->getTimezone()->getOffset($recurringTimeZone) : 0;
$yearRecurringTimestamp += ($timezoneOffset !== $initialStartOffset) ? $initialStartOffset - $timezoneOffset : 0;

// Add specific month dates
Expand Down

0 comments on commit 4de3963

Please sign in to comment.