[php-src] Issue #10958: DateTimeImmutable parses time-fractions wrongly

From: Date: Tue, 28 Mar 2023 06:45:33 +0000
Subject: [php-src] Issue #10958: DateTimeImmutable parses time-fractions wrongly
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-244024@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/10958 Author: heiglandreas ### Description The following code: ```php <?php $validDateTimeStringsAccordingToIso8601 = [ '20230328T070000.000Z', // a basic format containing a microsecond fraction '2023-03-28T07:00:00.000Z', // The exact sme as extended format '20230328T07.5Z' // a basic format describing 7:30 according to ISO ]; foreach ($validDateTimeStringsAccordingToIso8601 as $dateTime) { try { $d = new DateTimeImmutable($dateTime); echo $d->format('c') . PHP_EOL; } catch (Throwable $t) { echo $t . PHP_EOL; } } ``` Resulted in this output: ``` Exception: Failed to parse time string (20230328T070000.000Z) at position 16 (0): Unexpected character in /in/ZJt06:11 Stack trace: #0 /in/ZJt06(11): DateTimeImmutable->__construct('20230328T070000...') #1 {main} 2023-03-28T07:00:00+00:00 2023-03-28T07:05:00+00:00 ``` But I expected this output instead: ``` 2023-03-28T07:00:00+00:00 2023-03-28T07:00:00+00:00 2023-03-28T07:30:00+00:00 ``` (See also https://3v4l.org/ZJt06) There seems to be an issue with the interpretation of fractions when parsing datetime-strings. According to ISO the aboe mentioned strings are all valid and should be parseable. Especially as the first and the second example are the same datetime in a different notation they should be interpreted similarily. > A decimal fraction may be added to the lowest order time element present in any of these > representations. A decimal mark, either a comma or a dot, is used as a separator between the time > element and its fraction. (Following [ISO 80000-1](https://en.wikipedia.org/wiki/ISO/IEC_80000) > according to ISO 8601:1-2019,[[26]](https://en.wikipedia.org/wiki/ISO_8601#cite_note-26) it does not > stipulate a preference except within International Standards, but with a preference for a > [comma](https://en.wikipedia.org/wiki/Comma_(punctuation)) according to ISO > 8601:2004.[[27]](https://en.wikipedia.org/wiki/ISO_8601#cite_note-27)) For example, to denote > "14 hours, 30 and one half minutes", do not include a seconds figure; represent it as > "14:30,5", "T1430,5", "14:30.5", or "T1430.5". > > There is no limit on the number of decimal places for the decimal fraction. However, the number > of decimal places needs to be agreed to by the communicating parties. For example, in Microsoft SQL > Server, the [precision](https://en.wikipedia.org/wiki/Significant_figures) of a decimal fraction is > 3 for a DATETIME, i.e., > "yyyy-mm-ddThh:mm:ss[.mmm]".[[28]](https://en.wikipedia.org/wiki/ISO_8601#cite_note-28) (https://en.wikipedia.org/wiki/ISO_8601#Times) I'd argue about the third but as it is specified in the ISO explicitly I'd also expect that to work. ### PHP Version PHP 8.1 and 8.2 ### Operating System _No response_

« previous php.bugs (#244024) next »