8000 feature #18932 [Clock] Add `DatePoint` (alexandre-daubois) · symfony/symfony-docs@79ea13c · GitHub
[go: up one dir, main page]

Skip to content

Commit 79ea13c

Browse files
committed
feature #18932 [Clock] Add DatePoint (alexandre-daubois)
This PR was merged into the 6.4 branch. Discussion ---------- [Clock] Add `DatePoint` Fix #18930 Commits ------- f14bc14 [Clock] Add `DatePoint`
2 parents bde97b5 + f14bc14 commit 79ea13c

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

components/clock.rst

+54-1
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,53 @@ 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+
The Clock component uses a special :class:`Symfony\\Component\\Clock\\DatePoint`
221+
class. This is a small wrapper on top of PHP's :phpclass:`DateTimeImmutable`.
222+
You can use it seamlessly everywhere a :phpclass:`DateTimeImmutable` or
223+
:phpclass:`DateTimeInterface` is expected. The ``DatePoint`` object fetches the
224+
date and time 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 a new
227+
``DatePoint``. You can also create a new ``DatePoint`` instance directly, for
228+
instance when using it as a default value::
229+
230+
use Symfony\Component\Clock\DatePoint;
231+
232+
class Post
233+
{
234+
public function __construct(
235+
// ...
236+
private \DateTimeImmutable $createdAt = new DatePoint(),
237+
) {
238+
}
239+
}
240+
241+
The constructor also allows setting a timezone or custom referenced date::
242+
243+
// you can specify a timezone
244+
$withTimezone = new DatePoint(timezone: new \DateTimezone('UTC'));
245+
246+
// you can also create a DatePoint from a reference date
247+
$referenceDate = new \DateTimeImmutable();
248+
$relativeDate = new DatePoint('+1month', reference: $referenceDate);
249+
250+
.. note::
251+
In addition ``DatePoint`` offers stricter return types and provides consistent
252+
error handling across versions of PHP, thanks to polyfilling `PHP 8.3's behavior`_
253+
on the topic.
254+
255+
.. versionadded:: 6.4
256+
257+
The :class:`Symfony\\Component\\Clock\\DatePoint` class was introduced
258+
in Symfony 6.4.
259+
260+
.. _clock_writing-tests:
261+
210262
Writing Time-Sensitive Tests
211263
----------------------------
212264

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

0 commit comments

Comments
 (0)
0