Skip to content

Commit 5d75dcd

Browse files
[Clock] Add DatePoint
1 parent 80f7c9e commit 5d75dcd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

components/clock.rst

Lines changed: 43 additions & 0 deletions
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

@@ -193,6 +195,47 @@ being in a month or another.
193195

194196
The :class:`Symfony\\Component\\Clock\\ClockAwareTrait` was introduced in Symfony 6.3.
195197

198+
The ``DatePoint`` Class
199+
-----------------------
200+
201+
The Clock component comes with the
202+
:class:`Symfony\\Component\\Clock\\DatePoint` class. The main purpose of this
203+
class is to offer a stricter alternative to ``DateTimeImmutable`` by offering
204+
a stricter error handling, as well as stricter return types. ``DatePoint``
205+
actually extends ``DateTimeImmutable``, which means you can widely use it in
206+
your application if you already use ``DateTimeImmutable`` across your code base.
207+
208+
Creating a new ``DatePoint`` is as straightforward as creating other date
209+
objects::
210+
211+
use Symfony\Component\Clock\DatePoint;
212+
213+
// create a DatePoint set at the current date and time
214+
$now = new DatePoint();
215+
216+
// you can specify a timezone
217+
$withTimezone = new DatePoint(timezone: new \DateTimezone('UTC'));
218+
219+
// you can even create a DatePoint from a DateTimeImmutable
220+
$immutable = new \DateTimeImmutable();
221+
$fromImmutable = new DatePoint(reference: $immutable);
222+
223+
One big advantage of ``DatePoint`` is that it fully uses the Clock
224+
component. Indeed, when creating a new ``DatePoint`` object, the date
225+
and time are being fetched
226+
from the :class:`Symfony\\Component\\Clock\\Clock` class. This means
227+
that if you did any changes to the clock as stated in the
228+
:ref:`usage section <clock_usage>`, it will be reflected when creating
229+
a new ``DatePoint``. This can be particularly useful when
230+
:ref:`writing time-sensitive tests <clock_writing-tests>`.
231+
232+
.. versionadded:: 6.4
233+
234+
The :class:`Symfony\\Component\\Clock\\DatePoint` class was introduced
235+
in Symfony 6.4.
236+
237+
.. _clock_writing-tests:
238+
196239
Writing Time-Sensitive Tests
197240
----------------------------
198241

0 commit comments

Comments
 (0)