getkirby / kirby

Kirby's core application folder
https://getkirby.com
Other
1.31k stars 168 forks source link

[4.1.0] DateTest unit test fails when PHP timezone not UTC #6260

Closed SeriousKen closed 7 months ago

SeriousKen commented 9 months ago

Description

Unit test DateTest::testRound fails when default_timezone is not UTC.

2) Kirby\Toolkit\DateTest::testRound with data set "1Y: ceil sub" ('year', 1, '2020-09-29 16:05:15', '2021-01-01 00:00:00')
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'2021-01-01 00:00:00'
+'2020-12-31 23:00:00'

To reproduce

  1. Clone this repo
  2. Set default.timezone = Europe/London in php.ini
  3. Run phpunit --filter DateTest
  4. See error

    • Device: Dell Latitude Laptop
    • OS: Windows 11 with WSL 2 running Ubuntu 20.04
SeriousKen commented 9 months ago

This is fixed by changing the floor method in the Date class to the following:

public function floor(string $unit): static
{
    static::validateUnit($unit);

    $formats = [
        'year'   => 'Y-01-01',
        'month'  => 'Y-m-01',
        'day'    => 'Y-m-d',
        'hour'   => 'Y-m-d H:00:00',
        'minute' => 'Y-m-d H:i:00',
        'second' => 'Y-m-d H:i:s'
    ];

    $flooredDate = $this->format($formats[$unit]);
    $this->set($flooredDate);
    return $this;
}

This passes all current tests and it doesn't matter what the default timezone is set to in php.ini, however, I think there should be some new test cases added covering different timezones to prevent regression.

distantnative commented 7 months ago

PR: https://github.com/getkirby/kirby/pull/6264