diff --git a/src/RRule.php b/src/RRule.php index ad57b06..c9eb5d3 100755 --- a/src/RRule.php +++ b/src/RRule.php @@ -519,11 +519,17 @@ class RRule implements RRuleInterface * Format a rule according to RFC 5545 * @return string */ - public function rfcString() + public function rfcString($include_timezone = true) { $str = ''; if ( $this->rule['DTSTART'] ) { - if ( $this->dtstart->getTimeZone()->getName() == 'Z' ) { + if ( ! $include_timezone ) { + $str = sprintf( + "DTSTART:%s\nRRULE:", + $this->dtstart->format('Ymd\THis') + ); + } + elseif ( $this->dtstart->getTimeZone()->getName() == 'Z' ) { $str = sprintf( "DTSTART:%s\nRRULE:", $this->dtstart->format('Ymd\THis\Z') @@ -550,10 +556,18 @@ class RRule implements RRuleInterface continue; } if ( $key === 'UNTIL' && $value ) { - // according to the RFC, UNTIL must be in UTC - $tmp = clone $this->until; - $tmp->setTimezone(new \DateTimeZone('UTC')); - $parts[] = 'UNTIL='.$tmp->format('Ymd\THis\Z'); + if ( ! $include_timezone ) { + $tmp = clone $this->until; + // put until on the same timezone as DTSTART + $tmp->setTimeZone($this->dtstart->getTimezone()); + $parts[] = 'UNTIL='.$tmp->format('Ymd\THis'); + } + else { + // according to the RFC, UNTIL must be in UTC + $tmp = clone $this->until; + $tmp->setTimezone(new \DateTimeZone('UTC')); + $parts[] = 'UNTIL='.$tmp->format('Ymd\THis\Z'); + } continue; } if ( $value ) { diff --git a/tests/RRuleTest.php b/tests/RRuleTest.php index 1f571ed..ab0e8b2 100755 --- a/tests/RRuleTest.php +++ b/tests/RRuleTest.php @@ -1786,6 +1786,29 @@ class RRuleTest extends PHPUnit_Framework_TestCase $this->assertEquals("DTSTART:19970512T090000Z\nRRULE:FREQ=YEARLY", $rule->rfcString()); } + public function rfcStringsWithoutTimezone() + { + return array( + array( + "DTSTART;TZID=America/New_York:19970901T090000\nRRULE:FREQ=DAILY", + "DTSTART:19970901T090000\nRRULE:FREQ=DAILY", + ), + array( + "DTSTART;TZID=Europe/Paris:19970901T090000\nRRULE:FREQ=DAILY;UNTIL=19970902T070000Z", + "DTSTART:19970901T090000\nRRULE:FREQ=DAILY;UNTIL=19970902T090000", + ), + ); + } + + /** + * @dataProvider rfcStringsWithoutTimezone + */ + public function testRfcStringWithoutTimezone($str, $expected_str) + { + $rule = new RRule($str); + $this->assertEquals($expected_str, $rule->rfcString(false)); + } + /////////////////////////////////////////////////////////////////////////////// // Timezone