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

From: Date: Fri, 13 Sep 2002 01:16:45 +0000
Subject: #19383 [Opn->Dup]: Date->converttz($tz) fails for timezones in western hemisphere
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-19278@lists.php.net to get a copy of this message
ID: 19383 User updated by: asutton@sbcglobal.net Reported By: asutton@sbcglobal.net -Status: Open +Status: Duplicate Bug Type: Date/time related Operating System: Linux PHP Version: 4.2.3 New Comment: Nevermind. It looks like this has already been fixed in version 1.6 of Date.php, so it'll be ok in the next release. Thanks, Allan Previous Comments: ------------------------------------------------------------------------ [2002-09-12 20:04:19] asutton@sbcglobal.net 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 this bug report at http://bugs.php.net/?id=19383&edit=1

« previous php.bugs (#19278) next »