1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 20:58:30 +01:00

Updated Timezone EUF. Use user-defined timezone as default date timezone, if it's possible.

This commit is contained in:
Lóna Lore 2016-12-13 20:48:45 +01:00
parent 794a9f3ea6
commit 04cf3a6dc2
3 changed files with 180 additions and 128 deletions

View File

@ -1127,24 +1127,92 @@ if (($_SERVER['QUERY_STRING'] == 'logout')/* || (($pref['user_tracking'] == 'ses
exit();
}
/*
* Calculate time zone offset, based on session cookie set in e107.js.
* (Buyer beware: this may be wrong for the first pageview in a session,
* which is while the user is logged out, so not a problem...)
*
* Time offset is SECONDS. Seconds is much better than hours as a base,
* as some places have 30 and 45 minute time zones.
* It matches user clock time, instead of only time zones.
* Add the offset to MySQL/server time to get user time.
* Subtract the offset from user time to get server time.
*
/**
* @addtogroup timezone
* @{
*/
$tz = vartrue($pref['timezone'], 'UTC'); //TODO Adjust on the front-end based on user timezone value.
date_default_timezone_set($tz); // Must be set or PHP Warning thrown.
$tzUser = e107::user(USERID);
if (varset($tzUser['user_timezone'], false) && systemTimeZoneIsValid($tzUser['user_timezone']))
{
date_default_timezone_set($tzUser['user_timezone']);
unset($tzUser);
}
else
{
$tz = vartrue($pref['timezone'], 'UTC');
date_default_timezone_set($tz);
unset($tz);
}
/**
* Generate an array of time zones.
*
* @return array
* Array of time zones.
*/
function systemTimeZones()
{
// Never do something time consuming twice if you can hold onto the results
// and re-use them. So we re-use the statically cached value to save time
// and memory.
static $zones = array();
// If Timezone list is not populated yet.
if(empty($zones))
{
$zonelist = timezone_identifiers_list();
$timeNow = date('m/d/Y H:i', $_SERVER['REQUEST_TIME']);
foreach($zonelist as $zone)
{
// Because many time zones exist in PHP only for backward compatibility
// reasons and should not be used, the list is filtered by a regular
// expression.
if(preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone))
{
$dateTimeZone = new DateTimeZone($zone);
$dateTime = new DateTime($timeNow, $dateTimeZone);
$offset = $dateTime->format('O');
$offset = chunk_split($offset, 3, ':');
$zones[$zone] = str_replace('_', ' ', $zone) . ' (' . rtrim($offset, ':') . ')';
}
}
// Sort time zones alphabetically.
asort($zones);
}
return $zones;
}
/**
* Validate a timezone.
*
* @param string $zone
* Timezone.
*
* @return bool
*/
function systemTimeZoneIsValid($zone = '')
{
$zones = systemTimeZones();
$zoneKeys = array_keys($zones);
if(in_array($zone, $zoneKeys))
{
return true;
}
return false;
}
/**
* @} End of "addtogroup timezone".
*/
$e_deltaTime=0;

View File

@ -773,37 +773,6 @@ $text .= "
";
/**
* Generate an array of time zones.
*/
function systemTimeZones()
{
$zonelist = timezone_identifiers_list();
$timeNow = date('m/d/Y H:i', $_SERVER['REQUEST_TIME']);
$zones = array();
foreach($zonelist as $zone)
{
// Because many time zones exist in PHP only for backward compatibility
// reasons and should not be used, the list is filtered by a regular
// expression.
if(preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone))
{
$dateTimeZone = new DateTimeZone($zone);
$dateTime = new DateTime($timeNow, $dateTimeZone);
$offset = $dateTime->format('O');
$offset = chunk_split($offset, 3, ':');
$zones[$zone] = str_replace('_', ' ', $zone) . ' (' . rtrim($offset, ':') . ')';
}
}
// Sort time zones alphabetically.
asort($zones);
return $zones;
}
// =========== Registration Preferences. ==================

View File

@ -14,121 +14,136 @@
* $Author$
*/
if (!defined('e107_INIT')) { exit; }
/*
This file is used with the extended user field 'predefined list' type. It is invoked when the value field is 'timezones'.
It is an example of an extended user field which access a predetermined list of key-pair values. In this example all the data is loaded
into memory; for other applications the data may be read from a database, possibly with caching.
The objective is to provide a uniform interface to such data.
The class name must be the same as the file name - i.e. the list name prefixed with 'extended_'.
The variable name must be 'timezones_list', and is an array of possible values, each of which is a value => text pair
The text is displayed in a drop-down; the value is returned.
If function timezones_value() exists, it is called to create the displayed text
*/
class extended_timezones
if(!defined('e107_INIT'))
{
exit;
}
private $timezonesList = array(
'-12' => "International DateLine West",
'-11' => "Samoa",
'-10' => "Hawaii",
'-9' => "Alaska",
'-8' => "Pacific Time (US and Canada)",
'-7' => "Mountain Time (US and Canada)",
'-6' => "Central Time (US and Canada), Central America",
'-5' => "Eastern Time (US and Canada)",
'-4' => "Atlantic Time (Canada)",
'-3.30' => 'Newfoundland',
'-3' => "Greenland, Brasilia, Buenos Aires, Georgetown",
'-2' => "Mid-Atlantic",
'-1' => "Azores, Cape Verde Islands",
'+0' => "UK, Ireland, Lisbon",
'+1' => "West Central Africa, Western Europe",
'+2' => "Greece, Egypt, parts of Africa",
'+3' => "Russia, Baghdad, Kuwait, Nairobi",
'+3.30' => 'Tehran, Iran',
'+4' => "Abu Dhabi, Kabul",
'+4.30' => 'Afghanistan',
'+5' => "Islamabad, Karachi",
'+5.30' => "Mumbai, Delhi, Calcutta",
'+5.45' => 'Kathmandu',
'+6' => "Astana, Dhaka",
'+7' => "Bangkok, Rangoon",
'+8' => "Hong Kong, Singapore, Perth, Beijing",
'+9' => "Tokyo, Seoul",
'+9.30' => 'Darwin, Adelaide',
'+10' => "Brisbane, Canberra, Sydney, Melbourne",
'+10.30' => 'Lord Howe Island',
'+11' => "Soloman Islands",
'+11.30' => 'Norfolk Island',
'+12' => "New Zealand, Fiji, Marshall Islands",
'+13' => "Tonga, Nuku'alofa, Rawaki Islands",
'+13.45' => 'Chatham Island',
'+14' => 'Kiribati: Line Islands'
);
private $isEOF = FALSE; // True if at last element of list
private $bufferValid = FALSE;
/**
* @file
* This file is used with the extended user field 'predefined list' type. It is
* invoked when the value field is 'timezones'.
*
* It is an example of an extended user field which access a predetermined list
* of key-pair values. In this example all the data is loaded into memory; for
* other applications the data may be read from a database, possibly with
* caching.
*
* The objective is to provide a uniform interface to such data.
*
* The class name must be the same as the file name - i.e. the list name
* prefixed with 'extended_'.
*
* The variable name must be 'timezones_list', and is an array of possible
* values, each of which is a value => text pair.
*
* The text is displayed in a drop-down; the value is returned.
*
* If function timezones_value() exists, it is called to create the displayed
* text.
*/
/**
* Call before using the 'next' format option, to ensure the array is indexed from the beginning
* Class extended_timezones.
*/
class extended_timezones
{
/**
* @var array
*/
private $timezonesList = array();
/**
* @var bool
*/
private $isEOF = false; // True if at last element of list.
/**
* @var bool
*/
private $bufferValid = false;
/**
* Constructor.
*/
public function __construct()
{
$this->timezonesList = systemTimeZones();
}
/**
* Call before using the 'next' format option, to ensure the array is
* indexed from the beginning.
*/
public function pointerReset()
{
$this->isEOF = (FALSE === reset($this->timezonesList));
$this->bufferValid = TRUE;
$this->isEOF = (false === reset($this->timezonesList));
$this->bufferValid = true;
}
/**
* Return a formatted timezone value
*
* @param mixed $key - the key value to select
* @param string $formatSpec - defines format of return value
* @param mixed $key
* The key value to select.
* @param string $formatSpec
* Defines format of return value.
*
* @return mixed (according to $formatSpec). FALSE if no value available
* @return mixed
* (according to $formatSpec).
* false - if no value available.
* 'array' - a single-element array; key as passed, and value to match key
* 'next' - as 'array', but ignores the passed $key and moves to next value.
* default - a string usable for display
* 'default' - a string usable for display.
*/
public function getValue($key, $formatSpec = '')
{
if($formatSpec == 'next')
{
if (!$this->bufferValid) $this->pointerReset; // Make sure buffer is defined
if ($this->isEOF) return FALSE;
// Make sure buffer is defined.
if(!$this->bufferValid)
{
$this->pointerReset();
}
if($this->isEOF)
{
return false;
}
$key = key($this->timezonesList);
$val = current($this->timezonesList);
if (FALSE === $val)
if(false === $val)
{
$this->isEOF = TRUE;
return FALSE;
$this->isEOF = true;
return false;
}
$this->isEOF = (FALSE === next($this->timezonesList));
$this->isEOF = (false === next($this->timezonesList));
return array($key => $val);
}
$exists = isset($this->timezonesList[$key]);
if (!$exists) return FALSE;
if(!$exists)
{
return false;
}
$val = $this->timezonesList[$key];
if($formatSpec == 'array')
{
return array($key => $val);
}
// Default (as per earlier implementations) - can be specified with 'display' format
return 'GMT'.$key.' - '.$val;
// Default (as per earlier implementations) - can be specified with
// 'display' format.
return $val;
}
}
?>