Skip to content

Commit 0c23e95

Browse files
[Clock] Add DatePoint
1 parent 80f7c9e commit 0c23e95

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

components/clock.rst

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Installation
3030
3131
.. include:: /components/require_autoload.rst.inc
3232

33+
.. _clock_usage:
34+
3335
Usage
3436
-----
3537

@@ -60,10 +62,13 @@ The Clock component also provides the ``now()`` function::
6062

6163
use function Symfony\Component\Clock\now;
6264

63-
// Get the current time as a DateTimeImmutable instance
65+
// Get the current time as a DatePoint instance
6466
$now = now();
6567

6668
Later on this page you can learn how to use this clock in your services and tests.
69+
When using the Clock component, you manipulate
70+
:class:`Symfony\\Component\\Clock\\DatePoint` instances. You can learn more
71+
about it in :ref:`the dedicated section <clock_date-point>`.
6772

6873
.. versionadded:: 6.3
6974

@@ -193,6 +198,54 @@ being in a month or another.
193198

194199
The :class:`Symfony\\Component\\Clock\\ClockAwareTrait` was introduced in Symfony 6.3.
195200

201+
.. _clock_date-point:
202+
203+
The ``DatePoint`` Class
204+
-----------------------
205+
206+
When using the Clock component, you manipulate instances of
207+
:class:`Symfony\\Component\\Clock\\DatePoint`. The main purpose of this
208+
class is to leverage the Clock component to get the "current time". When
209+
creating a new ``DatePoint`` object, the date and time are being fetched
210+
from the :class:`Symfony\\Component\\Clock\\Clock` class. This means
211+
that if you did any changes to the clock as stated in the
212+
:ref:`usage section <clock_usage>`, it will be reflected when creating
213+
a new ``DatePoint``. This can be particularly useful when
214+
:ref:`writing time-sensitive tests <clock_writing-tests>`.
215+
216+
Creating a new ``DatePoint`` is as straightforward as creating other date
217+
objects::
218+
219+
use Symfony\Component\Clock\DatePoint;
220+
221+
// create a DatePoint set at the current date and time
222+
$now = new DatePoint();
223+
224+
// you can specify a timezone
225+
$withTimezone = new DatePoint(timezone: new \DateTimezone('UTC'));
226+
227+
// you can also create a DatePoint from a reference date
228+
$referenceDate = new \DateTimeImmutable();
229+
$relativeDate = new DatePoint('+1month', reference: $referenceDate);
230+
231+
``DatePoint`` extends ``DateTimeImmutable`` so that you can use it seamlessly
232+
everywhere a ``DateTimeImmutable`` is needed. In addition ``DatePoint`` offers
233+
stricter return types and provides consistent error handling across versions of
234+
PHP, thanks to polyfilling `PHP 8.3's behavior`_ on the topic.
235+
236+
Also, ``DatePoint`` offers a stricter alternative to ``DateTimeImmutable`` by
237+
having stricter error handling, as well as stricter return types. ``DatePoint``
238+
actually extends ``DateTimeImmutable``, which means you can widely use it in
239+
your application if you already type against ``DateTimeImmutable`` across your
240+
code base.
241+
242+
.. versionadded:: 6.4
243+
244+
The :class:`Symfony\\Component\\Clock\\DatePoint` class was introduced
245+
in Symfony 6.4.
246+
247+
.. _clock_writing-tests:
248+
196249
Writing Time-Sensitive Tests
197250
----------------------------
198251

@@ -252,3 +305,4 @@ control on your time-sensitive code's behavior.
252305
The :class:`Symfony\\Component\\Clock\\Test\\ClockSensitiveTrait` was introduced in Symfony 6.3.
253306

254307
.. _`PSR-20`: https://www.php-fig.org/psr/psr-20/
308+
.. _`PHP 8.3's behavior`: https://wiki.php.net/rfc/datetime-exceptions

0 commit comments

Comments
 (0)