Skip to content

[Clock] Add support for DateMalformedStringException and DateInvalidTimeZoneException #18827

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

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions components/clock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,34 @@ control on your time-sensitive code's behavior.

The :class:`Symfony\\Component\\Clock\\Test\\ClockSensitiveTrait` was introduced in Symfony 6.3.

Exceptions Management
---------------------

The Clock component takes full advantage of `the new DateTime exceptions`_
introduced in PHP 8.3: ``DateMalformedStringException`` and
``DateInvalidTimeZoneException``. These exceptions will be thrown
respectively when a invalid string is passed to a clock (e.g. when
creating a clock or modifying a ``MockClock``) and when an invalid timezone is
used::

$userInput = 'invalid timezone';

try {
$clock = Clock::get()->withTimeZone($userInput);
} catch (\DateInvalidTimeZoneException $exception) {
// ...
}

Thanks to the `symfony/polyfill-php83`_ dependency required by the Clock
component, you don't even need to run PHP 8.3 to enjoy these new explicit
and consistent exceptions when using the Clock component.

.. versionadded:: 6.4

The support for ``DateMalformedStringException`` and
``DateInvalidTimeZoneException`` was introduced in Symfony 6.4.

.. _`PSR-20`: https://www.php-fig.org/psr/psr-20/
.. _`accepted by the DateTime constructor`: https://www.php.net/manual/en/datetime.formats.php
.. _`the new DateTime exceptions`: https://wiki.php.net/rfc/datetime-exceptions
.. _`symfony/polyfill-php83`: https://github.com/symfony/polyfill-php83