Qual: Still working on cleaning code for timezone management.

This commit is contained in:
Laurent Destailleur 2014-03-21 18:05:08 +01:00
parent 73682baff0
commit 24dbfbba60
5 changed files with 23 additions and 55 deletions

View File

@ -155,7 +155,8 @@ print '<tr '.$bc[$var].'><td width="300">&nbsp; => price(1234.56)</td><td>'.pric
// Timezone // Timezone
$txt =$langs->trans("OSTZ").' (variable system TZ): '.(! empty($_ENV["TZ"])?$_ENV["TZ"]:$langs->trans("NotDefined")).'<br>'."\n"; $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("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; $var=!$var;
print '<tr '.$bc[$var].'><td width="300">'.$langs->trans("CurrentTimeZone").'</td><td>'; // Timezone server PHP print '<tr '.$bc[$var].'><td width="300">'.$langs->trans("CurrentTimeZone").'</td><td>'; // Timezone server PHP
$a=getServerTimeZoneInt('now'); $a=getServerTimeZoneInt('now');

View File

@ -201,6 +201,11 @@ class Conf
//var_dump($this->modules); //var_dump($this->modules);
//var_dump($this->modules_parts['theme']); //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 // Object $mc
if (! defined('NOREQUIREMC') && ! empty($this->multicompany->enabled)) if (! defined('NOREQUIREMC') && ! empty($this->multicompany->enabled))
{ {

View File

@ -69,8 +69,7 @@ function get_tz_array()
*/ */
function getServerTimeZoneString() function getServerTimeZoneString()
{ {
if (function_exists('date_default_timezone_get')) return @date_default_timezone_get(); return @date_default_timezone_get();
else return '';
} }
/** /**
@ -96,7 +95,7 @@ function getServerTimeZoneInt($refgmtdate='now')
} }
else 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) // Method 2 (does not include daylight, not supported by adodb)
if ($refgmtdate == 'now') if ($refgmtdate == 'now')
@ -126,45 +125,6 @@ function getServerTimeZoneInt($refgmtdate='now')
return $tz; 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 * Add a delay to a date

View File

@ -1027,14 +1027,7 @@ function dol_mktime($hour,$minute,$second,$month,$day,$year,$gm=false,$check=1)
{ {
if (empty($gm) || $gm === 'server') if (empty($gm) || $gm === 'server')
{ {
// If you can't set timezone of your PHP, set this constant. Better is to set it to UTC. $default_timezone=@date_default_timezone_get();
// 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();
$localtz = new DateTimeZone($default_timezone); $localtz = new DateTimeZone($default_timezone);
} }
else if ($gm === 'user') else if ($gm === 'user')

View File

@ -369,6 +369,12 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase
{ {
global $conf; 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) $result=dol_mktime(25,0,0,1,1,1970,1,1); // Error (25 hours)
print __METHOD__." result=".$result."\n"; print __METHOD__." result=".$result."\n";
$this->assertEquals('',$result); $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) $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 $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 // 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 $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"; print __METHOD__." result=".$result."\n";
$this->assertEquals(3600,$result); // 7200 if we are at greenwich winter, 3600 at Europe/Paris $this->assertEquals(3600,$result); // 7200 if we are at greenwich winter, 3600 at Europe/Paris
// Check that daylight saving time is used // 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 $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"; 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 $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);
} }