Fixing issue where the time helper sets the wrong timezone part in RSS dates

This commit is contained in:
Jelle Henkens 2011-08-26 22:16:49 +01:00
parent aee650defb
commit da4b75c993
2 changed files with 21 additions and 0 deletions

View file

@ -440,6 +440,17 @@ class TimeHelper extends AppHelper {
*/
function toRSS($dateString, $userOffset = null) {
$date = $this->fromString($dateString, $userOffset);
if(!is_null($userOffset)) {
if($userOffset == 0) {
$timezone = '+0000';
} else {
$hours = (int) floor(abs($userOffset));
$minutes = (int) (fmod(abs($userOffset), $hours) * 60);
$timezone = ($userOffset < 0 ? '-' : '+') . str_pad($hours, 2, '0', STR_PAD_LEFT) . str_pad($minutes, 2, '0', STR_PAD_LEFT);
}
return date('D, d M Y H:i:s', $date) . ' ' . $timezone;
}
return date("r", $date);
}

View file

@ -409,6 +409,16 @@ class TimeHelperTest extends CakeTestCase {
*/
function testToRss() {
$this->assertEqual(date('r'), $this->Time->toRss(time()));
if (!$this->skipIf(!class_exists('DateTimeZone'), '%s DateTimeZone class not available.')) {
$timezones = array('Europe/London', 'Europe/Brussels', 'UTC', 'America/Denver', 'America/Caracas', 'Asia/Kathmandu');
foreach($timezones as $timezone) {
$yourTimezone = new DateTimeZone($timezone);
$yourTime = new DateTime('now', $yourTimezone);
$userOffset = $yourTimezone->getOffset($yourTime) / HOUR;
$this->assertEqual($yourTime->format('r'), $this->Time->toRss(time(), $userOffset));
}
}
}
/**