From 4de39631b7ec2222a71dfc967de5e8b9da6d3f2c Mon Sep 17 00:00:00 2001 From: Jonathan Goode Date: Fri, 16 Dec 2016 15:00:02 +0000 Subject: [PATCH] Add toggle setting for applying time zone definitions when parsing recurrence rule data --- README.md | 25 +++++++++++++------------ src/ICal/ICal.php | 23 +++++++++++++++-------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a73c1b6..cb51e77 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ICal/ICal.php b/src/ICal/ICal.php index b74e35d..ac306fa 100644 --- a/src/ICal/ICal.php +++ b/src/ICal/ICal.php @@ -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 * @@ -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(); @@ -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 { @@ -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 @@ -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) { @@ -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 @@ -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); @@ -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]}" @@ -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