First change to prepare usage of native PHP functions for date instead

This commit is contained in:
Laurent Destailleur 2012-02-06 05:31:19 +01:00
parent 7b527e085d
commit 66ccb8c679
10 changed files with 107 additions and 90 deletions

View File

@ -157,12 +157,13 @@ $txt =$langs->trans("OSTZ").' (variable system TZ): '.($_ENV["TZ"]?$_ENV["TZ"]:$
$txt.=$langs->trans("PHPTZ").' (php.ini date.timezone): '.(ini_get("date.timezone")?ini_get("date.timezone"):$langs->trans("NotDefined")).''."\n"; // date.timezone must be in valued defined in http://fr3.php.net/manual/en/timezones.europe.php
$var=!$var;
print '<tr '.$bc[$var].'><td width="300">'.$langs->trans("CurrentTimeZone").'</td><td>'; // Timezone server PHP
$a=getCurrentTimeZoneString();
$a.=' '.getCurrentTimeZoneInt();
$a.=' ('.(getCurrentTimeZoneInt()>0?'+':'').(getCurrentTimeZoneInt()*3600).')';
$a=getServerTimeZoneString();
$a.=' '.getServerTimeZoneInt();
$a.=' ('.(getServerTimeZoneInt()>0?'+':'').(getServerTimeZoneInt()*3600).')';
print $form->textwithtooltip($a,$txt,2,1,img_info(''));
print '</td></tr>'."\n"; // value defined in http://fr3.php.net/manual/en/timezones.europe.php
$var=!$var;
print dol_now();
print '<tr '.$bc[$var].'><td width="300">&nbsp; => '.$langs->trans("CurrentHour").'</td><td>'.dol_print_date(dol_now(),'dayhour','tzserver').'</td></tr>'."\n";
$var=!$var;
print '<tr '.$bc[$var].'><td width="300">&nbsp; => dol_print_date(0,"dayhourtext")</td><td>'.dol_print_date(0,"dayhourtext").'</td>';

View File

@ -128,7 +128,7 @@ $form=new Form($db);
$companystatic=new Societe($db);
$contactstatic=new Contact($db);
$now=dol_now('tzref');
$now=dol_now();
// Define list of all external calendars
$listofextcals=array();

View File

@ -1587,7 +1587,7 @@ class ContratLigne
*/
function getLibStatut($mode)
{
return $this->LibStatut($this->statut,$mode,(isset($this->date_fin_validite)?($this->date_fin_validite < dol_now('tzref')?1:0):-1));
return $this->LibStatut($this->statut,$mode,(isset($this->date_fin_validite)?($this->date_fin_validite < dol_now()?1:0):-1));
}
/**

View File

@ -102,7 +102,7 @@ $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE cd.fk_contrat = c.rowid AND c.fk_soc = s.rowid";
$sql.= " AND (cd.statut != 4 OR (cd.statut = 4 AND (cd.date_fin_validite is null or cd.date_fin_validite >= '".$db->idate(dol_now('tzref'))."')))";
$sql.= " AND (cd.statut != 4 OR (cd.statut = 4 AND (cd.date_fin_validite is null or cd.date_fin_validite >= '".$db->idate($now)."')))";
$sql.= " AND c.entity = ".$conf->entity;
if ($user->societe_id) $sql.=' AND c.fk_soc = '.$user->societe_id;
if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
@ -139,7 +139,7 @@ $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql.= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c";
if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE cd.fk_contrat = c.rowid AND c.fk_soc = s.rowid";
$sql.= " AND (cd.statut = 4 AND cd.date_fin_validite < '".$db->idate(dol_now('tzref'))."')";
$sql.= " AND (cd.statut = 4 AND cd.date_fin_validite < '".$db->idate($now)."')";
$sql.= " AND c.entity = ".$conf->entity;
if ($user->societe_id) $sql.=' AND c.fk_soc = '.$user->societe_id;
if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;

View File

@ -64,7 +64,7 @@ $companystatic=new Societe($db);
* View
*/
$now=dol_now('tzref');
$now=dol_now();
$form=new Form($db);

View File

@ -66,7 +66,7 @@ include_once(DOL_DOCUMENT_ROOT."/core/boxes/modules_boxes.php");
$this->max=$max;
$now=dol_now('tzref');
$now=dol_now();
$this->info_box_head = array('text' => $langs->trans("BoxLastExpiredServices",$max));

View File

@ -67,27 +67,27 @@ function get_tz_array()
*
* @return string PHP server timezone string ('Europe/Paris')
*/
function getCurrentTimeZoneString()
function getServerTimeZoneString()
{
if (function_exists('date_default_timezone_get')) return date_default_timezone_get();
else return '';
}
/**
* Return server timezone int.
* If $conf->global->MAIN_NEW_DATE is set, we use new behaviour: All convertions take care of dayling saving time.
*
* @return string An offset in seconds (3600 for Europe/Paris on winter and 7200 for Europe/Paris on summer)
* @param string $refdate 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 getCurrentTimeZoneInt()
function getServerTimeZoneInt($refgmtdate='now')
{
global $conf;
if (class_exists('DateTime') && ! empty($conf->global->MAIN_NEW_DATE))
{
// Method 1 (include daylight)
$localtz = new DateTimeZone(date_default_timezone_get());
$localdt = new DateTime("now", $localtz);
$localtz = new DateTimeZone(getServerTimeZoneString());
$localdt = new DateTime($refgmtdate, $localtz);
$tmp=-1*$localtz->getOffset($localdt);
}
else
@ -95,10 +95,48 @@ function getCurrentTimeZoneInt()
// Method 2 (does not include daylight)
$tmp=dol_mktime(0,0,0,1,1,1970);
}
$tz=($tmp<0?'+':'-').abs($tmp/3600);
$tz=($tmp<0?1:-1)*abs($tmp/3600);
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 $refdate 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') && ! empty($conf->global->MAIN_NEW_DATE))
{
// Method 1 (include daylight)
$localtz = new DateTimeZone(getParentCompanyTimeZoneString());
$localdt = new DateTime($refgmtdate, $localtz);
$tmp=-1*$localtz->getOffset($localdt);
}
else
{
// 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 of a timezone to a date

View File

@ -383,39 +383,6 @@ function dol_size($size,$type='')
}
/**
* Return date for now. We should always use this function without parameters (that means GMT time)
*
* @param string $mode 'gmt' => we return GMT timestamp,
* 'tzserver' => we add the PHP server timezone
* 'tzref' => we add the company timezone
* 'tzuser' => we add the user timezone
* @return timestamp $date Timestamp
*/
function dol_now($mode='gmt')
{
// Note that gmmktime and mktime return same value (GMT) whithout parameters
if ($mode == 'gmt') $ret=gmmktime(); // Time for now at greenwich.
else if ($mode == 'tzserver') // Time for now with PHP server timezone added
{
$tzsecond=-dol_mktime(0,0,0,1,1,1970);
$ret=gmmktime()+$tzsecond;
}
else if ($mode == 'tzref') // Time for now where parent company timezone is added
{
// TODO Should add the company timezone
$ret=gmmktime();
}
else if ($mode == 'tzuser') // Time for now where user timezone is added
{
//print 'eeee'.time().'-'.mktime().'-'.gmmktime();
$tzhour=isset($_SESSION['dol_tz'])?$_SESSION['dol_tz']:0;
$ret=gmmktime()+($tzhour*60*60);
}
return $ret;
}
/**
* Clean a string to use it as a file name
*
@ -1094,6 +1061,42 @@ function dol_mktime($hour,$minute,$second,$month,$day,$year,$gm=false,$check=1)
}
/**
* Return date for now. We should always use this function without parameters (that means GMT time)
*
* @param string $mode 'gmt' => we return GMT timestamp,
* 'tzserver' => we add the PHP server timezone
* 'tzref' => we add the company timezone
* 'tzuser' => we add the user timezone
* @return timestamp $date Timestamp
*/
function dol_now($mode='gmt')
{
// Note that gmmktime and mktime return same value (GMT) whithout parameters
if ($mode == 'gmt') $ret=gmmktime(); // Time for now at greenwich.
else if ($mode == 'tzserver') // Time for now with PHP server timezone added
{
require_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php');
$tzsecond=getServerTimeZoneInt(); // Contains tz+dayling saving time
$ret=dol_now('gmt')+($tzsecond*3600);
}
/*else if ($mode == 'tzref') // Time for now with parent company timezone is added
{
require_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php');
$tzsecond=getParentCompanyTimeZoneInt(); // Contains tz+dayling saving time
$ret=dol_now('gmt')+($tzsecond*3600);
}*/
else if ($mode == 'tzuser') // Time for now with user timezone is added
{
//print 'eeee'.time().'-'.mktime().'-'.gmmktime();
$offsettz=(empty($_SESSION['dol_tz'])?0:$_SESSION['dol_tz'])*60*60;
$offsetdst=(empty($_SESSION['dol_dst'])?0:$_SESSION['dol_dst'])*60*60;
$ret=dol_now('gmt')+($offsettz+$offsetdst);
}
return $ret;
}
/**
* Return string with formated size
*

View File

@ -682,30 +682,6 @@ function _adodb_getdate($origd=false,$fast=false,$is_gmt=false)
);
}
function adodb_gmdate($fmt,$d=false)
{
return adodb_date($fmt,$d,true);
}
// accepts unix timestamp and iso date format in $d
function adodb_date2($fmt, $d=false, $is_gmt=false)
{
if ($d !== false) {
if (!preg_match(
"|^([0-9]{4})[-/\.]?([0-9]{1,2})[-/\.]?([0-9]{1,2})[ -]?(([0-9]{1,2}):?([0-9]{1,2}):?([0-9\.]{1,4}))?|",
($d), $rr)) return adodb_date($fmt,false,$is_gmt);
if ($rr[1] <= 100 && $rr[2]<= 1) return adodb_date($fmt,false,$is_gmt);
// h-m-s-MM-DD-YY
if (!isset($rr[5])) $d = adodb_mktime(0,0,0,$rr[2],$rr[3],$rr[1]);
else $d = @adodb_mktime($rr[5],$rr[6],$rr[7],$rr[2],$rr[3],$rr[1]);
}
return adodb_date($fmt,$d,$is_gmt);
}
/**
Return formatted date based on timestamp $d
*/
@ -848,15 +824,6 @@ static $daylight;
return $dates;
}
/**
Returns a timestamp given a GMT/UTC time.
Note that $is_dst is not implemented and is ignored.
*/
function adodb_gmmktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=false)
{
return adodb_mktime($hr,$min,$sec,$mon,$day,$year,$is_dst,true);
}
/**
Return a timestamp given a local time. Originally by jackbbs.
Note that $is_dst is not implemented and is ignored.
@ -966,11 +933,6 @@ function adodb_mktime($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_dst=fa
return $ret;
}
function adodb_gmstrftime($fmt, $ts=false)
{
return adodb_strftime($fmt,$ts,true);
}
// hack - convert to adodb_date
function adodb_strftime($fmt, $ts=false,$is_gmt=false)
{

View File

@ -254,10 +254,23 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
print __METHOD__." result=".$result."\n";
$this->assertEquals(7261,$result);
$tz=getCurrentTimeZoneInt();
$result=dol_mktime(2,0,0,1,1,1970,0); // 1970-01-01 02:00:00 in local area
$result=dol_mktime(2,0,0,1,1,1970,0); // 1970-01-01 02:00:00 in local area Europe/Paris -> 3600 GMT
print __METHOD__." result=".$result."\n";
$this->assertEquals(7200-($tz*3600),$result);
$tz=getServerTimeZoneInt('1970-01-01 02:00:00'); // +1 in Europe/Paris at this time (we are winter)
$this->assertEquals(7200-($tz*3600),$result); // Should be 7200 if we are at greenwich
}
/**
* testDolNow
*
* @return void
*/
public function testDolNow()
{
$now=dol_now('gmt');
$nowtzserver=dol_now('tzserver');
print __METHOD__."getServerTimeZoneInt=".(getServerTimeZoneInt()*3600)."\n";
$this->assertEquals(getServerTimeZoneInt()*3600,($nowtzserver-$now));
}
}
?>