mirror of
https://github.com/rlanvin/php-rrule.git
synced 2025-04-08 23:53:48 +02:00
Forcing RRule create with a timezone to be in UTC #15
PHP's default is to put them in "+00:00" timezone, which generates invalid RFC string. The method rfcString() will convert all invalid timezones to UTC to generate a valid RFC string.
This commit is contained in:
parent
7c041bfb42
commit
0be4b1fb50
@ -551,18 +551,29 @@ class RRule implements RRuleInterface
|
|||||||
$this->dtstart->format('Ymd\THis')
|
$this->dtstart->format('Ymd\THis')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
elseif ( $this->dtstart->getTimeZone()->getName() == 'Z' ) {
|
|
||||||
$str = sprintf(
|
|
||||||
"DTSTART:%s\nRRULE:",
|
|
||||||
$this->dtstart->format('Ymd\THis\Z')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
$str = sprintf(
|
$dtstart = clone $this->dtstart;
|
||||||
"DTSTART;TZID=%s:%s\nRRULE:",
|
$timezone_name = $dtstart->getTimeZone()->getName();
|
||||||
$this->dtstart->getTimezone()->getName(),
|
if ( strpos($timezone_name,':') !== false ) {
|
||||||
$this->dtstart->format('Ymd\THis')
|
// handle unsupported timezones like "+02:00"
|
||||||
);
|
// we convert them to UTC to generate a valid string
|
||||||
|
// note: there is possibly other weird timezones out there that we should catch
|
||||||
|
$dtstart->setTimezone(new \DateTimeZone('Z'));
|
||||||
|
$timezone_name = 'Z';
|
||||||
|
}
|
||||||
|
if ( $timezone_name == 'Z' ) {
|
||||||
|
$str = sprintf(
|
||||||
|
"DTSTART:%s\nRRULE:",
|
||||||
|
$dtstart->format('Ymd\THis\Z')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$str = sprintf(
|
||||||
|
"DTSTART;TZID=%s:%s\nRRULE:",
|
||||||
|
$timezone_name,
|
||||||
|
$dtstart->format('Ymd\THis')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1159,6 +1170,7 @@ class RRule implements RRuleInterface
|
|||||||
try {
|
try {
|
||||||
if ( is_integer($date) ) {
|
if ( is_integer($date) ) {
|
||||||
$date = \DateTime::createFromFormat('U',$date);
|
$date = \DateTime::createFromFormat('U',$date);
|
||||||
|
$date->setTimezone(new \DateTimeZone('Z')); // default is +00:00 (see issue #15)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$date = new \DateTime($date);
|
$date = new \DateTime($date);
|
||||||
|
@ -1793,6 +1793,36 @@ class RRuleTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals("DTSTART:19970512T090000Z\nRRULE:FREQ=YEARLY", $rule->rfcString());
|
$this->assertEquals("DTSTART:19970512T090000Z\nRRULE:FREQ=YEARLY", $rule->rfcString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://github.com/rlanvin/php-rrule/issues/15
|
||||||
|
*/
|
||||||
|
public function testRfcStsringsWithTimestamp()
|
||||||
|
{
|
||||||
|
$rrule = new RRule(array(
|
||||||
|
"freq" => "WEEKLY",
|
||||||
|
"dtstart" => 1470323171,
|
||||||
|
"interval" => 1
|
||||||
|
));
|
||||||
|
|
||||||
|
$str = $rrule->rfcString();
|
||||||
|
$new_rrule = new RRule($str);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testUnsupportedTimezoneConvertedToUtc()
|
||||||
|
{
|
||||||
|
$date = new DateTime('2016-07-08 12:00:00', new DateTimeZone('+06:00'));
|
||||||
|
$rrule = new RRule(array(
|
||||||
|
"freq" => "WEEKLY",
|
||||||
|
"dtstart" => $date,
|
||||||
|
"interval" => 1
|
||||||
|
));
|
||||||
|
|
||||||
|
$str = $rrule->rfcString();
|
||||||
|
$this->assertTrue(strpos($str, '20160708T060000Z')!== false);
|
||||||
|
$new_rrule = new RRule($str);
|
||||||
|
}
|
||||||
|
|
||||||
public function rfcStringsWithoutTimezone()
|
public function rfcStringsWithoutTimezone()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user