@@ -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,53 @@ 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
+ 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
+
210
262
Writing Time-Sensitive Tests
211
263
----------------------------
212
264
@@ -294,3 +346,4 @@ use them even if your project doesn't use PHP 8.3 yet.
294
346
.. _`accepted by the DateTime constructor` : https://www.php.net/manual/en/datetime.formats.php
295
347
.. _`PHP DateTime exceptions` : https://wiki.php.net/rfc/datetime-exceptions
296
348
.. _`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