Problem/Motivation
When the clock pointer is moved backwards, date formatting is incorrect.
\Drupal::service('date.formatter')->format($timestamp, 'custom', 'c', 'America/Vancouver')
Example
In the 'America/Vancouver' time zone, the clock pointer was moved back to November 4, 2018 at 2:00 am. When universal time indicates 2018-11-04T09:15:00+00:00 in the America/Vancouver time zone, we expect 2018-11-04T01:15:00-08:00. The incorrect date is returned (2018-11-04T01:15:00-07:00).
-07:00 => -08:00
Return: 2018-11-04T01:15:00-07:00 ('America/Vancouver')
Expected: 2018-11-04T01:15:00-08:00 ('America/Vancouver')
How to reproduce
Use 'c' format: Full time format (ISO 8601 date)
$timestamp = 1541315700; // 2018-11-04T07:15:00+00:00
for ($i = 1; $i <= 5; $i++) {
$utc = \Drupal::service('date.formatter')->format($timestamp, 'custom', 'c', 'UTC');
$vancouver = \Drupal::service('date.formatter')->format($timestamp, 'custom', 'c', 'America/Vancouver');
$timestamp += 3600;
echo $utc . ' ' . $vancouver;
}
Result (Drupal 8.6.3, PHP 7.2.11)
2018-11-04T07:15:00+00:00 2018-11-04T00:15:00-07:00
2018-11-04T08:15:00+00:00 2018-11-04T01:15:00-07:00
2018-11-04T09:15:00+00:00 2018-11-04T01:15:00-07:00 // Expected -08:00
2018-11-04T10:15:00+00:00 2018-11-04T02:15:00-08:00
2018-11-04T11:15:00+00:00 2018-11-04T03:15:00-08:00
See #7. Drupal\Core\Datetime\DateFormatter::format() returns incorrect result for format 'c' and 'I'.
Notice:The reason is a bug in PHP
new DateTime('now') return incorrect timezone during DST transitions and `DateTimePlus::createFromTimestamp()`.
It was fixed in PHP 8.1.7. Now it works in Drupal 10 which uses in PHP 8.1. See PHP requirements. It still doesn't work in the older version e.g. Drupal 9.5 and PHP 8.0.
DateTime doesn't handle the transition from Daylight Saving Time back to Standard Time correctly.
Bug #74274 Handling DST transitions correctly
Bug #77103 new DateTime('now') return incorrect timezone during DST transitions
Notice: The same problem in other zones in the Eastern Hemisphere ('Australia/Sydney' or 'Europe/Warsaw').
Proposed resolution
Fix it for Drupal 9.5 under PHP 8.1 or add only tests for Drupal 10.
Remaining tasks
Add test
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 3018996-10.patch | 3.59 KB | mpdonadio |
| #10 | 3018996-10-test-only.patch | 2.42 KB | mpdonadio |
| #7 | 3018996-7-test-only.patch | 2.79 KB | krzysztof domański |
Comments
Comment #5
krzysztof domańskiComment #7
krzysztof domańskiReturns
Comment #8
krzysztof domańskiDrupal\Core\Datetime\DateFormatter::format()returns incorrect result for format'c'and'I'.Comment #9
krzysztof domańskiReturns
Perhaps the reason is in
\Drupal\Component\Datetime\DateTimePlus::format().Comment #10
mpdonadioThis sample shows it better:
Looks like the problem is https://bugs.php.net/bug.php?id=77103 and `DateTimePlus::createFromTimestamp()`.
My local setup is a little busted, but I think this will fix it. DateFormatterTest and DateTimePlusTest both pass locally.
Comment #14
krzysztof domańskiLooks good.
Comment #17
krzysztof domańskiRetested, unexpectedly test only pass on D10. See https://www.drupal.org/pift-ci-job/2565225
Comment #18
smustgrave commentedCould this issue be fixed in D10?
As pointed out the test-only passed on 9.4 and up.
NW for the issue summary but not an issue please close out or find the issue it was fixed in so we can move over credit.
Comment #19
krzysztof domański1/ PHP Bug #77103 new DateTime('now') return incorrect timezone during DST transitions was fixed in PHP 8.1.7. It should work in Drupal 10. See PHP requirements.
2/ The tests from https://www.drupal.org/files/issues/2020-11-29/3018996-10.patch still make sense
3/ Not sure if the fix should be added. However D9.5 with old PHP 7 will not work without fix.
https://www.drupal.org/node/3060/qa
Comment #20
krzysztof domańskiComment #21
smustgrave commentedBrought this up with @catch to see how best to proceed
If this is fixed in php8 we should add the test coverage still, just the tests
Then we think about backporting the full patch to 9.5.
Comment #23
alexpottThis is not fixed in the versions of PHP we're using.... the test is failing today :) https://git.drupalcode.org/issue/drupal-229778/-/pipelines/646294/test_r...