@@ -30,6 +30,8 @@ Installation
30
30
31
31
.. include :: /components/require_autoload.rst.inc
32
32
33
+ .. _clock_usage :
34
+
33
35
Usage
34
36
-----
35
37
@@ -60,7 +62,7 @@ The Clock component also provides the ``now()`` function::
60
62
61
63
use function Symfony\Component\Clock\now;
62
64
63
- // Get the current time as a DateTimeImmutable instance
65
+ // Get the current time as a DatePoint instance
64
66
$now = now();
65
67
66
68
The ``now() `` function takes an optional ``modifier `` argument
@@ -73,6 +75,9 @@ which will be applied to the current time::
73
75
You can use any string `accepted by the DateTime constructor `_.
74
76
75
77
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 >`.
76
81
77
82
.. versionadded :: 6.3
78
83
@@ -207,6 +212,54 @@ being in a month or another.
207
212
208
213
The :class: `Symfony\\ Component\\ Clock\\ ClockAwareTrait ` was introduced in Symfony 6.3.
209
214
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 being 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
+ Also, ``DatePoint `` offers a stricter alternative to ``DateTimeImmutable `` by
251
+ having stricter error handling, as well as stricter return types. ``DatePoint ``
252
+ actually extends ``DateTimeImmutable ``, which means you can widely use it in
253
+ your application if you already type against ``DateTimeImmutable `` across your
254
+ code base.
255
+
256
+ .. versionadded :: 6.4
257
+
258
+ The :class: `Symfony\\ Component\\ Clock\\ DatePoint ` class was introduced
259
+ in Symfony 6.4.
260
+
261
+ .. _clock_writing-tests :
262
+
210
263
Writing Time-Sensitive Tests
211
264
----------------------------
212
265
@@ -294,3 +347,4 @@ use them even if your project doesn't use PHP 8.3 yet.
294
347
.. _`accepted by the DateTime constructor` : https://www.php.net/manual/en/datetime.formats.php
295
348
.. _`PHP DateTime exceptions` : https://wiki.php.net/rfc/datetime-exceptions
296
349
.. _`symfony/polyfill-php83` : https://github.com/symfony/polyfill-php83
350
+ .. _`PHP 8.3's behavior` : https://wiki.php.net/rfc/datetime-exceptions
0 commit comments