@@ -30,6 +30,8 @@ Installation
3030
3131 .. include :: /components/require_autoload.rst.inc
3232
33+ .. _clock_usage :
34+
3335Usage
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
6668The ``now() `` function takes an optional ``modifier `` argument
@@ -73,6 +75,9 @@ which will be applied to the current time::
7375You can use any string `accepted by the DateTime constructor `_.
7476
7577Later 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+
210262Writing 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