From 6f7d095e3f4b09847c5963646b2f4a817d68ba39 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 18 Apr 2010 22:11:04 +0100 Subject: [feature/new-tz-handling] Wrapper around DateTime for new date time handling. Wrapped PHP's DateTime with some extensions for supporting phpBB's relative date formats and provided the DateTime::getTimestamp() method to PHP < 5.3. PHPBB3-9558 --- phpBB/includes/datetime.php | 160 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 phpBB/includes/datetime.php (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php new file mode 100644 index 0000000000..b1e95e58a3 --- /dev/null +++ b/phpBB/includes/datetime.php @@ -0,0 +1,160 @@ +_user = $user ? $user : $GLOBALS['user']; + + $timezone = (!$timezone && $this->_user->tz instanceof DateTimeZone) ? $this->_user->tz : $timezone; + + parent::__construct($time, $timezone); + } + + /** + * Returns a UNIX timestamp representation of the date time. + * + * @return int UNIX timestamp + */ + public function getTimestamp() + { + static $compat; + + if (!isset($compat)) + { + $compat = !method_exists('DateTime', 'getTimestamp'); + } + + return !$compat ? parent::getTimestamp() : (int) parent::format('U'); + } + + /** + * Formats the current date time into the specified format + * + * @param string $format Optional format to use for output, defaults to users chosen format + * @param boolean $force_absolute Force output of a non relative date + * @return string Formatted date time + */ + public function format($format = '', $force_absolute = false) + { + $format = $format ? $format : $this->_user->date_format; + $relative = (strpos($format, self::RELATIVE_WRAPPER) !== false && !$force_absolute); + $now = new self('now', $this->_user->tz, $this->_user); + $delta = $now->getTimestamp() - $this->getTimestamp(); + + if ($relative) + { + if ($delta <= 3600 && ($delta >= -5 || (($now->getTimestamp() / 60) % 60) == (($this->getTimestamp() / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) + { + return $this->_user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); + } + else + { + $midnight = clone $now; + $midnight->setTime(0, 0, 0); + + $midnight = $midnight->getTimestamp(); + $gmepoch = $this->getTimestamp(); + + if (!($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800)) + { + $day = false; + + if ($gmepoch > $midnight + 86400) + { + $day = 'TOMORROW'; + } + else if ($gmepoch > $midnight) + { + $day = 'TODAY'; + } + else if ($gmepoch > $midnight - 86400) + { + $day = 'YESTERDAY'; + } + + if ($day !== false) + { + $format = self::_format_cache($format, $this->_user); + + return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); + } + } + } + } + + $format = self::_format_cache($format, $this->_user); + + return strtr(parent::format($format['format_long']), $format['lang']); + } + + /** + * Pre-processes the specified date format + * + * @param string $format Output format + * @param user $user User object to use for localisation + * @return array Processed date format + */ + static protected function _format_cache($format, $user) + { + $lang = $user->lang_name; + + if (!isset(self::$format_cache[$lang])) + { + self::$format_cache[$lang] = array(); + + if (!isset(self::$format_cache[$lang][$format])) + { + // Is the user requesting a friendly date format (i.e. 'Today 12:42')? + self::$format_cache[$lang][$format] = array( + 'is_short' => strpos($format, self::RELATIVE_WRAPPER) !== false, + 'format_short' => substr($format, 0, strpos($format, self::RELATIVE_WRAPPER)) . self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER . substr(strrchr($format, self::RELATIVE_WRAPPER), 1), + 'format_long' => str_replace(self::RELATIVE_WRAPPER, '', $format), + 'lang' => $user->lang['datetime'], + ); + + // Short representation of month in format? Some languages use different terms for the long and short format of May + if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false)) + { + self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short']; + } + } + } + + return self::$format_cache[$lang][$format]; + } +} -- cgit v1.2.1 From e8b60fc3d8b483fda50c715e59b73861cd3ced9e Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 18 Apr 2010 22:26:33 +0100 Subject: [feature/new-tz-handling] Use phpbb_datetime rather than phpbb_DateTime. PHPBB3-9558 --- phpBB/includes/datetime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index b1e95e58a3..d14693faa3 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -11,7 +11,7 @@ * phpBB custom extensions to the PHP DateTime class * This handles the relative formats phpBB employs */ -class phpbb_DateTime extends DateTime +class phpbb_datetime extends DateTime { /** * String used to wrap the date segment which should be replaced by today/tomorrow/yesterday @@ -29,7 +29,7 @@ class phpbb_DateTime extends DateTime static protected $format_cache = array(); /** - * Constructs a new instance of phpbb_DateTime, expanded to include an argument to inject + * Constructs a new instance of phpbb_datetime, expanded to include an argument to inject * the user context and modify the timezone to the users selected timezone if one is not set. * * @param string $time String in a format accepted by strtotime(). -- cgit v1.2.1 From a5c3ff376911f2f25595f1a540c7a16395dac67d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 20 Apr 2010 19:30:05 +0100 Subject: [feature/new-tz-handling] Renamed old variables and removed extra conditional. PHPBB3-9558 --- phpBB/includes/datetime.php | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index d14693faa3..92aef88599 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -87,32 +87,27 @@ class phpbb_datetime extends DateTime $midnight = clone $now; $midnight->setTime(0, 0, 0); - $midnight = $midnight->getTimestamp(); - $gmepoch = $this->getTimestamp(); + $midnight = $midnight->getTimestamp(); + $timestamp = $this->getTimestamp(); - if (!($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800)) + if ($timestamp > $midnight + 86400) { - $day = false; - - if ($gmepoch > $midnight + 86400) - { - $day = 'TOMORROW'; - } - else if ($gmepoch > $midnight) - { - $day = 'TODAY'; - } - else if ($gmepoch > $midnight - 86400) - { - $day = 'YESTERDAY'; - } - - if ($day !== false) - { - $format = self::_format_cache($format, $this->_user); - - return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); - } + $day = 'TOMORROW'; + } + else if ($timestamp > $midnight) + { + $day = 'TODAY'; + } + else if ($timestamp > $midnight - 86400) + { + $day = 'YESTERDAY'; + } + + if ($day !== false) + { + $format = self::_format_cache($format, $this->_user); + + return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); } } } -- cgit v1.2.1 From 3559d2062480d5c3a0e94dae7388f9f1e5918ea7 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 20 Apr 2010 19:43:49 +0100 Subject: [feature/new-tz-handling] Update user methods to use new date processing class. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit user::setup() now stores a DateTimeZone object in user::$timezone representing the users timezone. For backwards compatibility a numeric value in user/board_timezone will be converted into one of the legacy Etc/GMT±X timezones. This will be used until the user updates his/her timezone in the UCP. user::format_date() is now basically a legacy wrapper that transforms a UTC UNIX timestamp into a formatted localised date using phpbb_datetime::format(). PHPBB3-9558 --- phpBB/includes/session.php | 87 +++++++++++++--------------------------------- 1 file changed, 24 insertions(+), 63 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index 497aaf1141..ba9136b568 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -1574,20 +1574,26 @@ class user extends session { global $db, $template, $config, $auth, $phpEx, $phpbb_root_path, $cache; + // @todo Move this to a better location + if (!class_exists('phpbb_datetime')) + { + global $phpbb_root_path, $phpEx; + + require "{$phpbb_root_path}includes/datetime.$phpEx"; + } + if ($this->data['user_id'] != ANONYMOUS) { $this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']); $this->date_format = $this->data['user_dateformat']; - $this->timezone = $this->data['user_timezone'] * 3600; - $this->dst = $this->data['user_dst'] * 3600; + $this->timezone = $this->data['user_timezone']; } else { $this->lang_name = basename($config['default_lang']); $this->date_format = $config['default_dateformat']; - $this->timezone = $config['board_timezone'] * 3600; - $this->dst = $config['board_dst'] * 3600; + $this->timezone = $config['board_timezone']; /** * If a guest user is surfing, we try to guess his/her language first by obtaining the browser language @@ -1626,6 +1632,14 @@ class user extends session */ } + if (is_numeric($this->timezone)) + { + // Might still be numeric by chance + $this->timezone = sprintf('Etc/GMT%+d', ($this->timezone + ($this->data['user_id'] != ANONYMOUS ? $this->data['user_dst'] : $config['board_dst']))); + } + + $this->timezone = new DateTimeZone($this->timezone); + // We include common language file here to not load it every time a custom language file is included $lang = &$this->lang; @@ -2072,70 +2086,17 @@ class user extends session */ function format_date($gmepoch, $format = false, $forcedate = false) { - static $midnight; - static $date_cache; + static $utc; - $format = (!$format) ? $this->date_format : $format; - $now = time(); - $delta = $now - $gmepoch; - - if (!isset($date_cache[$format])) + if (!isset($utc)) { - // Is the user requesting a friendly date format (i.e. 'Today 12:42')? - $date_cache[$format] = array( - 'is_short' => strpos($format, '|'), - 'format_short' => substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1), - 'format_long' => str_replace('|', '', $format), - 'lang' => $this->lang['datetime'], - ); - - // Short representation of month in format? Some languages use different terms for the long and short format of May - if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false)) - { - $date_cache[$format]['lang']['May'] = $this->lang['datetime']['May_short']; - } + $utc = new DateTimeZone('UTC'); } - // Zone offset - $zone_offset = $this->timezone + $this->dst; - - // Show date <= 1 hour ago as 'xx min ago' but not greater than 60 seconds in the future - // A small tolerence is given for times in the future but in the same minute are displayed as '< than a minute ago' - if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now / 60) % 60) == (($gmepoch / 60) % 60)) && $date_cache[$format]['is_short'] !== false && !$forcedate && isset($this->lang['datetime']['AGO'])) - { - return $this->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); - } - - if (!$midnight) - { - list($d, $m, $y) = explode(' ', gmdate('j n Y', time() + $zone_offset)); - $midnight = gmmktime(0, 0, 0, $m, $d, $y) - $zone_offset; - } - - if ($date_cache[$format]['is_short'] !== false && !$forcedate && !($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800)) - { - $day = false; - - if ($gmepoch > $midnight + 86400) - { - $day = 'TOMORROW'; - } - else if ($gmepoch > $midnight) - { - $day = 'TODAY'; - } - else if ($gmepoch > $midnight - 86400) - { - $day = 'YESTERDAY'; - } - - if ($day !== false) - { - return str_replace('||', $this->lang['datetime'][$day], strtr(@gmdate($date_cache[$format]['format_short'], $gmepoch + $zone_offset), $date_cache[$format]['lang'])); - } - } + $time = new phpbb_DateTime("@$gmepoch", $utc, $this); + $time->setTimezone($this->tz); - return strtr(@gmdate($date_cache[$format]['format_long'], $gmepoch + $zone_offset), $date_cache[$format]['lang']); + return $time->format($format, $forcedate); } /** -- cgit v1.2.1 From b2a812e36bc90d01f21da469823f4e759294b770 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Tue, 20 Apr 2010 21:01:20 +0100 Subject: [feature/new-tz-handling] Correct capitalisation of phpbb_datetime. PHPBB3-9558 --- phpBB/includes/session.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index ba9136b568..d8204e41f6 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -2093,7 +2093,7 @@ class user extends session $utc = new DateTimeZone('UTC'); } - $time = new phpbb_DateTime("@$gmepoch", $utc, $this); + $time = new phpbb_datetime("@$gmepoch", $utc, $this); $time->setTimezone($this->tz); return $time->format($format, $forcedate); -- cgit v1.2.1 From 9e1812a0ca728aee69df9ec5b86ea21756d3b1cc Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 21:31:41 +0100 Subject: [feature/new-tz-handling] Remove old user::$dst property PHPBB3-9558 --- phpBB/includes/session.php | 1 - 1 file changed, 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index d8204e41f6..fe690a1a9a 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -1529,7 +1529,6 @@ class user extends session var $theme = array(); var $date_format; var $timezone; - var $dst; var $lang_name = false; var $lang_id = false; -- cgit v1.2.1 From 6a783b843b596c46738d76f2db5d539d8f68a815 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:17:40 +0100 Subject: [feature/new-tz-handling] Replace user::$timezone with user::$tz. user::$tz will store the new DateTimeZone object representing the users timezone instead of the existing user::$timezone and user::$dst combination. PHPBB3-9558 --- phpBB/includes/session.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index fe690a1a9a..2352e6394a 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -1528,7 +1528,14 @@ class user extends session var $help = array(); var $theme = array(); var $date_format; - var $timezone; + + /** + * Users current timezone + * + * @var DateTimeZone Timezone of the user + * @since 3.1 + */ + public $tz; var $lang_name = false; var $lang_id = false; @@ -1586,13 +1593,13 @@ class user extends session $this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']); $this->date_format = $this->data['user_dateformat']; - $this->timezone = $this->data['user_timezone']; + $this->tz = $this->data['user_timezone']; } else { $this->lang_name = basename($config['default_lang']); $this->date_format = $config['default_dateformat']; - $this->timezone = $config['board_timezone']; + $this->tz = $config['board_timezone']; /** * If a guest user is surfing, we try to guess his/her language first by obtaining the browser language @@ -1631,13 +1638,13 @@ class user extends session */ } - if (is_numeric($this->timezone)) + if (is_numeric($this->tz)) { // Might still be numeric by chance - $this->timezone = sprintf('Etc/GMT%+d', ($this->timezone + ($this->data['user_id'] != ANONYMOUS ? $this->data['user_dst'] : $config['board_dst']))); + $this->tz = sprintf('Etc/GMT%+d', ($this->tz + ($this->data['user_id'] != ANONYMOUS ? $this->data['user_dst'] : $config['board_dst']))); } - $this->timezone = new DateTimeZone($this->timezone); + $this->tz = new DateTimeZone($this->tz); // We include common language file here to not load it every time a custom language file is included $lang = &$this->lang; -- cgit v1.2.1 From 74be23a098ec222cf3f3d14d6b6df236c58e8c01 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:20:08 +0100 Subject: [feature/new-tz-handling] Added a user::create_datetime() method. New method which handles instantiating new phpbb_datetime objects in the context of the current user. PHPBB3-9558 --- phpBB/includes/session.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'phpBB/includes') diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index 2352e6394a..e91606878a 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -2105,6 +2105,21 @@ class user extends session return $time->format($format, $forcedate); } + /** + * Create a phpbb_datetime object in the context of the current user + * + * @since 3.1 + * @param string $time String in a format accepted by strtotime(). + * @param DateTimeZone $timezone Time zone of the time. + * @return phpbb_datetime Date time object linked to the current users locale + */ + public function create_datetime($time, DateTimeZone $timezone = null) + { + $timezone = $timezone ? $timezone : $this->tz; + + return new phpbb_datetime($time, $timezone, $this); + } + /** * Get language id currently used by the user */ -- cgit v1.2.1 From 2e7d9ec805a8f2088ceebf22dbc97b89a72f76d0 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 23:46:20 +0100 Subject: [feature/new-tz-handling] Fixed bug with signature of user::create_datetime(). First argument to user::create_datetime() should be optional. PHPBB3-9558 --- phpBB/includes/session.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/session.php b/phpBB/includes/session.php index e91606878a..cf2efd2960 100644 --- a/phpBB/includes/session.php +++ b/phpBB/includes/session.php @@ -2113,7 +2113,7 @@ class user extends session * @param DateTimeZone $timezone Time zone of the time. * @return phpbb_datetime Date time object linked to the current users locale */ - public function create_datetime($time, DateTimeZone $timezone = null) + public function create_datetime($time = 'now', DateTimeZone $timezone = null) { $timezone = $timezone ? $timezone : $this->tz; -- cgit v1.2.1 From 522f65d079c61e9ea7f718b67e37a9e58968b0f0 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:21:40 +0100 Subject: [feature/new-tz-handling] Correct typo in member comment. PHPBB3-9558 --- phpBB/includes/datetime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 92aef88599..3a7fc04105 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -24,7 +24,7 @@ class phpbb_datetime extends DateTime protected $_user; /** - * @var array Date formats are preprocessed by phpBB, to save constact recalculation they are cached. + * @var array Date formats are preprocessed by phpBB, to save constant recalculation they are cached. */ static protected $format_cache = array(); -- cgit v1.2.1 From c521ef1591022e69fe952ec23e6614ae36dee2e2 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:22:38 +0100 Subject: [feature/new-tz-handling] Comment and optimise phpbb_datetime::format(). - Added comments explaining the complex time computations for rendering relative date times. - Replaced some repeated method invokations with variables. PHPBB3-9558 --- phpBB/includes/datetime.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 3a7fc04105..577081e40b 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -74,11 +74,18 @@ class phpbb_datetime extends DateTime $format = $format ? $format : $this->_user->date_format; $relative = (strpos($format, self::RELATIVE_WRAPPER) !== false && !$force_absolute); $now = new self('now', $this->_user->tz, $this->_user); - $delta = $now->getTimestamp() - $this->getTimestamp(); + + $timestamp = $this->getTimestamp(); + $now_ts = $now->getTimeStamp(); + + $delta = $now_ts - $timestamp; if ($relative) { - if ($delta <= 3600 && ($delta >= -5 || (($now->getTimestamp() / 60) % 60) == (($this->getTimestamp() / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) + // Check the delta is less than or equal to 1 hour + // and the delta is either greater than -5 seconds or timestamp and current time are of the same minute (they must be in the same hour already) + // finally check that relative dates are supported by the language pack + if ($delta <= 3600 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) { return $this->_user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); } @@ -88,7 +95,6 @@ class phpbb_datetime extends DateTime $midnight->setTime(0, 0, 0); $midnight = $midnight->getTimestamp(); - $timestamp = $this->getTimestamp(); if ($timestamp > $midnight + 86400) { @@ -107,6 +113,7 @@ class phpbb_datetime extends DateTime { $format = self::_format_cache($format, $this->_user); + // Format using the short formatting and finally swap out the relative token placeholder with the correct value return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); } } -- cgit v1.2.1 From dba89a534120d48d0ba901aedd69f962536bf36d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:24:43 +0100 Subject: [feature/new-tz-handling] Added phpbb_datetime::__toString(). New phpbb_datetime::__toString() magic method that formats the datetime according to the users default settings. PHPBB3-9558 --- phpBB/includes/datetime.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 577081e40b..8763697c78 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -124,6 +124,16 @@ class phpbb_datetime extends DateTime return strtr(parent::format($format['format_long']), $format['lang']); } + /** + * Magic method to convert DateTime object to string + * + * @return Formatted date time, according to the users default settings. + */ + public function __toString() + { + return $this->format(); + } + /** * Pre-processes the specified date format * -- cgit v1.2.1 From f085735ef812a57790699a6d789cbab22f882328 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:25:52 +0100 Subject: [feature/new-tz-handling] Explained name of phpbb_datetime::getTimestamp() phpbb_datetime::getTimestamp() exists purely to support PHP 5.2 which does not implement the method. PHPBB3-9558 --- phpBB/includes/datetime.php | 1 + 1 file changed, 1 insertion(+) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 8763697c78..12d04d5ca6 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -48,6 +48,7 @@ class phpbb_datetime extends DateTime /** * Returns a UNIX timestamp representation of the date time. * + * @internal This method is for backwards compatibility with 5.2, hence why it doesn't use our method naming standards. * @return int UNIX timestamp */ public function getTimestamp() -- cgit v1.2.1 From 8fe46175aff1d9275b7337a8ced8060ff850d153 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:29:36 +0100 Subject: [feature/new-tz-handling] Fix undefined variable. PHPBB3-9558 --- phpBB/includes/datetime.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 12d04d5ca6..25da6401d7 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -97,6 +97,8 @@ class phpbb_datetime extends DateTime $midnight = $midnight->getTimestamp(); + $day = false; + if ($timestamp > $midnight + 86400) { $day = 'TOMORROW'; -- cgit v1.2.1 From 5dd7916c496b76ab2e1e28b804069dde63f7dbf0 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:47:34 +0100 Subject: [feature/new-tz-handling] Check the is_short flag stored inside the format array. Reuse the existing check store in the format array to determine if the date time format supports relative formatting. PHPBB3-9558 --- phpBB/includes/datetime.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 25da6401d7..ecb3dfcf17 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -73,7 +73,8 @@ class phpbb_datetime extends DateTime public function format($format = '', $force_absolute = false) { $format = $format ? $format : $this->_user->date_format; - $relative = (strpos($format, self::RELATIVE_WRAPPER) !== false && !$force_absolute); + $format = self::_format_cache($format, $this->_user); + $relative = ($format['is_short'] && !$force_absolute); $now = new self('now', $this->_user->tz, $this->_user); $timestamp = $this->getTimestamp(); @@ -114,8 +115,6 @@ class phpbb_datetime extends DateTime if ($day !== false) { - $format = self::_format_cache($format, $this->_user); - // Format using the short formatting and finally swap out the relative token placeholder with the correct value return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); } -- cgit v1.2.1 From e9fe9ea5185679e9950e330c949cc2577bfea21d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:52:04 +0100 Subject: [feature/new-tz-handling] Fix bug from 3.0 formatting future dates. Future dates can get formatted as 'less than a minute ago' if they occur in the future on the same minute as the current minute. PHPBB3-9558 PHPBB3-9712 --- phpBB/includes/datetime.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index ecb3dfcf17..2276d36413 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -85,9 +85,10 @@ class phpbb_datetime extends DateTime if ($relative) { // Check the delta is less than or equal to 1 hour + // and the delta not more than a minute in the past // and the delta is either greater than -5 seconds or timestamp and current time are of the same minute (they must be in the same hour already) // finally check that relative dates are supported by the language pack - if ($delta <= 3600 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) + if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) { return $this->_user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); } -- cgit v1.2.1 From 6e1278655ff3f250b4f1e09aa8674bae8d86287a Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 22:57:43 +0100 Subject: [feature/new-tz-handling] Removed line that was missed in cc312d8. PHPBB3-9558 --- phpBB/includes/datetime.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 2276d36413..22b7f871fd 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -122,8 +122,6 @@ class phpbb_datetime extends DateTime } } - $format = self::_format_cache($format, $this->_user); - return strtr(parent::format($format['format_long']), $format['lang']); } -- cgit v1.2.1 From 1665434853fb09e70337d23955e1c9a5f3f0d19d Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Wed, 7 Jul 2010 23:42:54 +0100 Subject: [feature/new-tz-handling] Remove code using legacy timezone properties. Code accessing the legacy user::$timezone and user::$dst properties has been removed and replaced with code utilising user::create_datetime(). Changed by Oleg: in viewtopic, memberlist and index use getTimestamp() + getOffset(). We show members that have birthdays on the specified date. getTimestamp() returns the current date in UTC. We add getOffset() to obtain the current local time in the viewing user's timezone. Then we find members having birthday on this date. Changed by Oleg again: Take leap year status out of the datetime object we have, this seems like it should work as one would expect. PHPBB3-9558 --- phpBB/includes/functions_profile_fields.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions_profile_fields.php b/phpBB/includes/functions_profile_fields.php index ec29a1732d..aacfa82c54 100644 --- a/phpBB/includes/functions_profile_fields.php +++ b/phpBB/includes/functions_profile_fields.php @@ -555,9 +555,12 @@ class custom_profile else if ($day && $month && $year) { global $user; - // Date should display as the same date for every user regardless of timezone, so remove offset - // to compensate for the offset added by user::format_date() - return $user->format_date(gmmktime(0, 0, 0, $month, $day, $year) - ($user->timezone + $user->dst), $user->lang['DATE_FORMAT'], true); + // Date should display as the same date for every user regardless of timezone + + return $user->create_datetime() + ->setDate($year, $month, $day) + ->setTime(0, 0, 0) + ->format($user->lang['DATE_FORMAT'], true); } return $value; -- cgit v1.2.1 From 0f320a6c48d63154bba45c2937adeee79165816c Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 8 Jul 2010 21:56:51 +0100 Subject: [feature/new-tz-handling] Update tz_select() to use the PHP timezone database. tz_select() now uses the PHP timezone database to generate the timezone selection box, it tries to use a translated language string otherwise falls back to a label produced from the timezone identifier. I've done this so new timezones are available immediately without a new language pack. PHPBB3-9558 --- phpBB/includes/functions.php | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 9e2e57dd5e..fb8ea93e32 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1071,28 +1071,45 @@ function style_select($default = '', $all = false) /** * Pick a timezone +* @todo Possible HTML escaping */ function tz_select($default = '', $truncate = false) { global $user; + static $timezones; + + if (!isset($timezones)) + { + $timezones = DateTimeZone::listIdentifiers(); + + sort($timezones); + } + $tz_select = ''; - foreach ($user->lang['tz_zones'] as $offset => $zone) + + foreach ($timezones as $timezone) { - if ($truncate) + if (isset($user->lang['timezones'][$timezone])) { - $zone_trunc = truncate_string($zone, 50, 255, false, '...'); + $title = $label = $user->lang['timezones'][$timezone]; } else { - $zone_trunc = $zone; + // No label, we'll figure one out + // @todo rtl languages? + $bits = explode('/', strtolower(str_replace('_', ' ', $timezone))); + + $title = $label = ucwords(implode(' - ', $bits)); } - if (is_numeric($offset)) + if ($truncate) { - $selected = ($offset == $default) ? ' selected="selected"' : ''; - $tz_select .= ''; + $label = truncate_string($label, 50, 255, false, '...'); } + + $selected = ($timezone === $default) ? ' selected="selected"' : ''; + $tz_select .= ''; } return $tz_select; -- cgit v1.2.1 From 190b019fa28f59c018554916e33446d93efb7311 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 8 Jul 2010 22:09:24 +0100 Subject: [feature/new-tz-handling] Remove case mangling, the identifiers are correct. PHPBB3-9558 --- phpBB/includes/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index fb8ea93e32..69bfe3e090 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1098,9 +1098,9 @@ function tz_select($default = '', $truncate = false) { // No label, we'll figure one out // @todo rtl languages? - $bits = explode('/', strtolower(str_replace('_', ' ', $timezone))); + $bits = explode('/', str_replace('_', ' ', $timezone)); - $title = $label = ucwords(implode(' - ', $bits)); + $title = $label = implode(' - ', $bits); } if ($truncate) -- cgit v1.2.1 From f17664a00c9fa3b80a887bde2cdb2424a5d5567a Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Fri, 9 Jul 2010 20:39:30 +0100 Subject: [feature/new-tz-handling] Correct a bug preventing multiple formats working. PHPBB3-9558 --- phpBB/includes/datetime.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 22b7f871fd..15e3c8b0b7 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -149,22 +149,22 @@ class phpbb_datetime extends DateTime if (!isset(self::$format_cache[$lang])) { self::$format_cache[$lang] = array(); + } - if (!isset(self::$format_cache[$lang][$format])) + if (!isset(self::$format_cache[$lang][$format])) + { + // Is the user requesting a friendly date format (i.e. 'Today 12:42')? + self::$format_cache[$lang][$format] = array( + 'is_short' => strpos($format, self::RELATIVE_WRAPPER) !== false, + 'format_short' => substr($format, 0, strpos($format, self::RELATIVE_WRAPPER)) . self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER . substr(strrchr($format, self::RELATIVE_WRAPPER), 1), + 'format_long' => str_replace(self::RELATIVE_WRAPPER, '', $format), + 'lang' => $user->lang['datetime'], + ); + + // Short representation of month in format? Some languages use different terms for the long and short format of May + if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false)) { - // Is the user requesting a friendly date format (i.e. 'Today 12:42')? - self::$format_cache[$lang][$format] = array( - 'is_short' => strpos($format, self::RELATIVE_WRAPPER) !== false, - 'format_short' => substr($format, 0, strpos($format, self::RELATIVE_WRAPPER)) . self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER . substr(strrchr($format, self::RELATIVE_WRAPPER), 1), - 'format_long' => str_replace(self::RELATIVE_WRAPPER, '', $format), - 'lang' => $user->lang['datetime'], - ); - - // Short representation of month in format? Some languages use different terms for the long and short format of May - if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false)) - { - self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short']; - } + self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short']; } } -- cgit v1.2.1 From bb461c8daa97358e8bcce923a21eba0abd6ea3dc Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Thu, 10 Mar 2011 19:14:53 -0500 Subject: [feature/new-tz-handling] Sort timezones in selector by offset. Since the list of timezones is very long, and users are likely to know their current offset but not necessarily which city that is nearby is in the timezone database, sort the list of timezones by offset. UTC is specially handled to show up before other GMT+0 timezones. PHPBB3-9558 --- phpBB/includes/functions.php | 71 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 69bfe3e090..31a191b513 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1069,6 +1069,65 @@ function style_select($default = '', $all = false) return $style_options; } +function phpbb_format_timezone_offset($tz_offset) +{ + $sign = ($tz_offset < 0) ? '-' : '+'; + $time_offset = abs($tz_offset); + + $offset_seconds = $time_offset % 3600; + $offset_minutes = $offset_seconds / 60; + $offset_hours = ($time_offset - $offset_seconds) / 3600; + + $offset_string = sprintf("%s%02d:%02d", $sign, $offset_hours, $offset_minutes); + return $offset_string; +} + +// Compares two time zone labels. +// Arranges them in increasing order by timezone offset. +// Places UTC before other timezones in the same offset. +function tz_select_compare($a, $b) +{ + $a_sign = $a[3]; + $b_sign = $b[3]; + if ($a_sign != $b_sign) + { + return $a_sign == '-' ? -1 : 1; + } + + $a_offset = substr($a, 4, 5); + $b_offset = substr($b, 4, 5); + if ($a_offset == $b_offset) + { + $a_name = substr($a, 12); + $b_name = substr($b, 12); + if ($a_name == $b_name) + { + return 0; + } else if ($a_name == 'UTC') + { + return -1; + } else if ($b_name == 'UTC') + { + return 1; + } + else + { + return $a_name < $b_name ? -1 : 1; + } + } + else + { + if ($a_sign == '-') + { + return $a_offset > $b_offset ? -1 : 1; + } + else + { + return $a_offset < $b_offset ? -1 : 1; + } + } +} + /** * Pick a timezone * @todo Possible HTML escaping @@ -1083,7 +1142,17 @@ function tz_select($default = '', $truncate = false) { $timezones = DateTimeZone::listIdentifiers(); - sort($timezones); + foreach ($timezones as &$timezone) + { + $tz = new DateTimeZone($timezone); + $dt = new phpbb_datetime('now', $tz); + $offset = $dt->getOffset(); + $offset_string = phpbb_format_timezone_offset($offset); + $timezone = 'GMT' . $offset_string . ' - ' . $timezone; + } + unset($timezone); + + usort($timezones, 'tz_select_compare'); } $tz_select = ''; -- cgit v1.2.1 From 50936cb2eff3f80d99390c76ef6ac535e73f6cc3 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Jun 2012 19:06:46 +0200 Subject: [feature/new-tz-handling] Fix selecting and validating of timezone in UCP PHPBB3-9558 --- phpBB/includes/functions.php | 24 ++++++++++++++---------- phpBB/includes/functions_user.php | 16 ++++++++++++++++ phpBB/includes/ucp/ucp_prefs.php | 4 ++-- 3 files changed, 32 insertions(+), 12 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 3ec4b76091..55f7f0531c 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1139,34 +1139,38 @@ function tz_select($default = '', $truncate = false) if (!isset($timezones)) { - $timezones = DateTimeZone::listIdentifiers(); + $unsorted_timezones = DateTimeZone::listIdentifiers(); + $timezones = array(); - foreach ($timezones as &$timezone) + foreach ($unsorted_timezones as $timezone) { $tz = new DateTimeZone($timezone); $dt = new phpbb_datetime('now', $tz); $offset = $dt->getOffset(); $offset_string = phpbb_format_timezone_offset($offset); - $timezone = 'GMT' . $offset_string . ' - ' . $timezone; + $timezones['GMT' . $offset_string . ' - ' . $timezone] = array( + 'tz' => $timezone, + 'label' => 'GMT' . $offset_string . ' - ' . $timezone, + ); } - unset($timezone); + unset($unsorted_timezones); - usort($timezones, 'tz_select_compare'); + uksort($timezones, 'tz_select_compare'); } $tz_select = ''; foreach ($timezones as $timezone) { - if (isset($user->lang['timezones'][$timezone])) + if (isset($user->lang['timezones'][$timezone['tz']])) { - $title = $label = $user->lang['timezones'][$timezone]; + $title = $label = $user->lang['timezones'][$timezone['tz']]; } else { // No label, we'll figure one out // @todo rtl languages? - $bits = explode('/', str_replace('_', ' ', $timezone)); + $bits = explode('/', str_replace('_', ' ', $timezone['label'])); $title = $label = implode(' - ', $bits); } @@ -1176,8 +1180,8 @@ function tz_select($default = '', $truncate = false) $label = truncate_string($label, 50, 255, false, '...'); } - $selected = ($timezone === $default) ? ' selected="selected"' : ''; - $tz_select .= ''; + $selected = ($timezone['tz'] === $default) ? ' selected="selected"' : ''; + $tz_select .= ''; } return $tz_select; diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 9b102b7387..3a77407c20 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -1395,6 +1395,22 @@ function validate_language_iso_name($lang_iso) return ($lang_id) ? false : 'WRONG_DATA'; } +/** +* Validate Timezone Name +* +* Tests whether a timezone name is valid +* +* @param string $timezone The timezone string to test +* +* @return bool|string Either false if validation succeeded or +* a string which will be used as the error message +* (with the variable name appended) +*/ +function validate_timezone($timezone) +{ + return (in_array($timezone, DateTimeZone::listIdentifiers())) ? false : 'TIMEZONE_INVALID'; +} + /** * Check to see if the username has been taken, or if it is disallowed. * Also checks if it includes the " character, which we don't allow in usernames. diff --git a/phpBB/includes/ucp/ucp_prefs.php b/phpBB/includes/ucp/ucp_prefs.php index 0c9f20f266..45e3bb8951 100644 --- a/phpBB/includes/ucp/ucp_prefs.php +++ b/phpBB/includes/ucp/ucp_prefs.php @@ -41,7 +41,7 @@ class ucp_prefs 'dateformat' => request_var('dateformat', $user->data['user_dateformat'], true), 'lang' => basename(request_var('lang', $user->data['user_lang'])), 'style' => request_var('style', (int) $user->data['user_style']), - 'tz' => request_var('tz', (float) $user->data['user_timezone']), + 'tz' => request_var('tz', $user->data['user_timezone']), 'dst' => request_var('dst', (bool) $user->data['user_dst']), 'viewemail' => request_var('viewemail', (bool) $user->data['user_allow_viewemail']), @@ -72,7 +72,7 @@ class ucp_prefs $error = validate_data($data, array( 'dateformat' => array('string', false, 1, 30), 'lang' => array('language_iso_name'), - 'tz' => array('num', false, -14, 14), + 'tz' => array('timezone'), )); if (!check_form_key('ucp_prefs_personal')) -- cgit v1.2.1 From 00b5e5345dc44c99340bba932522b6b05b48dab2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Jun 2012 19:19:34 +0200 Subject: [feature/new-tz-handling] Fix displaying of "All times are" string PHPBB3-9558 --- phpBB/includes/functions.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 55f7f0531c..44346c7795 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4769,6 +4769,14 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 } } + $dt = new phpbb_datetime('now', $user->tz); + $timezone_offset = 'GMT' . phpbb_format_timezone_offset($dt->getOffset()); + $timezone_name = $user->tz->getName(); + if (isset($user->lang['timezones'][$timezone_name])) + { + $timezone_name = $user->lang['timezones'][$timezone_name]; + } + // The following assigns all _common_ variables that may be used at any point in a template. $template->assign_vars(array( 'SITENAME' => $config['sitename'], @@ -4836,7 +4844,7 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 'S_CONTENT_FLOW_BEGIN' => ($user->lang['DIRECTION'] == 'ltr') ? 'left' : 'right', 'S_CONTENT_FLOW_END' => ($user->lang['DIRECTION'] == 'ltr') ? 'right' : 'left', 'S_CONTENT_ENCODING' => 'UTF-8', - 'S_TIMEZONE' => ($user->data['user_dst'] || ($user->data['user_id'] == ANONYMOUS && $config['board_dst'])) ? sprintf($user->lang['ALL_TIMES'], $user->lang['tz'][$tz], $user->lang['tz']['dst']) : sprintf($user->lang['ALL_TIMES'], $user->lang['tz'][$tz], ''), + 'S_TIMEZONE' => sprintf($user->lang['ALL_TIMES'], $timezone_offset, $timezone_name), 'S_DISPLAY_ONLINE_LIST' => ($l_online_time) ? 1 : 0, 'S_DISPLAY_SEARCH' => (!$config['load_search']) ? 0 : (isset($auth) ? ($auth->acl_get('u_search') && $auth->acl_getf_global('f_search')) : 1), 'S_DISPLAY_PM' => ($config['allow_privmsg'] && !empty($user->data['is_registered']) && ($auth->acl_get('u_readpm') || $auth->acl_get('u_sendpm'))) ? true : false, -- cgit v1.2.1 From 09499fb128802bd19cee1550bf6e9af80a9388e2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Jun 2012 22:33:03 +0200 Subject: [feature/new-tz-handling] Fix handling of timezone and dst in dateformat_select PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index d537885ef1..ce2ddc3b0b 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -906,11 +906,13 @@ class acp_board global $user, $config; // Let the format_date function operate with the acp values - $old_tz = $user->timezone; - $old_dst = $user->dst; - - $user->timezone = $config['board_timezone'] * 3600; - $user->dst = $config['board_dst'] * 3600; + $old_tz = $user->tz; + if (is_numeric($config['board_timezone'])) + { + // Might still be numeric by chance + $config['board_timezone'] = sprintf('Etc/GMT%+d', $config['board_timezone']); + } + $user->tz = new DateTimeZone($config['board_timezone']); $dateformat_options = ''; @@ -929,8 +931,7 @@ class acp_board $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . ''; // Reset users date options - $user->timezone = $old_tz; - $user->dst = $old_dst; + $user->tz = $old_tz; return " "; -- cgit v1.2.1 From 66ae9ee2ea8b65ddb4c39b4bc9add8d2aca2e73d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Jun 2012 22:39:35 +0200 Subject: [feature/new-tz-handling] Fix timezone selection on registration page PHPBB3-9558 --- phpBB/includes/ucp/ucp_register.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index 5d85029e62..aee55711b2 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -100,7 +100,7 @@ class ucp_register 'username' => utf8_normalize_nfc(request_var('username', '', true)), 'email' => strtolower(request_var('email', '')), 'lang' => $user->lang_name, - 'tz' => request_var('tz', (float) $config['board_timezone']), + 'tz' => request_var('tz', $config['board_timezone']), )); } @@ -172,7 +172,7 @@ class ucp_register 'password_confirm' => request_var('password_confirm', '', true), 'email' => strtolower(request_var('email', '')), 'lang' => basename(request_var('lang', $user->lang_name)), - 'tz' => request_var('tz', (float) $timezone), + 'tz' => request_var('tz', $timezone), ); // Check and initialize some variables if needed @@ -189,7 +189,7 @@ class ucp_register 'email' => array( array('string', false, 6, 60), array('email')), - 'tz' => array('num', false, -14, 14), + 'tz' => array('timezone'), 'lang' => array('language_iso_name'), )); @@ -279,7 +279,7 @@ class ucp_register 'user_password' => phpbb_hash($data['new_password']), 'user_email' => $data['email'], 'group_id' => (int) $group_id, - 'user_timezone' => (float) $data['tz'], + 'user_timezone' => $data['tz'], 'user_dst' => $is_dst, 'user_lang' => $data['lang'], 'user_type' => $user_type, @@ -453,7 +453,7 @@ class ucp_register 'L_PASSWORD_EXPLAIN' => $user->lang($config['pass_complex'] . '_EXPLAIN', $user->lang('CHARACTERS', (int) $config['min_pass_chars']), $user->lang('CHARACTERS', (int) $config['max_pass_chars'])), 'S_LANG_OPTIONS' => language_select($data['lang']), - 'S_TZ_OPTIONS' => tz_select($data['tz']), + 'S_TZ_OPTIONS' => tz_select($data['tz'], true), 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false, 'S_REGISTRATION' => true, 'S_COPPA' => $coppa, -- cgit v1.2.1 From f9bc8252641ec69983acfc6d392770934b7a37a4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Jun 2012 22:40:09 +0200 Subject: [feature/new-tz-handling] Fix timezone validation in ACP user section PHPBB3-9558 --- phpBB/includes/acp/acp_users.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index 17687b05c7..825bba514f 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1458,7 +1458,7 @@ class acp_users $data = array( 'dateformat' => utf8_normalize_nfc(request_var('dateformat', $user_row['user_dateformat'], true)), 'lang' => basename(request_var('lang', $user_row['user_lang'])), - 'tz' => request_var('tz', (float) $user_row['user_timezone']), + 'tz' => request_var('tz', $user_row['user_timezone']), 'style' => request_var('style', $user_row['user_style']), 'dst' => request_var('dst', $user_row['user_dst']), 'viewemail' => request_var('viewemail', $user_row['user_allow_viewemail']), @@ -1495,7 +1495,7 @@ class acp_users $error = validate_data($data, array( 'dateformat' => array('string', false, 1, 30), 'lang' => array('match', false, '#^[a-z_\-]{2,}$#i'), - 'tz' => array('num', false, -14, 14), + 'tz' => array('timezone'), 'topic_sk' => array('string', false, 1, 1), 'topic_sd' => array('string', false, 1, 1), -- cgit v1.2.1 From 963d4afc2cf5fb2903ed02ada20cdbf0188d57db Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Jun 2012 23:14:41 +0200 Subject: [feature/new-tz-handling] Replace gmmktime() and mktime() with phpbb_datetime PHPBB3-9558 --- phpBB/includes/functions_user.php | 7 ++++--- phpBB/includes/ucp/ucp_register.php | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 3a77407c20..f235b2be55 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -197,7 +197,6 @@ function user_add($user_row, $cp_data = false) 'user_lastpost_time' => 0, 'user_lastpage' => '', 'user_posts' => 0, - 'user_dst' => (int) $config['board_dst'], 'user_colour' => '', 'user_occ' => '', 'user_interests' => '', @@ -677,8 +676,10 @@ function user_ban($mode, $ban, $ban_len, $ban_len_other, $ban_exclude, $ban_reas if (sizeof($ban_other) == 3 && ((int)$ban_other[0] < 9999) && (strlen($ban_other[0]) == 4) && (strlen($ban_other[1]) == 2) && (strlen($ban_other[2]) == 2)) { - $time_offset = (isset($user->timezone) && isset($user->dst)) ? (int) $user->timezone + (int) $user->dst : 0; - $ban_end = max($current_time, gmmktime(0, 0, 0, (int)$ban_other[1], (int)$ban_other[2], (int)$ban_other[0]) - $time_offset); + $ban_end = max($current_time, $user->create_datetime() + ->setDate((int) $ban_other[0], (int) $ban_other[1], (int) $ban_other[2]) + ->setTime(0, 0, 0) + ->getTimestamp() + $user->tz->getOffset(new DateTime('UTC'))); } else { diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index aee55711b2..ac002a26c2 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -120,7 +120,10 @@ class ucp_register if ($coppa === false && $config['coppa_enable']) { $now = getdate(); - $coppa_birthday = $user->format_date(mktime($now['hours'] + $user->data['user_dst'], $now['minutes'], $now['seconds'], $now['mon'], $now['mday'] - 1, $now['year'] - 13), $user->lang['DATE_FORMAT']); + $coppa_birthday = $user->create_datetime() + ->setDate($now['year'] - 13, $now['mon'], $now['mday'] - 1) + ->setTime(0, 0, 0) + ->format($user->lang['DATE_FORMAT'], true); unset($now); $template->assign_vars(array( @@ -163,7 +166,6 @@ class ucp_register $captcha->init(CONFIRM_REG); } - $is_dst = $config['board_dst']; $timezone = $config['board_timezone']; $data = array( @@ -280,7 +282,6 @@ class ucp_register 'user_email' => $data['email'], 'group_id' => (int) $group_id, 'user_timezone' => $data['tz'], - 'user_dst' => $is_dst, 'user_lang' => $data['lang'], 'user_type' => $user_type, 'user_actkey' => $user_actkey, -- cgit v1.2.1 From 3c6272ff0475dc19cc67553f370ce227214d0613 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 4 Jun 2012 23:28:48 +0200 Subject: [feature/new-tz-handling] Remove appearances of board_dst and user_dst PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 1 - phpBB/includes/acp/acp_users.php | 3 --- phpBB/includes/functions_convert.php | 2 +- phpBB/includes/questionnaire/questionnaire.php | 1 - phpBB/includes/ucp/ucp_prefs.php | 3 --- phpBB/includes/user.php | 2 +- 6 files changed, 2 insertions(+), 10 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index ce2ddc3b0b..e018655407 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -58,7 +58,6 @@ class acp_board 'default_lang' => array('lang' => 'DEFAULT_LANGUAGE', 'validate' => 'lang', 'type' => 'select', 'function' => 'language_select', 'params' => array('{CONFIG_VALUE}'), 'explain' => false), 'default_dateformat' => array('lang' => 'DEFAULT_DATE_FORMAT', 'validate' => 'string', 'type' => 'custom', 'method' => 'dateformat_select', 'explain' => true), 'board_timezone' => array('lang' => 'SYSTEM_TIMEZONE', 'validate' => 'string', 'type' => 'select', 'function' => 'tz_select', 'params' => array('{CONFIG_VALUE}', 1), 'explain' => true), - 'board_dst' => array('lang' => 'SYSTEM_DST', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false), 'default_style' => array('lang' => 'DEFAULT_STYLE', 'validate' => 'int', 'type' => 'select', 'function' => 'style_select', 'params' => array('{CONFIG_VALUE}', false), 'explain' => false), 'override_user_style' => array('lang' => 'OVERRIDE_STYLE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index 825bba514f..b863a9ed80 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1460,7 +1460,6 @@ class acp_users 'lang' => basename(request_var('lang', $user_row['user_lang'])), 'tz' => request_var('tz', $user_row['user_timezone']), 'style' => request_var('style', $user_row['user_style']), - 'dst' => request_var('dst', $user_row['user_dst']), 'viewemail' => request_var('viewemail', $user_row['user_allow_viewemail']), 'massemail' => request_var('massemail', $user_row['user_allow_massemail']), 'hideonline' => request_var('hideonline', !$user_row['user_allow_viewonline']), @@ -1531,7 +1530,6 @@ class acp_users 'user_notify_type' => $data['notifymethod'], 'user_notify_pm' => $data['notifypm'], - 'user_dst' => $data['dst'], 'user_dateformat' => $data['dateformat'], 'user_lang' => $data['lang'], 'user_timezone' => $data['tz'], @@ -1654,7 +1652,6 @@ class acp_users 'NOTIFY_BOTH' => ($data['notifymethod'] == NOTIFY_BOTH) ? true : false, 'NOTIFY_PM' => $data['notifypm'], 'POPUP_PM' => $data['popuppm'], - 'DST' => $data['dst'], 'BBCODE' => $data['bbcode'], 'SMILIES' => $data['smilies'], 'ATTACH_SIG' => $data['sig'], diff --git a/phpBB/includes/functions_convert.php b/phpBB/includes/functions_convert.php index e9ec153c50..ac791e0d9b 100644 --- a/phpBB/includes/functions_convert.php +++ b/phpBB/includes/functions_convert.php @@ -1884,7 +1884,7 @@ function add_bots() 'user_email' => '', 'user_lang' => $config['default_lang'], 'user_style' => 1, - 'user_timezone' => 0, + 'user_timezone' => 'UTC', 'user_allow_massemail' => 0, ); diff --git a/phpBB/includes/questionnaire/questionnaire.php b/phpBB/includes/questionnaire/questionnaire.php index 46a743d7e9..5cb441d536 100644 --- a/phpBB/includes/questionnaire/questionnaire.php +++ b/phpBB/includes/questionnaire/questionnaire.php @@ -304,7 +304,6 @@ class phpbb_questionnaire_phpbb_data_provider 'avatar_max_width' => true, 'avatar_min_height' => true, 'avatar_min_width' => true, - 'board_dst' => true, 'board_email_form' => true, 'board_hide_emails' => true, 'board_timezone' => true, diff --git a/phpBB/includes/ucp/ucp_prefs.php b/phpBB/includes/ucp/ucp_prefs.php index 45e3bb8951..3f5f573389 100644 --- a/phpBB/includes/ucp/ucp_prefs.php +++ b/phpBB/includes/ucp/ucp_prefs.php @@ -43,7 +43,6 @@ class ucp_prefs 'style' => request_var('style', (int) $user->data['user_style']), 'tz' => request_var('tz', $user->data['user_timezone']), - 'dst' => request_var('dst', (bool) $user->data['user_dst']), 'viewemail' => request_var('viewemail', (bool) $user->data['user_allow_viewemail']), 'massemail' => request_var('massemail', (bool) $user->data['user_allow_massemail']), 'hideonline' => request_var('hideonline', (bool) !$user->data['user_allow_viewonline']), @@ -93,7 +92,6 @@ class ucp_prefs 'user_notify_pm' => $data['notifypm'], 'user_options' => $user->data['user_options'], - 'user_dst' => $data['dst'], 'user_dateformat' => $data['dateformat'], 'user_lang' => $data['lang'], 'user_timezone' => $data['tz'], @@ -145,7 +143,6 @@ class ucp_prefs 'S_HIDE_ONLINE' => $data['hideonline'], 'S_NOTIFY_PM' => $data['notifypm'], 'S_POPUP_PM' => $data['popuppm'], - 'S_DST' => $data['dst'], 'DATE_FORMAT' => $data['dateformat'], 'A_DATE_FORMAT' => addslashes($data['dateformat']), diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index 4c62dd93d7..a36d837fbd 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -126,7 +126,7 @@ class phpbb_user extends phpbb_session if (is_numeric($this->tz)) { // Might still be numeric by chance - $this->tz = sprintf('Etc/GMT%+d', ($this->tz + ($this->data['user_id'] != ANONYMOUS ? $this->data['user_dst'] : $config['board_dst']))); + $this->tz = sprintf('Etc/GMT%+d', $this->tz); } $this->tz = new DateTimeZone($this->tz); -- cgit v1.2.1 From 66f7d45603417df7619323977de62aebce0bf676 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 5 Jun 2012 21:44:44 +0200 Subject: [feature/new-tz-handling] Introduce 2 step timezone selection using javascript PHPBB3-9558 --- phpBB/includes/functions.php | 43 +++++++++++++++++++++++++++++++--------- phpBB/includes/ucp/ucp_prefs.php | 4 +++- 2 files changed, 37 insertions(+), 10 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 44346c7795..9263833b4c 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1129,14 +1129,14 @@ function tz_select_compare($a, $b) /** * Pick a timezone -* @todo Possible HTML escaping */ -function tz_select($default = '', $truncate = false) +function tz_select($default = '', $truncate = false, $return_tzs_only = true) { global $user; static $timezones; + $default_offset = ''; if (!isset($timezones)) { $unsorted_timezones = DateTimeZone::listIdentifiers(); @@ -1147,21 +1147,37 @@ function tz_select($default = '', $truncate = false) $tz = new DateTimeZone($timezone); $dt = new phpbb_datetime('now', $tz); $offset = $dt->getOffset(); + $current_time = $dt->format($user->lang['DATETIME_FORMAT'], true); $offset_string = phpbb_format_timezone_offset($offset); $timezones['GMT' . $offset_string . ' - ' . $timezone] = array( 'tz' => $timezone, - 'label' => 'GMT' . $offset_string . ' - ' . $timezone, + 'offest' => 'GMT' . $offset_string, + 'current' => $current_time, ); + if ($timezone === $default) + { + $default_offset = 'GMT' . $offset_string; + } } unset($unsorted_timezones); uksort($timezones, 'tz_select_compare'); } - $tz_select = ''; + $tz_select = $tz_dates = $opt_group = ''; foreach ($timezones as $timezone) { + if ($opt_group != $timezone['offest']) + { + $tz_select .= ($opt_group) ? '' : ''; + $tz_select .= ''; + $opt_group = $timezone['offest']; + + $selected = ($default_offset == $timezone['offest']) ? ' selected="selected"' : ''; + $tz_dates .= ''; + } + if (isset($user->lang['timezones'][$timezone['tz']])) { $title = $label = $user->lang['timezones'][$timezone['tz']]; @@ -1169,10 +1185,10 @@ function tz_select($default = '', $truncate = false) else { // No label, we'll figure one out - // @todo rtl languages? - $bits = explode('/', str_replace('_', ' ', $timezone['label'])); + $bits = explode('/', str_replace('_', ' ', $timezone['tz'])); - $title = $label = implode(' - ', $bits); + $label = implode(' - ', $bits); + $title = $timezone['offest'] . ' - ' . $label; } if ($truncate) @@ -1181,10 +1197,19 @@ function tz_select($default = '', $truncate = false) } $selected = ($timezone['tz'] === $default) ? ' selected="selected"' : ''; - $tz_select .= ''; + $tz_select .= ''; } + $tz_select .= ''; - return $tz_select; + if ($return_tzs_only) + { + return $tz_select; + } + + return array( + 'tz_select' => $tz_select, + 'tz_dates' => $tz_dates, + ); } // Functions handling topic/post tracking/marking diff --git a/phpBB/includes/ucp/ucp_prefs.php b/phpBB/includes/ucp/ucp_prefs.php index 3f5f573389..4239afc96e 100644 --- a/phpBB/includes/ucp/ucp_prefs.php +++ b/phpBB/includes/ucp/ucp_prefs.php @@ -131,6 +131,7 @@ class ucp_prefs } $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . ''; + $tz_select = tz_select($data['tz'], true, false); $template->assign_vars(array( 'ERROR' => (sizeof($error)) ? implode('
', $error) : '', @@ -153,7 +154,8 @@ class ucp_prefs 'S_LANG_OPTIONS' => language_select($data['lang']), 'S_STYLE_OPTIONS' => ($config['override_user_style']) ? '' : style_select($data['style']), - 'S_TZ_OPTIONS' => tz_select($data['tz'], true), + 'S_TZ_OPTIONS' => $tz_select['tz_select'], + 'S_TZ_DATE_OPTIONS' => $tz_select['tz_dates'], 'S_CAN_HIDE_ONLINE' => ($auth->acl_get('u_hideonline')) ? true : false, 'S_SELECT_NOTIFY' => ($config['jab_enable'] && $user->data['user_jabber'] && @extension_loaded('xml')) ? true : false) ); -- cgit v1.2.1 From 435573a9cb82060ddae673aa1e1572bd78c7741d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 12 Jun 2012 19:52:33 +0200 Subject: [feature/new-tz-handling] Fix Timezone selection on registration page PHPBB3-9558 --- phpBB/includes/ucp/ucp_register.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index ac002a26c2..804bd2e0e2 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -442,6 +442,7 @@ class ucp_register break; } + $tz_select = tz_select($data['tz'], true, false); $template->assign_vars(array( 'ERROR' => (sizeof($error)) ? implode('
', $error) : '', 'USERNAME' => $data['username'], @@ -454,7 +455,8 @@ class ucp_register 'L_PASSWORD_EXPLAIN' => $user->lang($config['pass_complex'] . '_EXPLAIN', $user->lang('CHARACTERS', (int) $config['min_pass_chars']), $user->lang('CHARACTERS', (int) $config['max_pass_chars'])), 'S_LANG_OPTIONS' => language_select($data['lang']), - 'S_TZ_OPTIONS' => tz_select($data['tz'], true), + 'S_TZ_OPTIONS' => $tz_select['tz_select'], + 'S_TZ_DATE_OPTIONS' => $tz_select['tz_dates'], 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false, 'S_REGISTRATION' => true, 'S_COPPA' => $coppa, -- cgit v1.2.1 From 5f96e5d374d1702d3d81591b7c69ede1cafebfa7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 12 Jun 2012 19:54:26 +0200 Subject: [feature/new-tz-handling] Fix timezone option when editing a user in the ACP PHPBB3-9558 --- phpBB/includes/acp/acp_users.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index b863a9ed80..949109abaf 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1639,6 +1639,7 @@ class acp_users ${'s_sort_' . $sort_option . '_dir'} .= ''; } + $tz_select = tz_select($data['tz'], true, false); $template->assign_vars(array( 'S_PREFS' => true, 'S_JABBER_DISABLED' => ($config['jab_enable'] && $user_row['user_jabber'] && @extension_loaded('xml')) ? false : true, @@ -1678,7 +1679,8 @@ class acp_users 'S_LANG_OPTIONS' => language_select($data['lang']), 'S_STYLE_OPTIONS' => style_select($data['style']), - 'S_TZ_OPTIONS' => tz_select($data['tz'], true), + 'S_TZ_OPTIONS' => $tz_select['tz_select'], + 'S_TZ_DATE_OPTIONS' => $tz_select['tz_dates'], ) ); -- cgit v1.2.1 From 2a880dafdfacb1bc56652976078a3bd3104ceaf9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:03:10 +0200 Subject: [feature/new-tz-handling] Do not use underscore as prefix for user PHPBB3-9558 --- phpBB/includes/datetime.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 15e3c8b0b7..745c3372f1 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -21,7 +21,7 @@ class phpbb_datetime extends DateTime /** * @var user User who is the context for this DateTime instance */ - protected $_user; + protected $user; /** * @var array Date formats are preprocessed by phpBB, to save constant recalculation they are cached. @@ -38,9 +38,9 @@ class phpbb_datetime extends DateTime */ public function __construct($time = 'now', DateTimeZone $timezone = null, $user = null) { - $this->_user = $user ? $user : $GLOBALS['user']; + $this->user = $user ?: $GLOBALS['user']; - $timezone = (!$timezone && $this->_user->tz instanceof DateTimeZone) ? $this->_user->tz : $timezone; + $timezone = (!$timezone && $this->user->tz instanceof DateTimeZone) ? $this->user->tz : $timezone; parent::__construct($time, $timezone); } @@ -72,10 +72,10 @@ class phpbb_datetime extends DateTime */ public function format($format = '', $force_absolute = false) { - $format = $format ? $format : $this->_user->date_format; - $format = self::_format_cache($format, $this->_user); + $format = $format ? $format : $this->user->date_format; + $format = self::_format_cache($format, $this->user); $relative = ($format['is_short'] && !$force_absolute); - $now = new self('now', $this->_user->tz, $this->_user); + $now = new self('now', $this->user->tz, $this->user); $timestamp = $this->getTimestamp(); $now_ts = $now->getTimeStamp(); @@ -88,9 +88,9 @@ class phpbb_datetime extends DateTime // and the delta not more than a minute in the past // and the delta is either greater than -5 seconds or timestamp and current time are of the same minute (they must be in the same hour already) // finally check that relative dates are supported by the language pack - if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->_user->lang['datetime']['AGO'])) + if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->user->lang['datetime']['AGO'])) { - return $this->_user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); + return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); } else { @@ -117,7 +117,7 @@ class phpbb_datetime extends DateTime if ($day !== false) { // Format using the short formatting and finally swap out the relative token placeholder with the correct value - return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->_user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); + return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang'])); } } } -- cgit v1.2.1 From 9836efc13c73ddf2382a40cb8ce5a4181d5831ca Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:03:39 +0200 Subject: [feature/new-tz-handling] Inject $user to avoid the usage of global PHPBB3-9558 --- phpBB/includes/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 9263833b4c..19e59f2ddb 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1145,7 +1145,7 @@ function tz_select($default = '', $truncate = false, $return_tzs_only = true) foreach ($unsorted_timezones as $timezone) { $tz = new DateTimeZone($timezone); - $dt = new phpbb_datetime('now', $tz); + $dt = new phpbb_datetime('now', $tz, $user); $offset = $dt->getOffset(); $current_time = $dt->format($user->lang['DATETIME_FORMAT'], true); $offset_string = phpbb_format_timezone_offset($offset); @@ -4794,7 +4794,7 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 } } - $dt = new phpbb_datetime('now', $user->tz); + $dt = new phpbb_datetime('now', $user->tz, $user); $timezone_offset = 'GMT' . phpbb_format_timezone_offset($dt->getOffset()); $timezone_name = $user->tz->getName(); if (isset($user->lang['timezones'][$timezone_name])) -- cgit v1.2.1 From fb77b6b70cb0fde351209593ea5897b2fd2e5174 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:04:49 +0200 Subject: [feature/new-tz-handling] Fix documentation in includes/datetime.php PHPBB3-9558 --- phpBB/includes/datetime.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 745c3372f1..ec2cb3f471 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -2,9 +2,8 @@ /** * * @package phpBB3 -* @version $Id$ -* @copyright (c) 2010 phpBB Group -* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 */ /** @@ -84,10 +83,12 @@ class phpbb_datetime extends DateTime if ($relative) { - // Check the delta is less than or equal to 1 hour - // and the delta not more than a minute in the past - // and the delta is either greater than -5 seconds or timestamp and current time are of the same minute (they must be in the same hour already) - // finally check that relative dates are supported by the language pack + /** + * Check the delta is less than or equal to 1 hour + * and the delta not more than a minute in the past + * and the delta is either greater than -5 seconds or timestamp and current time are of the same minute (they must be in the same hour already) + * finally check that relative dates are supported by the language pack + */ if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->user->lang['datetime']['AGO'])) { return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); -- cgit v1.2.1 From 2c4677b79ee657d2ab9f4e2592924391a7814f79 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:05:42 +0200 Subject: [feature/new-tz-handling] Remove function getTimestamp() as we require php 5.3 PHPBB3-9558 --- phpBB/includes/datetime.php | 18 ------------------ 1 file changed, 18 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index ec2cb3f471..0cb4c79a10 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -44,24 +44,6 @@ class phpbb_datetime extends DateTime parent::__construct($time, $timezone); } - /** - * Returns a UNIX timestamp representation of the date time. - * - * @internal This method is for backwards compatibility with 5.2, hence why it doesn't use our method naming standards. - * @return int UNIX timestamp - */ - public function getTimestamp() - { - static $compat; - - if (!isset($compat)) - { - $compat = !method_exists('DateTime', 'getTimestamp'); - } - - return !$compat ? parent::getTimestamp() : (int) parent::format('U'); - } - /** * Formats the current date time into the specified format * -- cgit v1.2.1 From 1716e2a2a57903464bf4a1b6843bca913c656695 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 11:28:23 +0200 Subject: [feature/new-tz-handling] Update doc blocks for the three timezone functions PHPBB3-9558 --- phpBB/includes/functions.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 19e59f2ddb..618af7ee00 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1068,6 +1068,13 @@ function style_select($default = '', $all = false) return $style_options; } +/** +* Format the timezone offset with hours and minutes +* +* @param int $tz_offset Timezone offset in seconds +* @return string Normalized offset string: -7200 => -02:00 +* 16200 => +04:30 +*/ function phpbb_format_timezone_offset($tz_offset) { $sign = ($tz_offset < 0) ? '-' : '+'; @@ -1081,9 +1088,11 @@ function phpbb_format_timezone_offset($tz_offset) return $offset_string; } -// Compares two time zone labels. -// Arranges them in increasing order by timezone offset. -// Places UTC before other timezones in the same offset. +/** +* Compares two time zone labels. +* Arranges them in increasing order by timezone offset. +* Places UTC before other timezones in the same offset. +*/ function tz_select_compare($a, $b) { $a_sign = $a[3]; @@ -1129,6 +1138,14 @@ function tz_select_compare($a, $b) /** * Pick a timezone +* +* @param string $default A timezone to select +* @param boolean $truncate Shall we truncate the options text +* @param boolean $return_tzs_only Shall we just return the options for the timezone selector, +* or also return the options for the time selector. +* +* @return string/array Returns either the options for timezone selector +* or an array, also containing the options for the time selector. */ function tz_select($default = '', $truncate = false, $return_tzs_only = true) { -- cgit v1.2.1 From 3e3489195fb4d804e65a7a5438c33ac2fe0564b6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 18 Jun 2012 14:32:09 +0200 Subject: [feature/new-tz-handling] Add a function to get the timestamp of a date The function uses the given timezone and returns the correct UNIX timestamp of it. PHPBB3-9558 --- phpBB/includes/user.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'phpBB/includes') diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index a36d837fbd..0990cc601a 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -647,6 +647,21 @@ class phpbb_user extends phpbb_session return new phpbb_datetime($time, $timezone, $this); } + /** + * Get the UNIX timestamp for a datetime in the users timezone, so we can store it in the database. + * + * @param string $format Format of the entered date/time + * @param string $time Date/time with the timezone applied + * @param DateTimeZone $timezone Timezone of the date/time, falls back to timezone of current user + * @return int Returns the unix timestamp + */ + public function get_timestamp_from_format($format, $time, DateTimeZone $timezone = null) + { + $timezone = $timezone ?: $this->tz; + $date = DateTime::createFromFormat($format, $time, $timezone); + return ($date !== false) ? $date->format('U') : false; + } + /** * Get language id currently used by the user */ -- cgit v1.2.1 From 217d9280dd48953d0572f1223c76913eec15a202 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 19 Jun 2012 19:21:59 +0200 Subject: [feature/new-tz-handling] Fix comment and validation in ACP board module PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index e018655407..59f7bf707f 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -57,7 +57,7 @@ class acp_board 'board_disable_msg' => false, 'default_lang' => array('lang' => 'DEFAULT_LANGUAGE', 'validate' => 'lang', 'type' => 'select', 'function' => 'language_select', 'params' => array('{CONFIG_VALUE}'), 'explain' => false), 'default_dateformat' => array('lang' => 'DEFAULT_DATE_FORMAT', 'validate' => 'string', 'type' => 'custom', 'method' => 'dateformat_select', 'explain' => true), - 'board_timezone' => array('lang' => 'SYSTEM_TIMEZONE', 'validate' => 'string', 'type' => 'select', 'function' => 'tz_select', 'params' => array('{CONFIG_VALUE}', 1), 'explain' => true), + 'board_timezone' => array('lang' => 'SYSTEM_TIMEZONE', 'validate' => 'timezone', 'type' => 'select', 'function' => 'tz_select', 'params' => array('{CONFIG_VALUE}', 1), 'explain' => true), 'default_style' => array('lang' => 'DEFAULT_STYLE', 'validate' => 'int', 'type' => 'select', 'function' => 'style_select', 'params' => array('{CONFIG_VALUE}', false), 'explain' => false), 'override_user_style' => array('lang' => 'OVERRIDE_STYLE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), @@ -908,7 +908,7 @@ class acp_board $old_tz = $user->tz; if (is_numeric($config['board_timezone'])) { - // Might still be numeric by chance + // Might still be numeric $config['board_timezone'] = sprintf('Etc/GMT%+d', $config['board_timezone']); } $user->tz = new DateTimeZone($config['board_timezone']); -- cgit v1.2.1 From a613caab093c3b354a7121c7989c9687d12bcdb4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 19 Jun 2012 19:24:03 +0200 Subject: [feature/new-tz-handling] Trim comment length and remove leading underscores PHPBB3-9558 --- phpBB/includes/datetime.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 0cb4c79a10..00dc1958e5 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -39,7 +39,7 @@ class phpbb_datetime extends DateTime { $this->user = $user ?: $GLOBALS['user']; - $timezone = (!$timezone && $this->user->tz instanceof DateTimeZone) ? $this->user->tz : $timezone; + $timezone = $timezone ?: $this->user->tz; parent::__construct($time, $timezone); } @@ -54,7 +54,7 @@ class phpbb_datetime extends DateTime public function format($format = '', $force_absolute = false) { $format = $format ? $format : $this->user->date_format; - $format = self::_format_cache($format, $this->user); + $format = self::format_cache($format, $this->user); $relative = ($format['is_short'] && !$force_absolute); $now = new self('now', $this->user->tz, $this->user); @@ -68,7 +68,8 @@ class phpbb_datetime extends DateTime /** * Check the delta is less than or equal to 1 hour * and the delta not more than a minute in the past - * and the delta is either greater than -5 seconds or timestamp and current time are of the same minute (they must be in the same hour already) + * and the delta is either greater than -5 seconds or timestamp + * and current time are of the same minute (they must be in the same hour already) * finally check that relative dates are supported by the language pack */ if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->user->lang['datetime']['AGO'])) @@ -125,7 +126,7 @@ class phpbb_datetime extends DateTime * @param user $user User object to use for localisation * @return array Processed date format */ - static protected function _format_cache($format, $user) + static protected function format_cache($format, $user) { $lang = $user->lang_name; -- cgit v1.2.1 From c7d32a1b2d300618278cb8178f9dfefa93804167 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 19 Jun 2012 19:26:57 +0200 Subject: [feature/new-tz-handling] Prefix function with phpbb_ PHPBB3-9558 --- phpBB/includes/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 618af7ee00..53511291b5 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1093,7 +1093,7 @@ function phpbb_format_timezone_offset($tz_offset) * Arranges them in increasing order by timezone offset. * Places UTC before other timezones in the same offset. */ -function tz_select_compare($a, $b) +function phpbb_tz_select_compare($a, $b) { $a_sign = $a[3]; $b_sign = $b[3]; @@ -1178,7 +1178,7 @@ function tz_select($default = '', $truncate = false, $return_tzs_only = true) } unset($unsorted_timezones); - uksort($timezones, 'tz_select_compare'); + uksort($timezones, 'phpbb_tz_select_compare'); } $tz_select = $tz_dates = $opt_group = ''; -- cgit v1.2.1 From a5628cbdc85a197f32bd14890938dabd23bb6ef6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 00:29:31 +0200 Subject: [feature/new-tz-handling] Fix docs in phpbb_user class PHPBB3-9558 --- phpBB/includes/user.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index 0990cc601a..df8f048c89 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -29,6 +29,10 @@ class phpbb_user extends phpbb_session var $help = array(); var $theme = array(); var $date_format; + + /** + * DateTimeZone object holding the timezone of the user + */ public $tz; var $lang_name = false; @@ -125,7 +129,7 @@ class phpbb_user extends phpbb_session if (is_numeric($this->tz)) { - // Might still be numeric by chance + // Might still be numeric $this->tz = sprintf('Etc/GMT%+d', $this->tz); } -- cgit v1.2.1 From 82e195ac683f6604aee9862233ffcb4f58af64b0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Jun 2012 14:34:35 +0200 Subject: [feature/new-tz-handling] Fix code and doc layout PHPBB3-9558 --- phpBB/includes/datetime.php | 6 ++++-- phpBB/includes/functions.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 00dc1958e5..317376431d 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -65,14 +65,16 @@ class phpbb_datetime extends DateTime if ($relative) { - /** + /* * Check the delta is less than or equal to 1 hour * and the delta not more than a minute in the past * and the delta is either greater than -5 seconds or timestamp * and current time are of the same minute (they must be in the same hour already) * finally check that relative dates are supported by the language pack */ - if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) && isset($this->user->lang['datetime']['AGO'])) + if ($delta <= 3600 && $delta > -60 && + ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60)) + && isset($this->user->lang['datetime']['AGO'])) { return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); } diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 53511291b5..98a08ea4d3 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1111,10 +1111,12 @@ function phpbb_tz_select_compare($a, $b) if ($a_name == $b_name) { return 0; - } else if ($a_name == 'UTC') + } + else if ($a_name == 'UTC') { return -1; - } else if ($b_name == 'UTC') + } + else if ($b_name == 'UTC') { return 1; } -- cgit v1.2.1 From f5bb145040a483a76fd734bb9d4025c54e7917a0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 16 Jul 2012 18:29:31 +0200 Subject: [feature/new-tz-handling] Require user argument on phpbb_datetime PHPBB3-9558 --- phpBB/includes/datetime.php | 7 +++---- phpBB/includes/functions.php | 4 ++-- phpBB/includes/user.php | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 317376431d..0d80c77115 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -35,10 +35,9 @@ class phpbb_datetime extends DateTime * @param DateTimeZone $timezone Time zone of the time. * @param user User object for context. */ - public function __construct($time = 'now', DateTimeZone $timezone = null, $user = null) + public function __construct($user, $time = 'now', DateTimeZone $timezone = null) { - $this->user = $user ?: $GLOBALS['user']; - + $this->user = $user; $timezone = $timezone ?: $this->user->tz; parent::__construct($time, $timezone); @@ -56,7 +55,7 @@ class phpbb_datetime extends DateTime $format = $format ? $format : $this->user->date_format; $format = self::format_cache($format, $this->user); $relative = ($format['is_short'] && !$force_absolute); - $now = new self('now', $this->user->tz, $this->user); + $now = new self($this->user, 'now', $this->user->tz); $timestamp = $this->getTimestamp(); $now_ts = $now->getTimeStamp(); diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 98a08ea4d3..4fc2739f33 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1164,7 +1164,7 @@ function tz_select($default = '', $truncate = false, $return_tzs_only = true) foreach ($unsorted_timezones as $timezone) { $tz = new DateTimeZone($timezone); - $dt = new phpbb_datetime('now', $tz, $user); + $dt = new phpbb_datetime($user, 'now', $tz); $offset = $dt->getOffset(); $current_time = $dt->format($user->lang['DATETIME_FORMAT'], true); $offset_string = phpbb_format_timezone_offset($offset); @@ -4813,7 +4813,7 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 } } - $dt = new phpbb_datetime('now', $user->tz, $user); + $dt = new phpbb_datetime($user, 'now', $user->tz); $timezone_offset = 'GMT' . phpbb_format_timezone_offset($dt->getOffset()); $timezone_name = $user->tz->getName(); if (isset($user->lang['timezones'][$timezone_name])) diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index df8f048c89..cc50d2b98e 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -631,7 +631,7 @@ class phpbb_user extends phpbb_session $utc = new DateTimeZone('UTC'); } - $time = new phpbb_datetime("@$gmepoch", $utc, $this); + $time = new phpbb_datetime($this, "@$gmepoch", $utc); $time->setTimezone($this->tz); return $time->format($format, $forcedate); @@ -648,7 +648,7 @@ class phpbb_user extends phpbb_session public function create_datetime($time = 'now', DateTimeZone $timezone = null) { $timezone = $timezone ?: $this->tz; - return new phpbb_datetime($time, $timezone, $this); + return new phpbb_datetime($this, $time, $timezone); } /** -- cgit v1.2.1 From b61ac43abecafdd4e9597022fe8b09323854aac9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 16 Jul 2012 18:41:30 +0200 Subject: [feature/new-tz-handling] Allow phpbb prefix for user data validation functions PHPBB3-9558 --- phpBB/includes/functions_user.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index f235b2be55..27f4b60189 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -1248,10 +1248,21 @@ function validate_data($data, $val_ary) $function = array_shift($validate); array_unshift($validate, $data[$var]); - if ($result = call_user_func_array('validate_' . $function, $validate)) + if (function_exists('phpbb_validate_' . $function)) { - // Since errors are checked later for their language file existence, we need to make sure custom errors are not adjusted. - $error[] = (empty($user->lang[$result . '_' . strtoupper($var)])) ? $result : $result . '_' . strtoupper($var); + if ($result = call_user_func_array('phpbb_validate_' . $function, $validate)) + { + // Since errors are checked later for their language file existence, we need to make sure custom errors are not adjusted. + $error[] = (empty($user->lang[$result . '_' . strtoupper($var)])) ? $result : $result . '_' . strtoupper($var); + } + } + else + { + if ($result = call_user_func_array('validate_' . $function, $validate)) + { + // Since errors are checked later for their language file existence, we need to make sure custom errors are not adjusted. + $error[] = (empty($user->lang[$result . '_' . strtoupper($var)])) ? $result : $result . '_' . strtoupper($var); + } } } } @@ -1407,7 +1418,7 @@ function validate_language_iso_name($lang_iso) * a string which will be used as the error message * (with the variable name appended) */ -function validate_timezone($timezone) +function phpbb_validate_timezone($timezone) { return (in_array($timezone, DateTimeZone::listIdentifiers())) ? false : 'TIMEZONE_INVALID'; } -- cgit v1.2.1 From 48d3c68290b96c7d2a0e875ec3338f067a529b22 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 16 Jul 2012 18:44:13 +0200 Subject: [feature/new-tz-handling] Use tmp variable for user timezone PHPBB3-9558 --- phpBB/includes/user.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index cc50d2b98e..7dfb375a9b 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -82,13 +82,13 @@ class phpbb_user extends phpbb_session $this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']); $this->date_format = $this->data['user_dateformat']; - $this->tz = $this->data['user_timezone']; + $user_timezone = $this->data['user_timezone']; } else { $this->lang_name = basename($config['default_lang']); $this->date_format = $config['default_dateformat']; - $this->tz = $config['board_timezone']; + $user_timezone = $config['board_timezone']; /** * If a guest user is surfing, we try to guess his/her language first by obtaining the browser language @@ -127,13 +127,13 @@ class phpbb_user extends phpbb_session */ } - if (is_numeric($this->tz)) + if (is_numeric($user_timezone)) { // Might still be numeric - $this->tz = sprintf('Etc/GMT%+d', $this->tz); + $user_timezone = sprintf('Etc/GMT%+d', $user_timezone); } - $this->tz = new DateTimeZone($this->tz); + $this->tz = new DateTimeZone($user_timezone); // We include common language file here to not load it every time a custom language file is included $lang = &$this->lang; -- cgit v1.2.1 From 3637cd395e39c1fa5b7279222abe1da5d2abcd00 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 17 Jul 2012 16:09:05 +0200 Subject: [feature/new-tz-handling] Properly name new timezone selection function Marked the old one as deprecated and made it using the new function. PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 12 +++++++++++- phpBB/includes/acp/acp_users.php | 6 +++--- phpBB/includes/functions.php | 28 ++++++++++++++++++---------- phpBB/includes/ucp/ucp_prefs.php | 6 +++--- phpBB/includes/ucp/ucp_register.php | 6 +++--- 5 files changed, 38 insertions(+), 20 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 59f7bf707f..159435c64c 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -57,7 +57,7 @@ class acp_board 'board_disable_msg' => false, 'default_lang' => array('lang' => 'DEFAULT_LANGUAGE', 'validate' => 'lang', 'type' => 'select', 'function' => 'language_select', 'params' => array('{CONFIG_VALUE}'), 'explain' => false), 'default_dateformat' => array('lang' => 'DEFAULT_DATE_FORMAT', 'validate' => 'string', 'type' => 'custom', 'method' => 'dateformat_select', 'explain' => true), - 'board_timezone' => array('lang' => 'SYSTEM_TIMEZONE', 'validate' => 'timezone', 'type' => 'select', 'function' => 'tz_select', 'params' => array('{CONFIG_VALUE}', 1), 'explain' => true), + 'board_timezone' => array('lang' => 'SYSTEM_TIMEZONE', 'validate' => 'timezone', 'type' => 'custom', 'method' => 'timezone_select', 'explain' => true), 'default_style' => array('lang' => 'DEFAULT_STYLE', 'validate' => 'int', 'type' => 'select', 'function' => 'style_select', 'params' => array('{CONFIG_VALUE}', false), 'explain' => false), 'override_user_style' => array('lang' => 'OVERRIDE_STYLE', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), @@ -896,6 +896,16 @@ class acp_board '

'; } + /** + * Select guest timezone + */ + function timezone_select($value, $key) + { + $timezone_select = phpbb_timezone_select($value, true); + $timezone_select['tz_select']; + + return ''; + } /** * Select default dateformat diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index 949109abaf..a228e07c22 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1639,7 +1639,7 @@ class acp_users ${'s_sort_' . $sort_option . '_dir'} .= ''; } - $tz_select = tz_select($data['tz'], true, false); + $timezone_selects = phpbb_timezone_select($data['tz'], true, false); $template->assign_vars(array( 'S_PREFS' => true, 'S_JABBER_DISABLED' => ($config['jab_enable'] && $user_row['user_jabber'] && @extension_loaded('xml')) ? false : true, @@ -1679,8 +1679,8 @@ class acp_users 'S_LANG_OPTIONS' => language_select($data['lang']), 'S_STYLE_OPTIONS' => style_select($data['style']), - 'S_TZ_OPTIONS' => $tz_select['tz_select'], - 'S_TZ_DATE_OPTIONS' => $tz_select['tz_dates'], + 'S_TZ_OPTIONS' => $timezone_selects['tz_select'], + 'S_TZ_DATE_OPTIONS' => $timezone_selects['tz_dates'], ) ); diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 4fc2739f33..3533a4ca00 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1143,13 +1143,26 @@ function phpbb_tz_select_compare($a, $b) * * @param string $default A timezone to select * @param boolean $truncate Shall we truncate the options text -* @param boolean $return_tzs_only Shall we just return the options for the timezone selector, -* or also return the options for the time selector. * -* @return string/array Returns either the options for timezone selector -* or an array, also containing the options for the time selector. +* @return string Returns the options for timezone selector only +* +* @deprecated +*/ +function tz_select($default = '', $truncate = false) +{ + $timezone_select = phpbb_timezone_select($default, $truncate); + return $timezone_select['tz_select']; +} + +/** +* Options to pick a timezone and date/time +* +* @param string $default A timezone to select +* @param boolean $truncate Shall we truncate the options text +* +* @return array Returns an array, also containing the options for the time selector. */ -function tz_select($default = '', $truncate = false, $return_tzs_only = true) +function phpbb_timezone_select($default = '', $truncate = false) { global $user; @@ -1220,11 +1233,6 @@ function tz_select($default = '', $truncate = false, $return_tzs_only = true) } $tz_select .= ''; - if ($return_tzs_only) - { - return $tz_select; - } - return array( 'tz_select' => $tz_select, 'tz_dates' => $tz_dates, diff --git a/phpBB/includes/ucp/ucp_prefs.php b/phpBB/includes/ucp/ucp_prefs.php index 4239afc96e..f63758c52d 100644 --- a/phpBB/includes/ucp/ucp_prefs.php +++ b/phpBB/includes/ucp/ucp_prefs.php @@ -131,7 +131,7 @@ class ucp_prefs } $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . ''; - $tz_select = tz_select($data['tz'], true, false); + $timezone_selects = phpbb_timezone_select($data['tz'], true, false); $template->assign_vars(array( 'ERROR' => (sizeof($error)) ? implode('
', $error) : '', @@ -154,8 +154,8 @@ class ucp_prefs 'S_LANG_OPTIONS' => language_select($data['lang']), 'S_STYLE_OPTIONS' => ($config['override_user_style']) ? '' : style_select($data['style']), - 'S_TZ_OPTIONS' => $tz_select['tz_select'], - 'S_TZ_DATE_OPTIONS' => $tz_select['tz_dates'], + 'S_TZ_OPTIONS' => $timezone_selects['tz_select'], + 'S_TZ_DATE_OPTIONS' => $timezone_selects['tz_dates'], 'S_CAN_HIDE_ONLINE' => ($auth->acl_get('u_hideonline')) ? true : false, 'S_SELECT_NOTIFY' => ($config['jab_enable'] && $user->data['user_jabber'] && @extension_loaded('xml')) ? true : false) ); diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index 804bd2e0e2..705000d7a8 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -442,7 +442,7 @@ class ucp_register break; } - $tz_select = tz_select($data['tz'], true, false); + $timezone_selects = phpbb_timezone_select($data['tz'], true, false); $template->assign_vars(array( 'ERROR' => (sizeof($error)) ? implode('
', $error) : '', 'USERNAME' => $data['username'], @@ -455,8 +455,8 @@ class ucp_register 'L_PASSWORD_EXPLAIN' => $user->lang($config['pass_complex'] . '_EXPLAIN', $user->lang('CHARACTERS', (int) $config['min_pass_chars']), $user->lang('CHARACTERS', (int) $config['max_pass_chars'])), 'S_LANG_OPTIONS' => language_select($data['lang']), - 'S_TZ_OPTIONS' => $tz_select['tz_select'], - 'S_TZ_DATE_OPTIONS' => $tz_select['tz_dates'], + 'S_TZ_OPTIONS' => $timezone_selects['tz_select'], + 'S_TZ_DATE_OPTIONS' => $timezone_selects['tz_dates'], 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false, 'S_REGISTRATION' => true, 'S_COPPA' => $coppa, -- cgit v1.2.1 From 515e1662a900526342b595692d73372f4fb7bbf9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Jul 2012 14:56:42 +0200 Subject: [feature/new-tz-handling] Remove "timezone might be numeric" As we updated all of the used values, there really shouldn't be one anymore. PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 5 ----- phpBB/includes/user.php | 6 ------ 2 files changed, 11 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 159435c64c..47a08b0bab 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -916,11 +916,6 @@ class acp_board // Let the format_date function operate with the acp values $old_tz = $user->tz; - if (is_numeric($config['board_timezone'])) - { - // Might still be numeric - $config['board_timezone'] = sprintf('Etc/GMT%+d', $config['board_timezone']); - } $user->tz = new DateTimeZone($config['board_timezone']); $dateformat_options = ''; diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index fb6c1552e8..c3bdc71774 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -127,12 +127,6 @@ class phpbb_user extends phpbb_session */ } - if (is_numeric($user_timezone)) - { - // Might still be numeric - $user_timezone = sprintf('Etc/GMT%+d', $user_timezone); - } - $this->tz = new DateTimeZone($user_timezone); // We include common language file here to not load it every time a custom language file is included -- cgit v1.2.1 From a71e60cdbd3e47c62a565dd777a28aec6832fb88 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Jul 2012 16:52:52 +0200 Subject: [feature/new-tz-handling] Rename $user->tz back to $user->timezone PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 6 +++--- phpBB/includes/datetime.php | 4 ++-- phpBB/includes/functions.php | 4 ++-- phpBB/includes/functions_user.php | 2 +- phpBB/includes/user.php | 10 +++++----- 5 files changed, 13 insertions(+), 13 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 47a08b0bab..4af9d52e44 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -915,8 +915,8 @@ class acp_board global $user, $config; // Let the format_date function operate with the acp values - $old_tz = $user->tz; - $user->tz = new DateTimeZone($config['board_timezone']); + $old_tz = $user->timezone; + $user->timezone = new DateTimeZone($config['board_timezone']); $dateformat_options = ''; @@ -935,7 +935,7 @@ class acp_board $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . ''; // Reset users date options - $user->tz = $old_tz; + $user->timezone = $old_tz; return " "; diff --git a/phpBB/includes/datetime.php b/phpBB/includes/datetime.php index 0d80c77115..b3462ddf67 100644 --- a/phpBB/includes/datetime.php +++ b/phpBB/includes/datetime.php @@ -38,7 +38,7 @@ class phpbb_datetime extends DateTime public function __construct($user, $time = 'now', DateTimeZone $timezone = null) { $this->user = $user; - $timezone = $timezone ?: $this->user->tz; + $timezone = $timezone ?: $this->user->timezone; parent::__construct($time, $timezone); } @@ -55,7 +55,7 @@ class phpbb_datetime extends DateTime $format = $format ? $format : $this->user->date_format; $format = self::format_cache($format, $this->user); $relative = ($format['is_short'] && !$force_absolute); - $now = new self($this->user, 'now', $this->user->tz); + $now = new self($this->user, 'now', $this->user->timezone); $timestamp = $this->getTimestamp(); $now_ts = $now->getTimeStamp(); diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 22e456c7de..b5d164f7a3 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4821,9 +4821,9 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 } } - $dt = new phpbb_datetime($user, 'now', $user->tz); + $dt = new phpbb_datetime($user, 'now', $user->timezone); $timezone_offset = 'GMT' . phpbb_format_timezone_offset($dt->getOffset()); - $timezone_name = $user->tz->getName(); + $timezone_name = $user->timezone->getName(); if (isset($user->lang['timezones'][$timezone_name])) { $timezone_name = $user->lang['timezones'][$timezone_name]; diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 27f4b60189..9bc72cbaa6 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -679,7 +679,7 @@ function user_ban($mode, $ban, $ban_len, $ban_len_other, $ban_exclude, $ban_reas $ban_end = max($current_time, $user->create_datetime() ->setDate((int) $ban_other[0], (int) $ban_other[1], (int) $ban_other[2]) ->setTime(0, 0, 0) - ->getTimestamp() + $user->tz->getOffset(new DateTime('UTC'))); + ->getTimestamp() + $user->timezone->getOffset(new DateTime('UTC'))); } else { diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index c3bdc71774..48c328214d 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -33,7 +33,7 @@ class phpbb_user extends phpbb_session /** * DateTimeZone object holding the timezone of the user */ - public $tz; + public $timezone; var $lang_name = false; var $lang_id = false; @@ -127,7 +127,7 @@ class phpbb_user extends phpbb_session */ } - $this->tz = new DateTimeZone($user_timezone); + $this->timezone = new DateTimeZone($user_timezone); // We include common language file here to not load it every time a custom language file is included $lang = &$this->lang; @@ -623,7 +623,7 @@ class phpbb_user extends phpbb_session } $time = new phpbb_datetime($this, "@$gmepoch", $utc); - $time->setTimezone($this->tz); + $time->setTimezone($this->timezone); return $time->format($format, $forcedate); } @@ -638,7 +638,7 @@ class phpbb_user extends phpbb_session */ public function create_datetime($time = 'now', DateTimeZone $timezone = null) { - $timezone = $timezone ?: $this->tz; + $timezone = $timezone ?: $this->timezone; return new phpbb_datetime($this, $time, $timezone); } @@ -652,7 +652,7 @@ class phpbb_user extends phpbb_session */ public function get_timestamp_from_format($format, $time, DateTimeZone $timezone = null) { - $timezone = $timezone ?: $this->tz; + $timezone = $timezone ?: $this->timezone; $date = DateTime::createFromFormat($format, $time, $timezone); return ($date !== false) ? $date->format('U') : false; } -- cgit v1.2.1 From 248a52be2af2d450be0447b7a241727cb3d61844 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Jul 2012 16:55:14 +0200 Subject: [feature/new-tz-handling] Delete old variable which is not used anymore PHPBB3-9558 --- phpBB/includes/functions.php | 3 --- 1 file changed, 3 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index b5d164f7a3..c6abcb27ef 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -4796,9 +4796,6 @@ function page_header($page_title = '', $display_online_list = true, $item_id = 0 $board_url = generate_board_url() . '/'; $web_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? $board_url : $phpbb_root_path; - // Which timezone? - $tz = ($user->data['user_id'] != ANONYMOUS) ? strval(doubleval($user->data['user_timezone'])) : strval(doubleval($config['board_timezone'])); - // Send a proper content-language to the output $user_lang = $user->lang['USER_LANG']; if (strpos($user_lang, '-x-') !== false) -- cgit v1.2.1 From 14a07b11936bc061b5c82daa6387eb4645974fac Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Jul 2012 17:18:10 +0200 Subject: [feature/new-tz-handling] Move update helper function to new class PHPBB3-9558 --- phpBB/includes/update_helpers.php | 112 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 phpBB/includes/update_helpers.php (limited to 'phpBB/includes') diff --git a/phpBB/includes/update_helpers.php b/phpBB/includes/update_helpers.php new file mode 100644 index 0000000000..8e8e9670b0 --- /dev/null +++ b/phpBB/includes/update_helpers.php @@ -0,0 +1,112 @@ + Date: Wed, 18 Jul 2012 18:59:25 +0200 Subject: [feature/new-tz-handling] Remove additional marking of selected items PHPBB3-9558 --- phpBB/includes/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index c6abcb27ef..6a4a67f9d7 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1207,7 +1207,7 @@ function phpbb_timezone_select($default = '', $truncate = false) $opt_group = $timezone['offest']; $selected = ($default_offset == $timezone['offest']) ? ' selected="selected"' : ''; - $tz_dates .= ''; + $tz_dates .= ''; } if (isset($user->lang['timezones'][$timezone['tz']])) @@ -1229,7 +1229,7 @@ function phpbb_timezone_select($default = '', $truncate = false) } $selected = ($timezone['tz'] === $default) ? ' selected="selected"' : ''; - $tz_select .= ''; + $tz_select .= ''; } $tz_select .= ''; -- cgit v1.2.1 From d099ef8cade0194fa8056439d489b159d5890f29 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 19 Jul 2012 10:34:21 +0200 Subject: [feature/new-tz-handling] Display suggestion when a different value is selected PHPBB3-9558 --- phpBB/includes/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 6a4a67f9d7..c6abcb27ef 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1207,7 +1207,7 @@ function phpbb_timezone_select($default = '', $truncate = false) $opt_group = $timezone['offest']; $selected = ($default_offset == $timezone['offest']) ? ' selected="selected"' : ''; - $tz_dates .= ''; + $tz_dates .= ''; } if (isset($user->lang['timezones'][$timezone['tz']])) @@ -1229,7 +1229,7 @@ function phpbb_timezone_select($default = '', $truncate = false) } $selected = ($timezone['tz'] === $default) ? ' selected="selected"' : ''; - $tz_select .= ''; + $tz_select .= ''; } $tz_select .= ''; -- cgit v1.2.1 From 6de222065e737bdab4ecdd010773fb70c415fb3b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 19 Jul 2012 14:36:20 +0200 Subject: [feature/new-tz-handling] Add previous selected value to validation if valid We also add the selected timezone if we can create an object with it. DateTimeZone::listIdentifiers seems to not add all identifiers to the list, because some are only kept for backward compatible reasons. If the user has a deprecated value, we add it here, so it can still be kept. Once the user changed his value, there is no way back to deprecated values. PHPBB3-9558 --- phpBB/includes/functions.php | 42 +++++++++++++++++++++++++++++++++++---- phpBB/includes/functions_user.php | 2 +- 2 files changed, 39 insertions(+), 5 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index c6abcb27ef..98465ca462 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1138,6 +1138,40 @@ function phpbb_tz_select_compare($a, $b) } } +/** +* Return list of timezone identifiers +* We also add the selected timezone if we can create an object with it. +* DateTimeZone::listIdentifiers seems to not add all identifiers to the list, +* because some are only kept for backward compatible reasons. If the user has +* a deprecated value, we add it here, so it can still be kept. Once the user +* changed his value, there is no way back to deprecated values. +* +* @param string $selected_timezone Additional timezone that shall +* be added to the list of identiers +* @return array DateTimeZone::listIdentifiers and additional +* selected_timezone if it is a valid timezone. +*/ +function phpbb_get_timezone_identifiers($selected_timezone) +{ + $timezones = DateTimeZone::listIdentifiers(); + + if (!in_array($selected_timezone, $timezones)) + { + try + { + // Add valid timezones that are currently selected but not returned + // by DateTimeZone::listIdentifiers + $validate_timezone = new DateTimeZone($selected_timezone); + $timezones[] = $selected_timezone; + } + catch (Exception $e) + { + } + } + + return $timezones; +} + /** * Pick a timezone * @@ -1171,9 +1205,9 @@ function phpbb_timezone_select($default = '', $truncate = false) $default_offset = ''; if (!isset($timezones)) { - $unsorted_timezones = DateTimeZone::listIdentifiers(); - $timezones = array(); + $unsorted_timezones = phpbb_get_timezone_identifiers($default); + $timezones = array(); foreach ($unsorted_timezones as $timezone) { $tz = new DateTimeZone($timezone); @@ -1207,7 +1241,7 @@ function phpbb_timezone_select($default = '', $truncate = false) $opt_group = $timezone['offest']; $selected = ($default_offset == $timezone['offest']) ? ' selected="selected"' : ''; - $tz_dates .= ''; + $tz_dates .= ''; } if (isset($user->lang['timezones'][$timezone['tz']])) @@ -1229,7 +1263,7 @@ function phpbb_timezone_select($default = '', $truncate = false) } $selected = ($timezone['tz'] === $default) ? ' selected="selected"' : ''; - $tz_select .= ''; + $tz_select .= ''; } $tz_select .= ''; diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 9bc72cbaa6..6e658b4ef4 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -1420,7 +1420,7 @@ function validate_language_iso_name($lang_iso) */ function phpbb_validate_timezone($timezone) { - return (in_array($timezone, DateTimeZone::listIdentifiers())) ? false : 'TIMEZONE_INVALID'; + return (in_array($timezone, phpbb_get_timezone_identifiers($timezone))) ? false : 'TIMEZONE_INVALID'; } /** -- cgit v1.2.1 From 196e6343702e7d769d1b1668fd29682ad3bade4c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 19 Jul 2012 15:46:12 +0200 Subject: [feature/new-tz-handling] Fall back to UTC, if the timezone is invalid This should avoid problems, when the board files are updated but database isn't. PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 9 ++++++++- phpBB/includes/user.php | 10 +++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 4af9d52e44..937d9e926f 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -916,7 +916,14 @@ class acp_board // Let the format_date function operate with the acp values $old_tz = $user->timezone; - $user->timezone = new DateTimeZone($config['board_timezone']); + try + { + $user->timezone = new DateTimeZone($config['board_timezone']); + } + catch (Exception $e) + { + // If the board timezone is invalid, we just use the users timezone. + } $dateformat_options = ''; diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index 48c328214d..fcbfaaddfa 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -127,7 +127,15 @@ class phpbb_user extends phpbb_session */ } - $this->timezone = new DateTimeZone($user_timezone); + try + { + $this->timezone = new DateTimeZone($user_timezone); + } + catch (Exception $e) + { + // If the timezone the user has selected is invalid, we fall back to UTC. + $this->timezone = new DateTimeZone('UTC'); + } // We include common language file here to not load it every time a custom language file is included $lang = &$this->lang; -- cgit v1.2.1 From 7df1c84447903ecc97a8418339ec6933bdfc9035 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 19 Jul 2012 17:41:27 +0200 Subject: [feature/new-tz-handling] Don't use global user but make it a parameter PHPBB3-9558 --- phpBB/includes/acp/acp_board.php | 4 +++- phpBB/includes/acp/acp_users.php | 2 +- phpBB/includes/functions.php | 9 +++++---- phpBB/includes/ucp/ucp_prefs.php | 2 +- phpBB/includes/ucp/ucp_register.php | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) (limited to 'phpBB/includes') diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 937d9e926f..575d05933f 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -901,7 +901,9 @@ class acp_board */ function timezone_select($value, $key) { - $timezone_select = phpbb_timezone_select($value, true); + global $user; + + $timezone_select = phpbb_timezone_select($user, $value, true); $timezone_select['tz_select']; return ''; diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index a228e07c22..3f05f216d9 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1639,7 +1639,7 @@ class acp_users ${'s_sort_' . $sort_option . '_dir'} .= ''; } - $timezone_selects = phpbb_timezone_select($data['tz'], true, false); + $timezone_selects = phpbb_timezone_select($user, $data['tz'], true); $template->assign_vars(array( 'S_PREFS' => true, 'S_JABBER_DISABLED' => ($config['jab_enable'] && $user_row['user_jabber'] && @extension_loaded('xml')) ? false : true, diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 98465ca462..0b71ea3484 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1184,22 +1184,23 @@ function phpbb_get_timezone_identifiers($selected_timezone) */ function tz_select($default = '', $truncate = false) { - $timezone_select = phpbb_timezone_select($default, $truncate); + global $user; + + $timezone_select = phpbb_timezone_select($user, $default, $truncate); return $timezone_select['tz_select']; } /** * Options to pick a timezone and date/time * +* @param phpbb_user $user Object of the current user * @param string $default A timezone to select * @param boolean $truncate Shall we truncate the options text * * @return array Returns an array, also containing the options for the time selector. */ -function phpbb_timezone_select($default = '', $truncate = false) +function phpbb_timezone_select($user, $default = '', $truncate = false) { - global $user; - static $timezones; $default_offset = ''; diff --git a/phpBB/includes/ucp/ucp_prefs.php b/phpBB/includes/ucp/ucp_prefs.php index f63758c52d..2228bc7931 100644 --- a/phpBB/includes/ucp/ucp_prefs.php +++ b/phpBB/includes/ucp/ucp_prefs.php @@ -131,7 +131,7 @@ class ucp_prefs } $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . ''; - $timezone_selects = phpbb_timezone_select($data['tz'], true, false); + $timezone_selects = phpbb_timezone_select($user, $data['tz'], true); $template->assign_vars(array( 'ERROR' => (sizeof($error)) ? implode('
', $error) : '', diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index 705000d7a8..6ce53a79ab 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -442,7 +442,7 @@ class ucp_register break; } - $timezone_selects = phpbb_timezone_select($data['tz'], true, false); + $timezone_selects = phpbb_timezone_select($user, $data['tz'], true); $template->assign_vars(array( 'ERROR' => (sizeof($error)) ? implode('
', $error) : '', 'USERNAME' => $data['username'], -- cgit v1.2.1