Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ The reason inside ] DateTime\DateTimeImmutable\DateTimeInterface.... how much more? #7965

Closed
6562680 opened this issue Jan 18, 2022 · 9 comments

Comments

@6562680
Copy link

6562680 commented Jan 18, 2022

Description

There are guys who wants Psr/Clock/ClockInterface with just one method ->now()...

  1. \DateTime, \DateTimezone, \DateInterval marked with \Exception. Most used editor PHPStorm highlights it. Since it is very internal method - using native \DateTime forces highlight all the code. Highlighting means "PLEASE TRY CATCH IT". So since 50% percent of tasks needs dates inside their logic - you have try/catch in 50% code just to hide damn exception and throw \Runtime instead. Its still an exception. But hidden one. So we have guys who wants ClockInterface

  2. Next one - Relative Date vs Absolute date. In PHP we have the date as single feature (but with 3? or more implementations) - functional way, object way, etc. The point is TIMEZONE. I dont know how exactly initial timezone comes inside PHP (while no one in config - perhaps at build from source time) - but i saw 10+ servers where unix commands show that the timezone is UTC, but PHP shows timezone as "Helsinki" or something. Yes it could be fixed with php.ini.

But with next things:
(1) You still have timezone in your database that could be different from your Unix. And with your php. And you WILL dance with that +1 -2 +3 -4 every time. Best configuration - same timezone everywhere. To do that you should be at least amateur in linux/docker/(mysql|postgres)/php+fpm. Usually this amateur is a guy known as dev-ops, he is clever guy, he wants 3000 USD/month to work only 10minutes per month in case your app falls down. Most product companies prefers outsourcing, and now you CANT ask this guy about this timezone without democratic negotiations.
(2) You know about timezone difference in maybe 2-3 months after project published. It already has a database for maybe 100k-1m records minimum with failed dates. And now you get the task "i dont know, but we differ in 2 hours, please, be THE PROGRAMMER - solve our brainpain"

The reason of above is IMPORTANT difference between relative and absolute date.

Relative date comes from your CPU clock. You write "today" or "yesterday" and you'll get for me (now):
2022-01-18 20:00:00 UTC+2

Ok, lets remember your server (and your postgres configured with UTC). You apply new \DateTimeZone('UTC') one of this way:

  1. new \DateTime($date, $timezoneUTC)
  2. (new \DateTime($date))->setTimezone($timezoneUTC)
  3. (new \DateTime($date, $timezone))->setTimezone($timezoneUTC)
  4. default_timezone_set($timezoneUTC); new \DateTime($date);
  5. ....want some more??

You ALWAYS get unexpected difference in (for me) 2 hours. So the time will be:
2022-01-18 18:00:00 UTC+0

But wait, now you should do that in your brain on every math operation, and try to remember it.

How to get same date as you see in your mac/windows/ubuntu tray, huh, developer?

Tada (it isnt "correct", its "usable", btw humans always incorrect):

new \DateTime((new \DateTime($yourStringNonObjectOrHelloDearExceptionDateHere))->format('Y-m-d H:i:s.u'), $timezoneUTC);

How many hours newbie should spent to research that ?? The point is difference between RELATIVE and ABSOLUTE dates. If you pass timezone as 2nd parameter with relative date - it will be applied like "create date, then - change timezone", instead of "create MY date in described timezone", do you get that?

BUT, we want to apologize - "today midnight" isnt a RELATIVE date. Actually not fully relative. It creates the date with 00:00:00 timestamp. And it will surprise you only if date been created inside UTC difference (for me) 2 hours from UTC. There will be previous day, but server awaits next one.

How about create DIFFERENT classes for Relative date and Absolute date?

Postgres solves that by presenting two different types \DateTime + \DateTimeWithTimezone. Problem of that - you cant just add timezone for non-timezone date, so postgres wont solve that

Or fix the behavior - "today, UTC" means "GET MY TIME IN UTC, I KNOW ITS A MISTAKE, BUT IT WILL HELP ME" :DD

And the next point.

When we serialize date into some storage, we need DATE_ATOM or maybe sql date format.
But while we're in the code - we need objects.

So creating date with new \DateTime() (and actually twice, and actually with timezone, and actually with trycatch in each class) - it wont respects use case "we already have the date object" so we need everywhere write that damn instanceof stuff:

$date = $date instanceof \DateTimeInterface // @gzhegow > whoops, interface doesnt have setTimezone, we need to rewrite...

Lets rewrite:

    /**
     * @author gzhegow > do you think it will be easy? No, its just \DateTime, you have to copy it for \DateTimeImmutable too
     *  > and it will unexpectedly change your timezone to UTC if you dont pass 2nd argument. Sometimes you need that. Sometimes no
     *
     * @param null|string|\DateTimeInterface $date
     * @param null|string|\DateTimeZone      $timezone
     *
     * @return \DateTimeInterface
     */
    private function newDate($date = null, $timezone = null) : \DateTimeInterface
    {
        $date = $date ?? 'now';
        $timezone = $timezone ?? 'UTC';
        $timezone = $timezone instanceof \DateTimeZone
            ? $timezone
            : new \DateTimeZone($timezone);

        try {
            $date = null
                ?? ( $date instanceof \DateTime ? $date->setTimezone($timezone) : null )
                ?? ( $date instanceof \DateTimeImmutable ? $date->setTimezone($timezone) : null )
                ?? ( new \DateTime(
                    ( new \DateTime($date) )->format('Y-m-d H:i:s.u'), $timezone
                ) );
        }
        catch ( \Exception $e ) {
            throw new \RuntimeException($e->getMessage(), null, $e); // @gzhegow > anyone, ask to implement `rethrow` language construct
        }

        return $date;
    }

To create date in PHP safely you need to always write that construct. In each class you need dates.

And am not speaking about humanize date stuff, localize today/yesterday/tomorrow, localize ago dates (ohh, you WILL meet the \IntlFormatter from lib-intl, and spent 3-4 hrs to remember it for long (?), am sure), and actually wont speak about localize ago in human expected format - that means "less than day = minutes/hours ago, greater than day - show date in Ymd" like Pavel Durov does.

Hello, Laravel developer, this paragraph for you. Your CARBON library eats 60ms for initialize cause of broken brain of its author that uses 1000 times preg_match. Nevermind.

Lets fix at least relative date stuff. Just to reduce syntax.

Other stuff we could speak later.

Thanks for your time, folks. Be safe.

[ Гриша, ты уже всех достал своим английским, уходи ]

@damianwadley
Copy link

Could you try summarizing what it is that you're asking for?

@6562680
Copy link
Author

6562680 commented Jan 19, 2022

  1. Last time i've created short topic - i've received stupid questions (known use cases). Once i created long one i receive "w00t"? How old are you?
  2. Ppl who we brought up (or not just "we") with nice smile recommends ANYONE to read "docs", it becomes a mem for now. Usually "the docs" contains 40 sheets of text, but you need 1 sentence. And all of the coders did that. Asking about "summarize" means "i did it no times", really? Or maybe you honor only "known ppl" who speaking from streams and big company meetups, and want "unkowns" like me to shut up?
  3. Read text, imagine yourself with that pain, read bold text, enable the brain, imagine some decision, write some comment. What an idea to write "i understand nothing, please, write more"

Fix date syntax, cus of there's no 1 problem. There's 12 problems in a row, that is created everytime this lib was upgraded. Its good features but really to work with dates you have to write private method for 30 lines in each file. Cause of broken syntax.

@damianwadley
Copy link

This issue tracker is for bug reports and feature requests. If you'd like to talk about how difficult it has been for you to use DateTime and DateTimeZone then consider signing up with WordPress or Blogger. If you'd like help understanding how to use them better then try asking on StackOverflow or elsewhere. If you don't want to deal with PHP's date/time classes yourself then there are libraries like Carbon you can use instead.

If you have concrete suggestions for what PHP could do differently then check out the RFC process.

@6562680
Copy link
Author

6562680 commented Jan 19, 2022

"Nice work bitch" (c) Escape From Tarkov

Three times i've catched PHP Dev Team realises idea that i proposed 3 years before. So slow.

Ignore, ban, block, shoot. (c) Earth
Apologize, ask, listen, understand, empathy (c) Somewhere not here

Continue to listen these "webinars" about whats better postgres or mysql.

@KapitanOczywisty
Copy link

@6562680 They might be slow to implement some features, but they also are responsible for stability and integrity of this project, so even if sometimes frustrating, they have right to make own choices.

You're report is chaotic and hostile, nobody wants to read that. And your bad experiences does not justify being d.. to random maintainers. If you want real change, be constructive and respectful.

@6562680
Copy link
Author

6562680 commented Jan 19, 2022

@KapitanOczywisty

@6562680 They might be slow to implement some features, but they also are responsible for stability and integrity of this project, so even if sometimes frustrating, they have right to make own choices.

You're report is chaotic and hostile, nobody wants to read that. And your bad experiences does not justify being d.. to random maintainers. If you want real change, be constructive and respectful.

Respectfullness will not lead you do success. In any cases.

1 way) In market cases only respectless gives you money - ppl likes those who do forbidden things, not them who has emathy (jesus was murdered, yes?) - and ITS NOT my way (if yours - the speak is end right now)

2 way) In science cases respectfullness is only emotion, emotion needed by your brain, not by society. Emotion - is an engine being inside you, gives you energy. Its not bad, its required thing, very powerfull thing, but emotions should GIVE energy instead of CONTROL your action. When you prefer emotions while working with society - you'll get the war about preferences, holywar i mean, and after some time - no results, spent time and its all.

Society is about "we have to take care about this bastards otherwise they destroy each one, and us too in the batch". They dont need your help or salvation. They will scold and even resist you. Its not about "they dont want". Its about "they dont know what they want". But if you dont participate - powerfuls of this world ask you to "defend the motherland" and gives you rifle to kill another guys who dont want the war like you.

It will work like that:
every 26 minutes becomes a new billionare (guys who select first way), and every minute becomes 146 poor people. When too many poor people - its a problem of 1917. So the people will be dressed in military and expelled in other country by generals who likes "respectfulness". Its a working 2000 year old story. Thats why we MUST select other way.

Correct way is "apologize, ask, listen, understand, imagine solution, present it, spent some time to talk about". There's not respectfulness. This story is about "what happens if?" and "what it looks like?" This way is not available for 95% of ppl. For the justice 15 years ago this way wasnt available for 85% ppl. Today - 95%. More poor people - less respectful people. War moves closer.

Be safe.

@6562680
Copy link
Author

6562680 commented Jan 19, 2022

@damianwadley

This issue tracker is for bug reports and feature requests. If you'd like to talk about how difficult it has been for you to use DateTime and DateTimeZone then consider signing up with WordPress or Blogger. If you'd like help understanding how to use them better then try asking on StackOverflow or elsewhere. If you don't want to deal with PHP's date/time classes yourself then there are libraries like Carbon you can use instead.

I answer with a quote that you didnt even read, but already ask me to go away:

Hello, Laravel developer, this paragraph for you. Your CARBON library eats 60ms for initialize cause of broken brain of its author that uses 1000 times preg_match. Nevermind.

Remember. We didnt write workers 90% of the time. Our scripts initialises 1000 times in a seconds in parallel with nginx.

@6562680
Copy link
Author

6562680 commented Jan 19, 2022

He's crazy but he's right (c)

@emerencia31

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants