mirror of
https://github.com/rlanvin/php-rrule.git
synced 2025-02-20 09:54:16 +01: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')
|
||||
);
|
||||
}
|
||||
elseif ( $this->dtstart->getTimeZone()->getName() == 'Z' ) {
|
||||
$str = sprintf(
|
||||
"DTSTART:%s\nRRULE:",
|
||||
$this->dtstart->format('Ymd\THis\Z')
|
||||
);
|
||||
}
|
||||
else {
|
||||
$str = sprintf(
|
||||
"DTSTART;TZID=%s:%s\nRRULE:",
|
||||
$this->dtstart->getTimezone()->getName(),
|
||||
$this->dtstart->format('Ymd\THis')
|
||||
);
|
||||
$dtstart = clone $this->dtstart;
|
||||
$timezone_name = $dtstart->getTimeZone()->getName();
|
||||
if ( strpos($timezone_name,':') !== false ) {
|
||||
// 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 {
|
||||
if ( is_integer($date) ) {
|
||||
$date = \DateTime::createFromFormat('U',$date);
|
||||
$date->setTimezone(new \DateTimeZone('Z')); // default is +00:00 (see issue #15)
|
||||
}
|
||||
else {
|
||||
$date = new \DateTime($date);
|
||||
|
@ -1793,6 +1793,36 @@ class RRuleTest extends PHPUnit_Framework_TestCase
|
||||
$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()
|
||||
{
|
||||
return array(
|
||||
|
Loading…
x
Reference in New Issue
Block a user