mirror of
https://github.com/rlanvin/php-rrule.git
synced 2025-02-20 09:54:16 +01:00
Adding $include_timezone parameter to rfcString()
This commit is contained in:
parent
cb5735edd9
commit
d3d9c5090c
@ -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 ) {
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user