From db352c17f8ce7e0abb06d6c71755ae2d9fd4c58d Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Jun 2011 02:19:51 +0200 Subject: [PATCH 1/3] [ticket/10243] Adding wrapper function for getdate() for UTC timestamps. PHPBB3-10243 --- phpBB/includes/functions.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index b1c1c14d0c..86eab4666f 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -265,6 +265,27 @@ function phpbb_mt_rand($min, $max) return ($min > $max) ? mt_rand($max, $min) : mt_rand($min, $max); } +/** +* Wrapper for getdate() which returns the equivalent array for UTC timestamps. +* +* @param int $time Unix timestamp (optional) +* +* @return array Returns an associative array of information related to the timestamp. +* See http://www.php.net/manual/en/function.getdate.php +*/ +function phpbb_gmgetdate($time = false) +{ + if ($time === false) + { + $time = time(); + } + + // getdate() interprets timestamps in local time. + // What follows uses the fact that getdate() and + // date('Z') balance each other out. + return getdate($time - date('Z')); +} + /** * Return formatted string for filesizes * From 91fd6df4308583c570dd7710d80a142a84c106aa Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 13 Jul 2011 00:01:37 +0200 Subject: [PATCH 2/3] [ticket/10243] Call phpbb_gmgetdate() from various places. PHPBB3-10243 --- phpBB/index.php | 2 +- phpBB/memberlist.php | 2 +- phpBB/viewtopic.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/index.php b/phpBB/index.php index cc83641acd..3b58646af0 100644 --- a/phpBB/index.php +++ b/phpBB/index.php @@ -83,7 +83,7 @@ $legend = implode(', ', $legend); $birthday_list = ''; if ($config['load_birthdays'] && $config['allow_birthdays']) { - $now = getdate(time() + $user->timezone + $user->dst - date('Z')); + $now = phpbb_gmgetdate(time() + $user->timezone + $user->dst); $sql = 'SELECT u.user_id, u.username, u.user_colour, u.user_birthday FROM ' . USERS_TABLE . ' u LEFT JOIN ' . BANLIST_TABLE . " b ON (u.user_id = b.ban_userid) diff --git a/phpBB/memberlist.php b/phpBB/memberlist.php index 589877305f..b3800ca0dd 100644 --- a/phpBB/memberlist.php +++ b/phpBB/memberlist.php @@ -1667,7 +1667,7 @@ function show_profile($data, $user_notes_enabled = false, $warn_user_enabled = f if ($bday_year) { - $now = getdate(time() + $user->timezone + $user->dst - date('Z')); + $now = phpbb_gmgetdate(time() + $user->timezone + $user->dst); $diff = $now['mon'] - $bday_month; if ($diff == 0) diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php index 08e82591de..3f73fe356a 100644 --- a/phpBB/viewtopic.php +++ b/phpBB/viewtopic.php @@ -995,7 +995,7 @@ $sql = $db->sql_build_query('SELECT', array( $result = $db->sql_query($sql); -$now = getdate(time() + $user->timezone + $user->dst - date('Z')); +$now = phpbb_gmgetdate(time() + $user->timezone + $user->dst); // Posts are stored in the $rowset array while $attach_list, $user_cache // and the global bbcode_bitfield are built From 0e85704d75c2edefdbdfb9d5542ab366744e65fb Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 13 Jul 2011 00:44:10 +0200 Subject: [PATCH 3/3] [ticket/10243] Adding a few unit tests for phpbb_gmgetdate(). PHPBB3-10243 --- tests/wrapper/gmgetdate_test.php | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/wrapper/gmgetdate_test.php diff --git a/tests/wrapper/gmgetdate_test.php b/tests/wrapper/gmgetdate_test.php new file mode 100644 index 0000000000..0b4c3378a9 --- /dev/null +++ b/tests/wrapper/gmgetdate_test.php @@ -0,0 +1,49 @@ +run_gmgetdate_assertion(); + $this->run_test_with_timezone('UTC'); + $this->run_test_with_timezone('Europe/Berlin'); + $this->run_test_with_timezone('America/Los_Angeles'); + $this->run_test_with_timezone('Antarctica/South_Pole'); + } + + protected function run_test_with_timezone($timezone_identifier) + { + $current_timezone = date_default_timezone_get(); + + date_default_timezone_set($timezone_identifier); + $this->run_gmgetdate_assertion(); + date_default_timezone_set($current_timezone); + } + + protected function run_gmgetdate_assertion() + { + $expected = time(); + + $date_array = phpbb_gmgetdate($expected); + + $actual = gmmktime( + $date_array['hours'], + $date_array['minutes'], + $date_array['seconds'], + $date_array['mon'], + $date_array['mday'], + $date_array['year'] + ); + + $this->assertEquals($expected, $actual); + } +}