Bug #81074 [Opn->Nab]: DateTime::setTime causes unexpected offset change

From: Date: Sun, 08 Aug 2021 11:49:11 +0000
Subject: Bug #81074 [Opn->Nab]: DateTime::setTime causes unexpected offset change
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235680@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81074&edit=1

 ID:                 81074
 Updated by:         derick@php.net
 Reported by:        php-bugs at allenjb dot me dot uk
 Summary:            DateTime::setTime causes unexpected offset change
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 PHP Version:        8.0.6
 Block user comment: N
 Private report:     N

 New Comment:

This is (now) expected behaviour. It prefers the non-DST time.

You're correct that saying that you can't chose to pick one over the other, but
that's a separate issue, and documented in https://wiki.php.net/rfc/datetime_and_daylight_saving_time#backward_transitions


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

Forgot to mention: I tested this on current master this morning and the issue still occurs there.

------------------------------------------------------------------------
[2021-05-23 09:57:30] php-bugs at allenjb dot me dot uk

Description:
------------
When using ->setTime() on a DateTime object, the offset may be changed even if the new time is
valid in the existing offset.

(Aside: This bug also reveals that with the current API it's actually impossible to use
->setTime() to set some valid time values)

Test script:
---------------
<?php

$tzUK = new DateTimeZone("Europe/London");
$tzUtc = new DateTimeZone("UTC");
$dt = DateTime::createFromFormat("!Y-m-d H:i:s", "2020-10-25 00:05:00", $tzUtc);
$dt = DateTime::createFromFormat('U', $dt->format('U'));

print $dt->format(DateTime::RFC3339 ." e") ."\n";

$dt->setTimeZone($tzUK);

print $dt->format(DateTime::RFC3339 ." e") ."\n";

$dt->setTime((int) $dt->format('H'), (int) $dt->format('i'), 0);

print $dt->format(DateTime::RFC3339 ." e") ."\n";

Expected result:
----------------
2020-10-25T00:05:00+00:00 +00:00
2020-10-25T01:05:00+01:00 Europe/London
2020-10-25T01:05:00+01:00 Europe/London

Actual result:
--------------
2020-10-25T00:05:00+00:00 +00:00
2020-10-25T01:05:00+01:00 Europe/London
2020-10-25T01:05:00+00:00 Europe/London


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



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


Thread (3 messages)

« previous php.bugs (#235680) next »