From e1fc0a90598c714bdb12e6edb3a5281da5300ad1 Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 20 Jul 2021 20:35:04 +0700 Subject: [PATCH 1/2] [ticket/16823] Make datetime class correctly handle edge cases PHPBB3-16823 --- phpBB/phpbb/datetime.php | 6 +++--- tests/datetime/from_format_test.php | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/datetime.php b/phpBB/phpbb/datetime.php index 4b799b6219..7384eeabbd 100644 --- a/phpBB/phpbb/datetime.php +++ b/phpBB/phpbb/datetime.php @@ -101,15 +101,15 @@ class datetime extends \DateTime { $day = false; - if ($timestamp > $midnight + 86400) + if ($timestamp >= $midnight + 86400) { $day = 'TOMORROW'; } - else if ($timestamp > $midnight) + else if ($timestamp >= $midnight) { $day = 'TODAY'; } - else if ($timestamp > $midnight - 86400) + else if ($timestamp >= $midnight - 86400) { $day = 'YESTERDAY'; } diff --git a/tests/datetime/from_format_test.php b/tests/datetime/from_format_test.php index d95bacdbce..2fe671bbec 100644 --- a/tests/datetime/from_format_test.php +++ b/tests/datetime/from_format_test.php @@ -58,7 +58,6 @@ class phpbb_datetime_from_format_test extends phpbb_test_case $this->assertEquals($expected, $user->format_date($timestamp, $format, true)); } - public function relative_format_date_data() { // If the current time is too close to the testing time, @@ -103,6 +102,20 @@ class phpbb_datetime_from_format_test extends phpbb_test_case gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time, false, gmdate('Y-m-d', time() - 2 * 86400) . ' ' . $testing_time, ), + + // Test edge cases: Yesterday 00:00, Today 00:00, Tomorrow 00:00 + array( + gmdate('Y-m-d', strtotime('yesterday')) . ' 00:00', false, + 'Yesterday 00:00', + ), + array( + gmdate('Y-m-d', strtotime('today')) . ' 00:00', false, + 'Today 00:00', + ), + array( + gmdate('Y-m-d', strtotime('tomorrow')) . ' 00:00', false, + 'Tomorrow 00:00', + ), ); } From 2e29bb48fa207bdddcb3a441e1744b4c663c712b Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 20 Jul 2021 20:59:28 +0700 Subject: [PATCH 2/2] [ticket/16823] Adjust wrapper condition PHPBB3-16823 --- phpBB/phpbb/datetime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/datetime.php b/phpBB/phpbb/datetime.php index 7384eeabbd..e171ac2dd1 100644 --- a/phpBB/phpbb/datetime.php +++ b/phpBB/phpbb/datetime.php @@ -97,7 +97,7 @@ class datetime extends \DateTime $midnight = $midnight->getTimestamp(); - if ($timestamp <= $midnight + 2 * 86400) + if ($timestamp < $midnight + 2 * 86400) { $day = false;