php / php-src

The PHP Interpreter
https://www.php.net
Other
37.98k stars 7.73k forks source link

DateTimeImmutable::createFromFormat parses non-existing timezone offsets #11150

Closed dbertovi closed 1 year ago

dbertovi commented 1 year ago

Description

The following code:

<?php
//OK Example:
$time = '2023-02-22 19:44:42.85857+05:30';
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.uP', $time);
echo $date->format('P');
//OUTPUT = +05:30 //OK

//BUG Example:
$time = '2023-02-22 19:44:42.127801-110';
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.uP', $time);
echo $date->format('P');
//OUTPUT = -01:10 //NOT OK, TIMEZONE OFFSET DOESN'T EXIST, must throw an exception

But I expected this output instead: Fatal Error

Shouldn't PHP check the proper existing time zone offset, when it already properly lists timezone identifiers (a.k.a. through DateTimeZone::listIdentifiers())?

PHP Version

PHP 8.0.28

Operating System

LiteSpeed

iluuu1994 commented 1 year ago

I'm guessing this is by-design. If you specify an offset you should know what you're doing. @derickr Can you confirm/deny?

dbertovi commented 1 year ago

I'm guessing not, but am open to that. Actually, completely other thing that I was doing lead me to this ambiguity. So for example, this function works like charm:

<?php
function getIsDaylightSaving($zoneid = 'America/Los_Angeles') {

    try {

        $date = new DateTime('now', new DateTimeZone($zoneid));
        return $date->format('I');

    } catch (Exception $e) {

        return 400;

    }

}

So if one try to do following:

<?php
//get daylight saving time status from timezone identifier, 
$dst = getIsDaylightSaving('America/New_York');
//OUTPUT = 1 //which is OK

//but if I use foo timezone identifier like (this one differs built in IANA timezone db):
$dst = getIsDaylightSaving('ABBA/Mama_Mia');
//The instance new DateTime('now', new DateTimeZone($zoneid)); will throw an exception which will be caught in this function
//so the OUTPUT will be 400 //which is also OK.

So my point is that I was surprised that PHP didn't care about timezone offsets, but do care about timezone identifiers, since timezone offsets are only the following:

<?php
$offsets = array('+00:00', '+01:00', '+02:00', '+03:00', '+03:30', '+04:00', '+04:30', '+05:00', '+05:30', '+05:45', '+06:00', '+06:30', '+07:00', '+08:00', '+08:45', '+09:00', '+09:30', '+10:00', '+10:30', '+11:00', '+12:00', '+12:45', '+13:00', '+14:00', '-01:00', '-02:00', '-02:30', '-03:00', '-04:00', '-05:00', '-06:00', '-07:00', '-08:00', '-09:00', '-09:30', '-10:00', '-11:00', '-12:00');

For that reason, I think it is a bug, or even some unfinished job. Anyway. I 'dodged a bullet' simply by checking for the offset myself. Everything else is explained in my first comment (i.e., $time variables are expected user input variables). Hope it helps.

damianwadley commented 1 year ago

The offset isn't some magic string that identifies a location like the named timezones. It's an offset. An amount of hours and minutes ahead or behind UTC.

So what's the added value that comes with restricting the offset to only those that are in active, official use by some area of the world? What problems does doing so solve, or what advantages does it offer?

If you're getting these values from the user then how do you know that the user didn't intend -01:10 for some reason? And why ask them for numeric offsets when the named ones are so much more human-friendly?

dbertovi commented 1 year ago

The offset isn't some magic string that identifies a location like the named timezones. It's an offset. An amount of hours and minutes ahead or behind UTC.

So what's the added value that comes with restricting the offset to only those that are in active, official use by some area of the world? What problems does doing so solve, or what advantages does it offer?

If you're getting these values from the user then how do you know that the user didn't intend -01:10 for some reason? And why ask them for numeric offsets when the named ones are so much more human-friendly?

What is this comment? Some kind of words game? Joke? That I am not into? Apropos what is human-friendly, or not, all those values can be a device snapshot. Some distant device snapshot having an application installed on it, that is programmed by a distant developer on certain distant operating system, with some distant programming language, that a developer will use, through his application, to fetch certain data... Now, that developer can be a professional, but can also be not-professional. And now going deeper in that story that here is being presented to my report is pure game of words as this answer is. Yada yada yada ... Please don't waste my time or yours on unconstructive comments.

That is not a topic here. Anyone's programming tasks, styles or stuff like that is not a topic here.

My point is that a PHP allows -01:10 value where it shouldn't. The end. Schluss. Ende. And that is all. And I didn't expect it to do so, to my surprise, though I use PHP on a daily basis since it's version 3.

damianwadley commented 1 year ago

That was a lot of words to say "it's wrong because I think it's wrong".

Why is it wrong to allow an offset that isn't in use by some (named) timezone? Why must it only allow offsets that can be found applied in (named) timezones?

dbertovi commented 1 year ago

Well, because of your 'human readability' - 'user-friendliness' etc. Why would I as a developer risk using PHP DateTime object (DateTimeImmutable), without being sure it'll do all the jobs, because I do not want some Carbon datetime library to mess with my RFC 3339 datetime standards implementation, with my ISO 8601 datetime standards implementation and with WHATWG HTML living standard datetime standard implementation. That's why. But I am flexible. On constructive part of it. And I will always find a solution. This I consider a bug. There's only 37/38 timezone offsets that should have been properly parsed by those functions. Our planet Earth is only so big.

dbertovi commented 1 year ago

And again, 'that was a lot...' - you are wasting my time.

MorganLOCode commented 1 year ago

The offset -1:10 does exist; it's 1050 nautical miles west of Greenwich meridian (measuring from the equator). There just doesn't happen to be a timezone that maps to it at present.

dbertovi commented 1 year ago

The offset -1:10 does exist; it's 1050 nautical miles west of Greenwich meridian (measuring from the equator). There just doesn't happen to be a timezone that maps to it at present.

Can you really be more STUPID? No one should be so stupid. What thef is wrong with you?

dbertovi commented 1 year ago

Why would anybody talk about things they do not understand or even comment about it? Why did you delete my last comment? Abuse? Abuse of stupidity? Well, delete everything here. You have my license to do so. F-word.

The offset -1:10 does exist; it's 1050 nautical miles west of Greenwich meridian (measuring from the equator). There just doesn't happen to be a timezone that maps to it at present.

Is this intellectual? What is this, PHP????? Joke? some king of programming joke? Programatical joke? Joke? That is imbecilism.

I really never used this possibility to report a bug. On PHP. This is my first one. If you are hitting me with your 'best-ones' than we really are fucked. I kept PHP as authority. Now I am not sure. And it's over. The love is over. Whether DateTime and it's complementary functions are crap or not, I do not allow such depravity of intelligence. Delete this shit here, and let's go our own ways. My brain always told me - learn all. So I did. This is imbecility. To mark my comment on stupidity as abusive. FU. Mark this one too. While this a-hole https://github.com/damianwadley, with no project or any intelligence is completely OK guy? I am abusive for HAVING to take so much time loss, thinking and SH? Screwya. This is all lie. And I know. And I do not dance to your or anyone elses lies. FU!

dbertovi commented 1 year ago

Imbeciles.

dbertovi commented 1 year ago

The best thing in one's organization is when one's organization thinks it is given by the 'Creation'. Deity. godshit-like-things. FU, WE the engineers are keeping your disgusting asses alive. But then, we are the engineers. Why do you do this? Training some sort of discipline? It'll be your demise.

dbertovi commented 1 year ago

Why would you do all this? Why? Why spin me? Why? Are you all tested? High on whats the name of the drug oh yea are you al high on cocaine? What are you high on?