DateTimePlus does not create dates with proper timestamps during DST transitions

Created on 8 December 2018, about 6 years ago
Updated 1 March 2023, almost 2 years ago

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 🐛 DateTimePlus does not create dates with proper timestamps during DST transitions Needs work . 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

🐛 Bug report
Status

Needs work

Version

10.1

Component
Datetime 

Last updated 2 days ago

Created by

🇵🇱Poland Krzysztof Domański Poland

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024