Bug #81074 [Com]: DateTime::setTime causes unexpected offset change

From: Date: Sun, 23 May 2021 09:58:48 +0000
Subject: Bug #81074 [Com]: DateTime::setTime causes unexpected offset change
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233996@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
 Comment by:         php-bugs at allenjb dot me dot uk
 Reported by:        php-bugs at allenjb dot me dot uk
 Summary:            DateTime::setTime causes unexpected offset change
 Status:             Open
 Type:               Bug
 Package:            Date/time related
 PHP Version:        8.0.6
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[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 (#233996) next »