Bug #77103 [Com]: new DateTime('now') return incorrect timezone during DST transitions

From: Date: Thu, 04 Apr 2019 08:39:09 +0000
Subject: Bug #77103 [Com]: new DateTime('now') return incorrect timezone during DST transitions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220314@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77103&edit=1 ID: 77103 Comment by: jfcalcerrada at yahoo dot es Reported by: tomas dot prochazka at gmail dot com Summary: new DateTime('now') return incorrect timezone during DST transitions Status: Open Type: Bug Package: Date/time related Operating System: irrelevant PHP Version: 7.2.11 Block user comment: N Private report: N New Comment: Sorry! Corrected the expected output Expected: --------- string(25) "2017-10-29T00:59:59+01:00" string(25) "2017-10-29T01:00:00+01:00" string(25) "2017-10-29T01:59:59+01:00" string(25) "2017-10-29T01:00:00+00:00" string(25) "2017-10-29T01:59:59+00:00" Previous Comments: ------------------------------------------------------------------------ [2019-04-04 08:38:18] jfcalcerrada at yahoo dot es There seems to be a problem when transitioning out of DST. Some times are duplicated, as the offset is not handled correctly: $tz = new DateTimeZone('Europe/London'); $date = new DateTimeImmutable('now', $tz); $time = 1509235200; $date1 = $date->setTimestamp($time - 1); $date2 = $date->setTimestamp($time); $date3 = $date->setTimestamp($time + 3600 - 1); $date4 = $date->setTimestamp($time + 3600); $date5 = $date->setTimestamp($time + 3600*2 - 1); var_dump($date1->format(DateTime::ATOM)); var_dump($date2->format(DateTime::ATOM)); var_dump($date3->format(DateTime::ATOM)); var_dump($date4->format(DateTime::ATOM)); var_dump($date5->format(DateTime::ATOM)); Expected: --------- string(25) "2017-10-29T00:59:59+01:00" string(25) "2017-10-29T01:00:00+00:00" string(25) "2017-10-29T01:59:59+00:00" string(25) "2017-10-29T01:00:00+00:00" string(25) "2017-10-29T01:59:59+00:00" Actual: ------- string(25) "2017-10-29T00:59:59+01:00" string(25) "2017-10-29T01:00:00+00:00" string(25) "2017-10-29T01:59:59+00:00" string(25) "2017-10-29T01:00:00+00:00" string(25) "2017-10-29T01:59:59+00:00" ------------------------------------------------------------------------ [2018-11-03 23:12:24] tomas dot prochazka at gmail dot com Description: ------------ For timezone Europe/Prague should be DST changed at 2018-10-28 at 3 am back to 2 am. Until 3 am. it still should be CEST time, and after change it should be CET. So time between 2 and 3 is repeated two times, but in different timezone. When I'm using this code before 2 am in the transition day 2018-10-28 it works as expected: $d = DateTime('now') $d->format('Y-m-d H:i:s P T') => 2018-10-28 01:58:38 +02:00 CEST $d->$d->getOffset() => 7200 but few minutes later, after 2 am, the same code return 2018-10-28 02:00:04 +01:00 CET 3600 Which is not true, it still should be +02:00 CEST and 7200. After 2:39:59 it correctly return back to 2:00:00 and now already correctly with CET. So if you are logging data do some log file in the standardized format like $d->format(DateTime::ISO8601) you will get all times between 2 and 3 am twice the time with exactly the same time string. Which is really very wrong. I would not expect such bug in PHP. Luckily date('P, Z, I, T') works correctly It return "+02:00, 7200, 1, CEST" between 2 and 3 am before DST transition. I found that I can use this $d = new \Nette\Utils\DateTime(date('Y-m-d H:i:s T')); instead of 'now' and then it correctly return 2018-10-28 02:12:03 +02:00 CEST 7200 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77103&edit=1

« previous php.bugs (#220314) next »