Skip to content

Conversation

@heiglandreas
Copy link
Contributor

The question "why not to convert to UTC" comes up very frequently so
Larry asked to add the answer to the meta-docs for PSR-20.

The question "why not to convert to UTC" comes up very frequently so
Larry asked to add the answer to the meta-docs for PSR-20.

### Why not simply convert all times to UTC?

Timezones change! Regularily! And unforseeably! So when you are converting a DateTime in the future to UTC you are using the offset that is known today to convert from the local time to UTC. When that offset changes between now and the datetime then you have no chance to adapt to that.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're talking about timestamp, it's not only timezones. DST is problem by itself for delivery service i.e. you order to deliver a package at day X at 12:00 and that's converted to int. Then DST comes into effect some days before and you get your package either at 11:00 or 13:00 instead of 12:00.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Regularily/Regularly/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not an issue as that information is encoded into the timezone-information.

Remember: A timezone is a geographical region that is observing the same date and time by governmental definition. Such a timezone is identified by a timezone-identifier such as Europe/Moskow.
Do not confuse timezones with offsets that are defining the difference to the time-standard (which can be either GMT or UTC) or with named offsets like MSK that cover some (but not all) timezones that are observing a certain offset. A timezone can be referenced via different named offsets at different times due to DST changes.

So when using a timezone, the DST information is available. When using an offset then you do not know whether that offset is standard-time or daylight saving time.

@hvanoch
Copy link

hvanoch commented Oct 29, 2021

Notice that there is a case when storing the time in local time with timezone can loose information.
Example:

var_dump((new DateTimeImmutable('2021-10-31 00:30:00', new DateTimeZone('UTC')))->setTimeZone(new DateTimeZone('Europe/Brussels')));
var_dump((new DateTimeImmutable('2021-10-31 01:30:00', new DateTimeZone('UTC')))->setTimeZone(new DateTimeZone('Europe/Brussels')));

If we would store 2021-10-31 02:30:00, Europe/Brussels in the database, we would not know if it is before or after DST.

@Crell
Copy link
Contributor

Crell commented Oct 29, 2021

@hvanoch Hm. Another fun and exciting problem... I can't tell if that's an argument for or against using UTC, though, just that datetime is fugly. 😄

@heiglandreas
Copy link
Contributor Author

That is indeed correct. And in a perfect world you would store 2021-10-31:2:30:00+01:00, Europe/Brussels in the database. Sadly a technical limitation causes the issue that in PHP we will lose the information due to the fact that there is an RFC open but not yet implemented which would solve this issue. As long as this is not implemented that is indeed an issue. Also entering correct birth dates in currently not supported for i.e. Germany (someone born in the second iteration of the time between 2 and 3 o clock on the morning of the DST back-transition will be born at 2:xx').

As long as it is not supported to call new DateTimeImmutable('2021-10-31 02:30:00+01:00', new DateTimeImmutable('Europe/Berlin')); this one hour is sadly lost.

Nevertheless: this one possible lost hour for future datetimes is much less hassle than converting everything back and forth to and from UTC. And Log-like Information (everything that happens right now can easily be stored in UTC (Yes. it might happen that the calculation from local time to UTC might be broken due to changes that should have been applied now have not yet found their way into the TZDB due to governments thinking one day in advance is enough time for everyone to react)

For the ClockInterface though that is irrelevant as the clock returns a slice of the current time and this one slice will also during the transition backwards return the correct information so when your business-logic calls $clockInterface->now()->getTimestamp() you will get a different timestamp than an hour before or after.

@Crell
Copy link
Contributor

Crell commented Dec 24, 2021

The analogy based argument isn't really convincing for me. At this point I've essentially resigned myself to the fact that the timezone will be undefined, but the argument for it needs to be solid enough to avoid 10 years of people whining about it. (See also: PSR-7.) I don't feel like this is.

From advocating the use of local times (which is hard to do for local
times) this argumentation moves towards explaining that the interface
itself, the explicit contract, does not care about the implementation
and with this rather open explicit contract everyone can also implement
their own implicit contract to use "UTC" or whatever they want. If they
want to. The interface itself is as open as possible while at the same
time being as strict as necessary
@heiglandreas heiglandreas force-pushed the addReasonsNotToConvertToUtc branch from aa89846 to d366693 Compare December 29, 2021 08:20
@heiglandreas
Copy link
Contributor Author

I've redone the argumentation completely away from advocating the use of local time over UTC (which is still a good idea, but kinda hard to convince people that it makes sense even for current date-times) over to trying to make it clear that the interface is as open as possible while at the same time being as strict as necessary. Not enforcing UTC on the explicit contract that the interface defines does not hinder an implicit contract on the implementation level which defines that the interface will always return objects with a certain timezone preset.

Copy link
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition itself is fine. Minor issues are easy to be fixed.

heiglandreas and others added 5 commits January 6, 2022 09:56
Co-authored-by: Alexander Makarov <[email protected]>
Co-authored-by: Alexander Makarov <[email protected]>
Co-authored-by: Alexander Makarov <[email protected]>
Co-authored-by: Alexander Makarov <[email protected]>
Co-authored-by: Alexander Makarov <[email protected]>
@localheinz
Copy link
Contributor

What is the hold-up here - can we move this forward?

use-cases that do require something else will have to explicitly work around that or find other ways of
handling the problem, the contract tries to solve as the contract does not fit the needs. This is different
from the issue of i.e., immutability which can also not be enforced on the language level but which is
necessary to adhere to other calls on the contract. IN the case of the `clock`-interface there will be no
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
necessary to adhere to other calls on the contract. IN the case of the `clock`-interface there will be no
necessary to adhere to other calls on the contract. In the case of the `clock`-interface there will be no

Typo?

Copy link
Contributor

@ashnazg ashnazg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the "English flow" could be tweaked, but content seems good to me.

@ashnazg
Copy link
Contributor

ashnazg commented Jul 18, 2022

@cseufert ping... Github shows you still active in general, so reaching out to you here rather than just on Discord to confirm you're still interested in this PSR... ?

@ashnazg
Copy link
Contributor

ashnazg commented Jul 18, 2022

@cseufert, as Editor, you can merge these, right?

@cseufert
Copy link
Contributor

I cant merge it, can only approve it for merging

@ashnazg
Copy link
Contributor

ashnazg commented Jul 18, 2022

I cant merge it, can only approve it for merging

Ok, I'll find out who can, and get both open PRs merged. Thanks for getting them both approved, and welcome back ;-)

@Jean85 Jean85 merged commit 7d0c5d5 into php-fig:master Jul 18, 2022
@Jean85
Copy link
Member

Jean85 commented Jul 18, 2022

Done, my pleasure! 😄

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants