mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-12-27 09:09:04 +01:00
Refined egw_time class a bit more and and integrated it with preferences
class, to automatically set relevant user prefs (tz, dateformat, timeformat).
This commit is contained in:
parent
e0795f7328
commit
3ef01bc589
@ -38,6 +38,10 @@
|
|||||||
*/
|
*/
|
||||||
class egw_time extends DateTime
|
class egw_time extends DateTime
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Database timestamp format: Y-m-d H:i:s
|
||||||
|
*/
|
||||||
|
const DATABASE = 'Y-m-d H:i:s';
|
||||||
/**
|
/**
|
||||||
* DateTimeZone of server, read via date_default_timezone_get(), set by self::init()
|
* DateTimeZone of server, read via date_default_timezone_get(), set by self::init()
|
||||||
*
|
*
|
||||||
@ -46,25 +50,25 @@ class egw_time extends DateTime
|
|||||||
static public $server_timezone;
|
static public $server_timezone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DateTimeZone of user, read from user prefs, set by self::init()
|
* DateTimeZone of user, read from user prefs, set by self::init() or self::setUserPrefs()
|
||||||
*
|
*
|
||||||
* @var DateTimeZone
|
* @var DateTimeZone
|
||||||
*/
|
*/
|
||||||
static public $user_timezone;
|
static public $user_timezone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Time format from user prefs, set by self::init()
|
* Time format from user prefs, set by self::setUserPrefs()
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
static public $user_time_format = 'H:i';
|
static public $user_timeformat = 'H:i';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Date format from user prefs, set by self::init()
|
* Date format from user prefs, set by self::setUserPrefs()
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
static public $user_date_format = 'Y-m-d';
|
static public $user_dateformat = 'Y-m-d';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
@ -77,11 +81,8 @@ class egw_time extends DateTime
|
|||||||
*/
|
*/
|
||||||
public function __construct($time='now',DateTimeZone $tz=null,&$type=null)
|
public function __construct($time='now',DateTimeZone $tz=null,&$type=null)
|
||||||
{
|
{
|
||||||
if (is_null($tz))
|
if (is_null($tz)) $tz = self::$user_timezone; // default user timezone
|
||||||
{
|
|
||||||
if (is_null(self::$user_timezone)) self::init();
|
|
||||||
$tz = self::$user_timezone;
|
|
||||||
}
|
|
||||||
switch(($type = gettype($time)))
|
switch(($type = gettype($time)))
|
||||||
{
|
{
|
||||||
case 'NULL':
|
case 'NULL':
|
||||||
@ -114,6 +115,17 @@ class egw_time extends DateTime
|
|||||||
|
|
||||||
case 'array':
|
case 'array':
|
||||||
parent::__construct('now',$tz);
|
parent::__construct('now',$tz);
|
||||||
|
if (isset($time['Y'])) // array format used in eTemplate
|
||||||
|
{
|
||||||
|
$time = array(
|
||||||
|
'year' => $time['Y'],
|
||||||
|
'month' => $time['m'],
|
||||||
|
'day' => $time['d'],
|
||||||
|
'hour' => $time['H'],
|
||||||
|
'minute' => $time['i'],
|
||||||
|
'second' => $time['s'],
|
||||||
|
);
|
||||||
|
}
|
||||||
if (!empty($time['full']) && empty($time['year']))
|
if (!empty($time['full']) && empty($time['year']))
|
||||||
{
|
{
|
||||||
$time['year'] = (int)substr($time['full'],0,4);
|
$time['year'] = (int)substr($time['full'],0,4);
|
||||||
@ -144,8 +156,6 @@ class egw_time extends DateTime
|
|||||||
*/
|
*/
|
||||||
public function setUser()
|
public function setUser()
|
||||||
{
|
{
|
||||||
if (is_null(self::$user_timezone)) self::init();
|
|
||||||
|
|
||||||
$this->setTimezone(self::$user_timezone);
|
$this->setTimezone(self::$user_timezone);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,8 +166,6 @@ class egw_time extends DateTime
|
|||||||
*/
|
*/
|
||||||
public function setServer()
|
public function setServer()
|
||||||
{
|
{
|
||||||
if (is_null(self::$server_timezone)) self::init();
|
|
||||||
|
|
||||||
$this->setTimezone(self::$server_timezone);
|
$this->setTimezone(self::$server_timezone);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,15 +186,17 @@ class egw_time extends DateTime
|
|||||||
case '1': // boolean true: date as in user prefs
|
case '1': // boolean true: date as in user prefs
|
||||||
if (is_bool($type))
|
if (is_bool($type))
|
||||||
{
|
{
|
||||||
$type = $type ? self::$user_date_format : self::$user_time_format;
|
$type = $type ? self::$user_dateformat : self::$user_timeformat;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$type = self::$user_date_format.', '.self::$user_time_format;
|
$type = self::$user_dateformat.', '.self::$user_timeformat;
|
||||||
}
|
}
|
||||||
// fall through
|
break;
|
||||||
|
|
||||||
case 'string':
|
case 'string':
|
||||||
return parent::format('Y-m-d H:i:s');
|
$type = self::DATABASE;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'server': // timestamp in servertime
|
case 'server': // timestamp in servertime
|
||||||
$this->setServer();
|
$this->setServer();
|
||||||
@ -199,7 +209,7 @@ class egw_time extends DateTime
|
|||||||
case 'object':
|
case 'object':
|
||||||
case 'datetime':
|
case 'datetime':
|
||||||
case 'egw_time':
|
case 'egw_time':
|
||||||
return $this;
|
return clone($this);
|
||||||
|
|
||||||
case 'array':
|
case 'array':
|
||||||
$arr = array(
|
$arr = array(
|
||||||
@ -213,11 +223,38 @@ class egw_time extends DateTime
|
|||||||
);
|
);
|
||||||
$arr['raw'] = mktime($arr['hour'],$arr['minute'],$arr['second'],$arr['month'],$arr['day'],$arr['year']);
|
$arr['raw'] = mktime($arr['hour'],$arr['minute'],$arr['second'],$arr['month'],$arr['day'],$arr['year']);
|
||||||
return $arr;
|
return $arr;
|
||||||
|
|
||||||
|
case 'date_array': // array with short keys used by date: Y, m, d, H, i, s (used in eTemplate)
|
||||||
|
return array(
|
||||||
|
'Y' => (int)parent::format('Y'),
|
||||||
|
'm' => (int)parent::format('m'),
|
||||||
|
'd' => (int)parent::format('d'),
|
||||||
|
'H' => (int)parent::format('H'),
|
||||||
|
'i' => (int)parent::format('i'),
|
||||||
|
's' => (int)parent::format('s'),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// default $type contains string with format
|
// default $type contains string with format
|
||||||
return parent::format($type);
|
return parent::format($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cast object to a string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
$tz = $this->getTimezone();
|
||||||
|
if (!$tz)
|
||||||
|
{
|
||||||
|
ob_start();
|
||||||
|
debug_print_backtrace();
|
||||||
|
error_log(ob_get_clean());
|
||||||
|
}
|
||||||
|
return $this->format(self::DATABASE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a server time into a user time
|
* Convert a server time into a user time
|
||||||
*
|
*
|
||||||
@ -227,14 +264,12 @@ class egw_time extends DateTime
|
|||||||
*/
|
*/
|
||||||
public static function server2user($time,$type=null)
|
public static function server2user($time,$type=null)
|
||||||
{
|
{
|
||||||
if (is_null(self::$user_timezone)) self::init();
|
|
||||||
|
|
||||||
if (!is_a($time,$typeof='egw_time')) $time = new egw_time($time,self::$server_timezone,$typeof);
|
if (!is_a($time,$typeof='egw_time')) $time = new egw_time($time,self::$server_timezone,$typeof);
|
||||||
$time->setUser();
|
$time->setUser();
|
||||||
|
|
||||||
if (is_null($type)) $type = $typeof;
|
if (is_null($type)) $type = $typeof;
|
||||||
|
|
||||||
//echo "<p>".__METHOD__."($time,$type) = ".print_r($datetime->format($type),true)."</p>\n";
|
//echo "<p>".__METHOD__."($time,$type) = ".print_r($format->format($type),true)."</p>\n";
|
||||||
return $time->format($type);
|
return $time->format($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,13 +282,12 @@ class egw_time extends DateTime
|
|||||||
*/
|
*/
|
||||||
public static function user2server($time,$type=null)
|
public static function user2server($time,$type=null)
|
||||||
{
|
{
|
||||||
if (is_null(self::$user_timezone)) self::init();
|
|
||||||
|
|
||||||
if (!is_a($time,$typeof='egw_time')) $time = new egw_time($time,self::$user_timezone,$typeof);
|
if (!is_a($time,$typeof='egw_time')) $time = new egw_time($time,self::$user_timezone,$typeof);
|
||||||
$time->setServer();
|
$time->setServer();
|
||||||
|
|
||||||
if (is_null($type)) $type = $typeof;
|
if (is_null($type)) $type = $typeof;
|
||||||
|
|
||||||
|
//echo "<p>".__METHOD__."($time,$type) = ".print_r($format->format($type),true)."</p>\n";
|
||||||
return $time->format($type);
|
return $time->format($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,27 +307,79 @@ class egw_time extends DateTime
|
|||||||
return $time->format($type);
|
return $time->format($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setter for user timezone, should be called after reading user preferences
|
||||||
|
*
|
||||||
|
* @param string $tz timezone, eg. 'Europe/Berlin' or 'UTC'
|
||||||
|
* @param string $dateformat eg. 'Y-m-d' or 'd.m.Y'
|
||||||
|
* @param string|int $timeformat integer 12, 24, or format string eg. 'H:i'
|
||||||
|
* @throws egw_exception_wrong_userinput if invalid $tz parameter
|
||||||
|
* @return DateTimeZone
|
||||||
|
*/
|
||||||
|
public static function setUserPrefs($tz,$dateformat,$timeformat)
|
||||||
|
{
|
||||||
|
if (!empty($dateformat)) self::$user_dateformat = $dateformat;
|
||||||
|
|
||||||
|
switch($timeformat)
|
||||||
|
{
|
||||||
|
case '24':
|
||||||
|
case '':
|
||||||
|
self::$user_timeformat = 'H:i';
|
||||||
|
break;
|
||||||
|
case '12':
|
||||||
|
self::$user_timeformat = 'h:i a';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
self::$user_timeformat = $timeformat;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
self::$user_timezone = new DateTimeZone($tz);
|
||||||
|
}
|
||||||
|
catch(Exception $e)
|
||||||
|
{
|
||||||
|
throw new egw_exception_wrong_userinput(lang('You need to %1set your timezone preference%2.','<a href="'.egw::link('/index.php',array(
|
||||||
|
'menuaction' => 'preferences.uisettings.index',
|
||||||
|
'appname' => 'preferences')).'">','</a>'));
|
||||||
|
}
|
||||||
|
return self::$user_timezone;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get offset in seconds between user and server time at given time $time
|
||||||
|
*
|
||||||
|
* Compatibility method for old code. It is only valid for the given time, because of possible daylight saving changes!
|
||||||
|
*
|
||||||
|
* @param int|string|DateTime $time='now'
|
||||||
|
* @return int difference in seconds between user and server time (for the given time!)
|
||||||
|
*/
|
||||||
|
public static function tz_offset_s($time='now')
|
||||||
|
{
|
||||||
|
if (!is_a($time,'DateTime')) $time = new egw_time($time);
|
||||||
|
|
||||||
|
return egw_time::$user_timezone->getOffset($time) - egw_time::$server_timezone->getOffset($time);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init static variables, reading user prefs
|
* Init static variables, reading user prefs
|
||||||
*/
|
*/
|
||||||
private static function init()
|
public static function init()
|
||||||
{
|
{
|
||||||
if (is_null(self::$server_timezone))
|
self::$server_timezone = new DateTimeZone(date_default_timezone_get());
|
||||||
|
if (isset($GLOBALS['egw_info']['user']['preferences']['common']['tz']))
|
||||||
{
|
{
|
||||||
self::$server_timezone = new DateTimeZone(date_default_timezone_get());
|
self::setUserPrefs($GLOBALS['egw_info']['user']['preferences']['common']['tz'],
|
||||||
|
$GLOBALS['egw_info']['user']['preferences']['common']['dateformat'],
|
||||||
|
$GLOBALS['egw_info']['user']['preferences']['common']['timeformat']);
|
||||||
}
|
}
|
||||||
if (is_null(self::$user_timezone) && isset($GLOBALS['egw_info']['user']['preferences']['common']))
|
else
|
||||||
{
|
{
|
||||||
if (empty($GLOBALS['egw_info']['user']['preferences']['common']['tz']))
|
self::$user_timezone = clone(self::$server_timezone);
|
||||||
{
|
|
||||||
throw new egw_exception_wrong_userinput(lang('You need to %1set your timezone preference%2.','<a href="'.egw::link('/index.php',array(
|
|
||||||
'menuaction' => 'preferences.uisettings.index',
|
|
||||||
'appname' => 'preferences')).'">','</a>'));
|
|
||||||
}
|
|
||||||
self::$user_timezone = new DateTimeZone($GLOBALS['egw_info']['user']['preferences']['common']['tz']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
egw_time::init();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) // some tests
|
if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) // some tests
|
||||||
{
|
{
|
||||||
|
@ -328,21 +328,30 @@ class preferences
|
|||||||
{
|
{
|
||||||
$prefs =& $this->data['common'];
|
$prefs =& $this->data['common'];
|
||||||
|
|
||||||
if (isset($prefs['tz']))
|
if (!empty($prefs['tz']))
|
||||||
{
|
{
|
||||||
$GLOBALS['egw']->datetimezone = new DateTimeZone($prefs['tz']);
|
egw_time::setUserPrefs($prefs['tz'],$prefs['date_format'],$prefs['time_format']);
|
||||||
$server_offset = date('Z');
|
// set the old preference for compatibilty with old code
|
||||||
$GLOBALS['egw']->datetime_now = new DateTime('now',$GLOBALS['egw']->datetimezone);
|
$GLOBALS['egw_info']['user']['preferences']['common']['tz_offset'] = egw_time::tz_offset_s()/3600;
|
||||||
$utc_offset = $GLOBALS['egw']->datetime_now->getOffset();
|
//echo "<p>".__METHOD__."() tz=$prefs[tz] --> tz_offset={$GLOBALS['egw_info']['user']['preferences']['common']['tz_offset']}</p>\n";
|
||||||
$user_now = $GLOBALS['egw']->datetime_now->format('Y-m-d H:i:s e (T)');
|
|
||||||
$GLOBALS['egw_info']['user']['preferences']['common']['tz_offset'] = $prefs['tz_offset'] = ($utc_offset - $server_offset)/3600;
|
|
||||||
|
|
||||||
//echo "<p>".__METHOD__."() tz='{$prefs['tz']}'=$utc_offset=$user_now, server=date('Z')=$server_offset --> $prefs[tz_offset]</p>\n";
|
// ToDo: get rid of that
|
||||||
|
if (isset($GLOBALS['egw']) && is_object($GLOBALS['egw']))
|
||||||
$GLOBALS['egw']->unset_datetime(); // to force an update
|
{
|
||||||
|
$GLOBALS['egw']->unset_datetime(); // to force an update
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set user timezone, if we get restored from session
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function __wakeup()
|
||||||
|
{
|
||||||
|
$this->check_set_tz_offset();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* read preferences from repository and stores in an array
|
* read preferences from repository and stores in an array
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user