php / php/php-src

IntlDateFormatter::localtime() unexpectedly causes time zone to be changed if specified

Offen
#13,766 4 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@devnexen arbeitet bereits daran.

Seit 20.3.2024.

Extension: intl Feature Status: Needs Triage
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

Description

The following code:

<?php

$oIntlDateFormatter = new IntlDateFormatter("en_GB");
$oIntlDateFormatter->setTimeZone('Europe/Berlin');
$oIntlDateFormatter->setPattern('VV');

$oIntlDateFormatter->parse('America/Los_Angeles', $offsetResult);
var_dump($oIntlDateFormatter->getTimeZone()->getID());

$oIntlDateFormatter->localtime('America/Los_Angeles');
var_dump($oIntlDateFormatter->getTimeZone()->getID());

Resulted in this output:

string(13) "Europe/Berlin"
string(19) "America/Los_Angeles"

But I expected this output instead:
Either:

string(13) "Europe/Berlin"
string(13) "Europe/Berlin"

Or

string(19) "America/Los_Angeles"
string(19) "America/Los_Angeles"

IntlDateFormatter should either consistently keep the originally set time zone or consistently overwrite the time zone whenever different time zone data is supplied.

IntlDateFormatter::localtime() causes the time zone within an IntlDateFormatter object to be changed, if a time zone was provided in the used date/time pattern. This is nowhere mentioned in the documentation of IntlDateFormatter::localtime() and in contrast IntlDateFormatter::parse() does not behave this way. This is why I think the behaviour of IntlDateFormatter::localtime() is not intentional.

In contrast when I look at DateTime::createFromFormat(), a time zone supplied in the $datetime parameter consistently overrides a supplied $timezone parameter.

Having looked a bit into PHPs code, It seems like DateTime::createFromFormat() and internally always generates a field-based time value and generates a date/time representation from that, just like IntlDateFormatter::localtime(). So this may be also triggering the change of the time zone.
IntlDateFormatter::parse() on the other hand appears to to only call into the ICU library to get a parse the supplied date/time string.

PHP Version

PHP 8.3.4

Operating System

Ubuntu 22.04.1

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.