Bug #81072 [Com]: Incorrect behavior of DateTime->modify across DST changes

From: Date: Fri, 29 Apr 2022 09:47:32 +0000
Subject: Bug #81072 [Com]: Incorrect behavior of DateTime->modify across DST changes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-241317@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81072&edit=1

 ID:                 81072
 Comment by:         r dot aalderink at visymo dot com
 Reported by:        ph-bugs at allenjb dot me dot uk
 Summary:            Incorrect behavior of DateTime->modify across DST
                     changes
 Status:             Verified
 Type:               Bug
 Package:            Date/time related
 PHP Version:        8.0.6
 Block user comment: N
 Private report:     N

 New Comment:

Ran into the same issue, I have a very simple oneliner to reproduce it:

-----
echo (new \DateTime('2022-03-13 03:00:00', new
\DateTimeZone('America/Los_Angeles')))->modify('-5
minutes')->format('Y-m-d H:i:s');
-----

This becomes: 2022-03-13 03:55:00
Expected: 2022-03-13 01:55:00


Previous Comments:
------------------------------------------------------------------------
[2021-10-01 18:25:40] cmb@php.net

> As you can see, when working backwards with ->modify("-1 hour")
> DateTime fails to properly handle the DST change and keeps pushing
> the time forward to 03:00:00

So this about <https://3v4l.org/XWdf2>, right?

If so, yes, that doesn't look right.

------------------------------------------------------------------------
[2021-07-30 10:53:24] antonino dot spampinato86 at gmail dot com

Sorry change $dtWithout_test1 = incrementWithoutSwitch($dtWithout, $invert); to $dtWithout =
incrementWithoutSwitch($dtWithout, $invert);
The bug is there, the expected result is not obtained. :-)

------------------------------------------------------------------------
[2021-07-30 10:43:46] antonino dot spampinato86 at gmail dot com

This is the refined version proposed. DST transition bug for modify function exists. Bug 74224
mentions that from php 8.1 that diff function has been changed and supports time not conversion to
UTC and this makes time or date calculations consistent with local time. Actually diff function also
suffers from DST or ST problems, If you deem it necessary I will also show it to you.

function incrementWithSwitch($date, $invert)
{
    $timezone = $date->getTimezone();
    $date = $date->setTimezone(new \DateTimeZone('UTC'));
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    $date = $date->setTimezone($timezone);

    //$date = $date->setTime((int)$date->format('H'), 0);
    return $date;
}


function incrementWithoutSwitch($date, $invert)
{
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    return $date;
}

$tz = new \DateTimeZone('Europe/London');
$dtWith = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', '2021-03-28
04:00:00', $tz);
$dtWithout = $dtWith;

$invert = true;
$string = 'Start ' . $dtWith->format('Y-m-d H:i:s Z') . PHP_EOL;
for ($i = 0; $i < 4; $i++) {
    $string .= "Round {$i}: ";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout_test1 = incrementWithoutSwitch($dtWithout, $invert);
    $string .= 'With switch:    ' . $dtWith->format('Y-m-d H:i:s Z');
    $string .= ' :: Without switch: '. $dtWithout->format('Y-m-d H:i:s Z') .
PHP_EOL;
}

$string .= '---' . PHP_EOL;
$dtWith = \DateTimeImmutable::createFromFormat('!Y-m-d H:i:s', '2021-03-27
23:00:00', $tz);
$dtWithout = $dtWith;

$invert = false;
$string .= 'Start ' . $dtWith->format('Y-m-d H:i:s Z') . PHP_EOL;
for ($i = 0; $i < 4; $i++) {
    $string .= "Round {$i}: ";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    $string .= 'With switch:    ' . $dtWith->format('Y-m-d H:i:s Z');
    $string .= ' :: Without switch: ' . $dtWithout->format('Y-m-d H:i:s Z') .
PHP_EOL;
}
print($string);

Expected:
Start 2021-03-28 04:00:00 3600
Round 0: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 03:00:00 3600
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 02:00:00 3600
Round 2: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 00:00:00 3600
Round 3: With switch:    2021-03-27 23:00:00 0 :: Without switch: 2021-03-27 23:00:00 3600
---
Start 2021-03-27 23:00:00 0
Round 0: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 00:00:00 0
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 02:00:00 3600
Round 2: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 03:00:00 3600
Round 3: With switch:    2021-03-28 04:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600

Actual: Start 2021-03-28 04:00:00 3600
Round 0: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600
Round 2: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 04:00:00 3600
Round 3: With switch:    2021-03-27 23:00:00 0 :: Without switch: 2021-03-28 04:00:00 3600
---
Start 2021-03-27 23:00:00 0
Round 0: With switch:    2021-03-28 00:00:00 0 :: Without switch: 2021-03-28 00:00:00 0
Round 1: With switch:    2021-03-28 02:00:00 3600 :: Without switch: 2021-03-28 02:00:00 3600
Round 2: With switch:    2021-03-28 03:00:00 3600 :: Without switch: 2021-03-28 03:00:00 3600
Round 3: With switch:    2021-03-28 04:00:00 3600 :: Without switch: 2021-03-28 04:00:00 3600

------------------------------------------------------------------------
[2021-05-30 04:22:13] php-bugs at lists dot php dot net

No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.

------------------------------------------------------------------------
[2021-05-23 09:45:58] php-bugs at allenjb dot me dot uk

I've tested this with current master (8.1-dev) and the issue still exists.

I noticed a bug in the reproduction script I previously posted and have fixed it. Updated script: https://3v4l.org/01oD9

<?php

function incrementWithSwitch($date, $invert)
{
    $timezone = $date->getTimezone();
    $date = $date->setTimezone(new DateTimeZone('UTC'));
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    $date = $date->setTimezone($timezone);

    $date = $date->setTime((int)$date->format('H'), 0);
    return $date;
}


function incrementWithoutSwitch($date, $invert)
{
    $date = $date->modify(($invert ? '-' : '+') . '1 hour');
    return $date;
}

$tz = new \DateTimeZone("Europe/London");
$dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28
04:00:00", $tz);
$dtWithout = $dtWith;

$invert = true;
for ($i = 0; $i < 4; $i++) {
    print "\nRound {$i}:";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    print "With switch:    ". $dtWith->format(\DateTime::RFC3339);
    print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339);
}

print "\n---\n";

$dtWith = \DateTimeImmutable::createFromFormat("!Y-m-d H:i:s", "2021-03-28
00:00:00");
$dtWithout = $dtWith;

$invert = false;
for ($i = 0; $i < 4; $i++) {
    print "\nRound {$i}:";
    $dtWith = incrementWithSwitch($dtWith, $invert);
    $dtWithout = incrementWithoutSwitch($dtWithout, $invert);
    print "With switch:    ". $dtWith->format(\DateTime::RFC3339);
    print " :: Without switch: ". $dtWithout->format(\DateTime::RFC3339);
}

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=81072


--
Edit this bug report at https://bugs.php.net/bug.php?id=81072&edit=1


Thread (11 messages)

« previous php.bugs (#241317) next »