From 24dbfbba60d98ebf10140fef397d7a9996088b42 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Fri, 21 Mar 2014 18:05:08 +0100 Subject: [PATCH] Qual: Still working on cleaning code for timezone management. --- htdocs/admin/system/dolibarr.php | 3 ++- htdocs/core/class/conf.class.php | 7 ++++- htdocs/core/lib/date.lib.php | 44 ++----------------------------- htdocs/core/lib/functions.lib.php | 9 +------ test/phpunit/FunctionsLibTest.php | 15 ++++++++--- 5 files changed, 23 insertions(+), 55 deletions(-) diff --git a/htdocs/admin/system/dolibarr.php b/htdocs/admin/system/dolibarr.php index 59f3b3f3856..9b5d411b136 100644 --- a/htdocs/admin/system/dolibarr.php +++ b/htdocs/admin/system/dolibarr.php @@ -155,7 +155,8 @@ print '<tr '.$bc[$var].'><td width="300"> => price(1234.56)</td><td>'.pric // Timezone $txt =$langs->trans("OSTZ").' (variable system TZ): '.(! empty($_ENV["TZ"])?$_ENV["TZ"]:$langs->trans("NotDefined")).'<br>'."\n"; $txt.=$langs->trans("PHPTZ").' (php.ini date.timezone): '.(ini_get("date.timezone")?ini_get("date.timezone"):$langs->trans("NotDefined")).''."<br>\n"; // date.timezone must be in valued defined in http://fr3.php.net/manual/en/timezones.europe.php -$txt.=$langs->trans("YouCanEditPHPTZ"); +$txt.=$langs->trans("Dolibarr constant MAIN_SERVER_TZ").': '.(empty($conf->global->MAIN_SERVER_TZ)?$langs->trans("NotDefined"):$conf->global->MAIN_SERVER_TZ); +//$txt.=$langs->trans("YouCanEditPHPTZ"); // deprecated $var=!$var; print '<tr '.$bc[$var].'><td width="300">'.$langs->trans("CurrentTimeZone").'</td><td>'; // Timezone server PHP $a=getServerTimeZoneInt('now'); diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 8629d7c73e3..1d122a6c6f1 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -200,7 +200,12 @@ class Conf } //var_dump($this->modules); //var_dump($this->modules_parts['theme']); - + + // If you can't set timezone of your PHP, set this constant. Better is to set it to UTC. + // In future, this constant will be forced to 'UTC' so PHP server timezone will not have effect anymore. + //$this->global->MAIN_SERVER_TZ='Europe/Paris'; + if (! empty($this->global->MAIN_SERVER_TZ) && $this->global->MAIN_SERVER_TZ != 'auto') date_default_timezone_set($this->global->MAIN_SERVER_TZ); + // Object $mc if (! defined('NOREQUIREMC') && ! empty($this->multicompany->enabled)) { diff --git a/htdocs/core/lib/date.lib.php b/htdocs/core/lib/date.lib.php index 18c6bc23148..8a1c35ace9c 100644 --- a/htdocs/core/lib/date.lib.php +++ b/htdocs/core/lib/date.lib.php @@ -69,8 +69,7 @@ function get_tz_array() */ function getServerTimeZoneString() { - if (function_exists('date_default_timezone_get')) return @date_default_timezone_get(); - else return ''; + return @date_default_timezone_get(); } /** @@ -96,7 +95,7 @@ function getServerTimeZoneInt($refgmtdate='now') } else { - dol_print_error('','PHP version must be 5.2+'); + dol_print_error('','PHP version must be 5.3+'); /* // Method 2 (does not include daylight, not supported by adodb) if ($refgmtdate == 'now') @@ -126,45 +125,6 @@ function getServerTimeZoneInt($refgmtdate='now') return $tz; } -/** - * Return server timezone string - * - * @return string Parent company timezone string ('Europe/Paris') - * -function getParentCompanyTimeZoneString() -{ - if (function_exists('date_default_timezone_get')) return @date_default_timezone_get(); - else return ''; -} -*/ - -/** - * Return parent company timezone int. - * If $conf->global->MAIN_NEW_DATE is set, we use new behaviour: All convertions take care of dayling saving time. - * - * @param string $refgmtdate Reference date for timezone (timezone differs on winter and summer) - * @return int An offset in hour (+1 for Europe/Paris on winter and +2 for Europe/Paris on summer) - * -function getParentCompanyTimeZoneInt($refgmtdate='now') -{ - global $conf; - if (class_exists('DateTime')) - { - // Method 1 (include daylight) - $localtz = new DateTimeZone(getParentCompanyTimeZoneString()); - $localdt = new DateTime($refgmtdate, $localtz); - $tmp=-1*$localtz->getOffset($localdt); - } - else - { - dol_print_error('','PHP version must be 5.2+'); - // Method 2 (does not include daylight) - $tmp=dol_mktime(0,0,0,1,1,1970); - } - $tz=($tmp<0?1:-1)*abs($tmp/3600); - return $tz; -}*/ - /** * Add a delay to a date diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 79d169513d0..d02633c2d04 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -1027,14 +1027,7 @@ function dol_mktime($hour,$minute,$second,$month,$day,$year,$gm=false,$check=1) { if (empty($gm) || $gm === 'server') { - // If you can't set timezone of your PHP, set this constant. Better is to set it to UTC. - // In future, this constant will be forced to 'UTC' so PHP server timezone will not have effect anymore. - if (! empty($conf->global->MAIN_SERVER_TZ)) - { - if ($conf->global->MAIN_SERVER_TZ != 'auto') $default_timezone=$conf->global->MAIN_SERVER_TZ; - else $default_timezone=@date_default_timezone_get(); - } - else $default_timezone=@date_default_timezone_get(); + $default_timezone=@date_default_timezone_get(); $localtz = new DateTimeZone($default_timezone); } else if ($gm === 'user') diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index cbfab3d69c9..ad46b7e5def 100755 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -368,7 +368,13 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase public function testDolMkTime() { global $conf; - + + $savtz=date_default_timezone_get(); + + // Some test for UTC TZ + date_default_timezone_set('UTC'); + + // Check bad hours $result=dol_mktime(25,0,0,1,1,1970,1,1); // Error (25 hours) print __METHOD__." result=".$result."\n"; $this->assertEquals('',$result); @@ -394,17 +400,20 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase $tz=getServerTimeZoneInt('winter'); // +1 in Europe/Paris at this time (this time is winter) $this->assertEquals(7200-($tz*3600),$result); // 7200 if we are at greenwich winter, 7200-($tz*3600) at local winter + // Some test for local TZ Europe/Paris + date_default_timezone_set('Europe/Paris'); + // Check that tz for paris in winter is used - $conf->global->MAIN_SERVER_TZ='Europe/Paris'; $result=dol_mktime(2,0,0,1,1,1970,'server'); // 1970-01-01 02:00:00 = 7200 in local area Europe/Paris = 3600 GMT print __METHOD__." result=".$result."\n"; $this->assertEquals(3600,$result); // 7200 if we are at greenwich winter, 3600 at Europe/Paris // Check that daylight saving time is used - $conf->global->MAIN_SERVER_TZ='Europe/Paris'; $result=dol_mktime(2,0,0,6,1,2014,0); // 2014-06-01 02:00:00 = 1401588000-3600(location)-3600(daylight) in local area Europe/Paris = 1401588000 GMT print __METHOD__." result=".$result."\n"; $this->assertEquals(1401588000-3600-3600,$result); // 1401588000 are at greenwich summer, 1401588000-3600(location)-3600(daylight) at Europe/Paris summer + + date_default_timezone_set($savtz); } -- GitLab