Skip to content

Commit

Permalink
Merge pull request phpbb#6252 from rxu/ticket/16823
Browse files Browse the repository at this point in the history
[ticket/16823] Make datetime class correctly handle edge cases
  • Loading branch information
CHItA authored Jul 31, 2021
2 parents a380c78 + 2e29bb4 commit 90fccff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 4 additions & 4 deletions phpBB/phpbb/datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ public function format($format = '', $force_absolute = false)

$midnight = $midnight->getTimestamp();

if ($timestamp <= $midnight + 2 * 86400)
if ($timestamp < $midnight + 2 * 86400)
{
$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';
}
Expand Down
15 changes: 14 additions & 1 deletion tests/datetime/from_format_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function test_from_format($timezone, $format, $expected)
$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,
Expand Down Expand Up @@ -103,6 +102,20 @@ public function relative_format_date_data()
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',
),
);
}

Expand Down

0 comments on commit 90fccff

Please sign in to comment.