Skip to content

Commit 2a88325

Browse files
[Clock] Add DatePoint
1 parent f509341 commit 2a88325

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,7 +62,7 @@ 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
The ``now()`` function takes an optional ``modifier`` argument
@@ -73,6 +75,9 @@ which will be applied to the current time::
7375
You can use any string `accepted by the DateTime constructor`_.
7476

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

7782
.. versionadded:: 6.3
7883

@@ -207,6 +212,48 @@ being in a month or another.
207212

208213
The :class:`Symfony\\Component\\Clock\\ClockAwareTrait` was introduced in Symfony 6.3.
209214

215+
.. _clock_date-point:
216+
217+
The ``DatePoint`` Class
218+
-----------------------
219+
220+
When using the Clock component, you manipulate instances of
221+
:class:`Symfony\\Component\\Clock\\DatePoint`. The main purpose of this
222+
class is to leverage the Clock component to get the "current time". When
223+
creating a new ``DatePoint`` object, the date and time are fetched
224+
from the :class:`Symfony\\Component\\Clock\\Clock` class. This means
225+
that if you did any changes to the clock as stated in the
226+
:ref:`usage section <clock_usage>`, it will be reflected when creating
227+
a new ``DatePoint``. This can be particularly useful when
228+
:ref:`writing time-sensitive tests <clock_writing-tests>`.
229+
230+
Creating a new ``DatePoint`` is as straightforward as creating other date
231+
objects::
232+
233+
use Symfony\Component\Clock\DatePoint;
234+
235+
// create a DatePoint set at the current date and time
236+
$now = new DatePoint();
237+
238+
// you can specify a timezone
239+
$withTimezone = new DatePoint(timezone: new \DateTimezone('UTC'));
240+
241+
// you can also create a DatePoint from a reference date
242+
$referenceDate = new \DateTimeImmutable();
243+
$relativeDate = new DatePoint('+1month', reference: $referenceDate);
244+
245+
``DatePoint`` extends ``DateTimeImmutable`` so that you can use it seamlessly
246+
everywhere a ``DateTimeImmutable`` is needed. In addition ``DatePoint`` offers
247+
stricter return types and provides consistent error handling across versions of
248+
PHP, thanks to polyfilling `PHP 8.3's behavior`_ on the topic.
249+
250+
.. versionadded:: 6.4
251+
252+
The :class:`Symfony\\Component\\Clock\\DatePoint` class was introduced
253+
in Symfony 6.4.
254+
255+
.. _clock_writing-tests:
256+
210257
Writing Time-Sensitive Tests
211258
----------------------------
212259

@@ -294,3 +341,4 @@ use them even if your project doesn't use PHP 8.3 yet.
294341
.. _`accepted by the DateTime constructor`: https://www.php.net/manual/en/datetime.formats.php
295342
.. _`PHP DateTime exceptions`: https://wiki.php.net/rfc/datetime-exceptions
296343
.. _`symfony/polyfill-php83`: https://github.com/symfony/polyfill-php83
344+
.. _`PHP 8.3's behavior`: https://wiki.php.net/rfc/datetime-exceptions

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