Skip to content

Commit 4aca469

Browse files
[Clock] Add DatePoint
1 parent 80f7c9e commit 4aca469

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

components/clock.rst

Lines changed: 49 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,49 @@ 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 is that it fully leverages the Clock component. Indeed, when creating a new
209+
``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 DateTimeImmutable
228+
$immutable = new \DateTimeImmutable();
229+
$fromImmutable = new DatePoint(reference: $immutable);
230+
231+
Also, ``DatePoint`` offers a stricter alternative to ``DateTimeImmutable`` by
232+
having stricter error handling, as well as stricter return types. ``DatePoint``
233+
actually extends ``DateTimeImmutable``, which means you can widely use it in
234+
your application if you already type against ``DateTimeImmutable`` across your
235+
code base.
236+
237+
.. versionadded:: 6.4
238+
239+
The :class:`Symfony\\Component\\Clock\\DatePoint` class was introduced
240+
in Symfony 6.4.
241+
242+
.. _clock_writing-tests:
243+
196244
Writing Time-Sensitive Tests
197245
----------------------------
198246

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy