#19383 [NEW]: Date->converttz($tz) fails for timezones in western hemisphere

From: Date: Fri, 13 Sep 2002 01:04:19 +0000
Subject: #19383 [NEW]: Date->converttz($tz) fails for timezones in western hemisphere
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-19277@lists.php.net to get a copy of this message
From: asutton@sbcglobal.net Operating system: Linux PHP version: 4.2.3 PHP Bug Type: Date/time related Bug description: Date->converttz($tz) fails for timezones in western hemisphere When using Date.php, the converttz($tz) function does not properly convert the date if the timezone offset from gmt is negative. The problem appears to be caused by a typo in the converttz function which causes subtractSeconds() to be called with a negative value. In a nutshell, the abs() should be applied to the subtractSeconds() argument, not the addSeconds(). I have included a patch which has completely eliminated the problem, as far as I can test. One could also make a case for ammending subtractSeconds() and addSeconds() to catch negative values. Here's a script which demonstrates the problem: ---Cut Here--- <html> <body> <?php require_once("Date.php"); require_once("Date/TimeZone.php"); $mydate = new Date("2002-05-06 20:21:22"); $mylocaltz = new Date_Timezone("US/Central"); print "UTC: " . $mydate->format("%Y-%m-%d %H:%M:%S") . "<br>\n"; $mydate->converttz($mylocaltz); print "CDT: " . $mydate->format("%Y-%m-%d %H:%M:%S") . "<br>\n"; ?> </body> </html> ---Cut Here--- The output on my system is: UTC: 2002-05-06 20:21:22 CDT: 2002-05-06 25:21:22 Note that it is going the wrong way, and that the hours is greater than 23! This isn't even a valid date. Here's the patch: ---Cut Here--- *** ../Date.php Fri Jun 21 20:58:20 2002 --- Date.php Thu Sep 12 17:16:42 2002 *************** *** 440,452 **** } else { $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000)) ; } // convert UTC to new timezone if($tz->getOffset($this) > 0) { ! $this->addSeconds(intval(abs($tz->getOffset($this)) / 1000)); } else { ! $this->subtractSeconds(intval($tz->getOffset($this) / 1000)); } $this->tz = $tz; } /** --- 440,452 ---- } else { $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000)) ; } // convert UTC to new timezone if($tz->getOffset($this) > 0) { ! $this->addSeconds(intval($tz->getOffset($this) / 1000)); } else { ! $this->subtractSeconds(intval(abs($tz->getOffset($this)) / 1000)); } $this->tz = $tz; } /** ---Cut Here--- Thanks, Allan -- Allan Sutton asutton@sbcglobal.net -- Edit bug report at http://bugs.php.net/?id=19383&edit=1 -- Try a CVS snapshot: http://bugs.php.net/fix.php?id=19383&r=trysnapshot Fixed in CVS: http://bugs.php.net/fix.php?id=19383&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=19383&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=19383&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=19383&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=19383&r=support Expected behavior: http://bugs.php.net/fix.php?id=19383&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=19383&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=19383&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=19383&r=globals

« previous php.bugs (#19277) next »