@@ -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,10 +62,13 @@ 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
Later on this page you can learn how to use this clock in your services and tests.
69
+ When using the Clock component, you manipulate
70
+ :class: `Symfony\\ Component\\ Clock\\ DatePoint ` instances. You can learn more
71
+ about it in :ref: `the dedicated section <clock_date-point >`.
67
72
68
73
.. versionadded :: 6.3
69
74
@@ -193,6 +198,54 @@ being in a month or another.
193
198
194
199
The :class: `Symfony\\ Component\\ Clock\\ ClockAwareTrait ` was introduced in Symfony 6.3.
195
200
201
+ .. _clock_date-point :
202
+
203
+ The ``DatePoint `` Class
204
+ -----------------------
205
+
206
+ When using the Clock component, you manipulate instances of
207
+ :class: `Symfony\\ Component\\ Clock\\ DatePoint `. The main purpose of this
208
+ class is to leverage the Clock component to get the "current time". When
209
+ creating a new ``DatePoint `` object, the date and time are being fetched
210
+ from the :class: `Symfony\\ Component\\ Clock\\ Clock ` class. This means
211
+ that if you did any changes to the clock as stated in the
212
+ :ref: `usage section <clock_usage >`, it will be reflected when creating
213
+ a new ``DatePoint ``. This can be particularly useful when
214
+ :ref: `writing time-sensitive tests <clock_writing-tests >`.
215
+
216
+ Creating a new ``DatePoint `` is as straightforward as creating other date
217
+ objects::
218
+
219
+ use Symfony\Component\Clock\DatePoint;
220
+
221
+ // create a DatePoint set at the current date and time
222
+ $now = new DatePoint();
223
+
224
+ // you can specify a timezone
225
+ $withTimezone = new DatePoint(timezone: new \DateTimezone('UTC'));
226
+
227
+ // you can also create a DatePoint from a reference date
228
+ $referenceDate = new \DateTimeImmutable();
229
+ $relativeDate = new DatePoint('+1month', reference: $referenceDate);
230
+
231
+ ``DatePoint `` extends ``DateTimeImmutable `` so that you can use it seamlessly
232
+ everywhere a ``DateTimeImmutable `` is needed. In addition ``DatePoint `` offers
233
+ stricter return types and provides consistent error handling across versions of
234
+ PHP, thanks to polyfilling `PHP 8.3's behavior `_ on the topic.
235
+
236
+ Also, ``DatePoint `` offers a stricter alternative to ``DateTimeImmutable `` by
237
+ having stricter error handling, as well as stricter return types. ``DatePoint ``
238
+ actually extends ``DateTimeImmutable ``, which means you can widely use it in
239
+ your application if you already type against ``DateTimeImmutable `` across your
240
+ code base.
241
+
242
+ .. versionadded :: 6.4
243
+
244
+ The :class: `Symfony\\ Component\\ Clock\\ DatePoint ` class was introduced
245
+ in Symfony 6.4.
246
+
247
+ .. _clock_writing-tests :
248
+
196
249
Writing Time-Sensitive Tests
197
250
----------------------------
198
251
@@ -252,3 +305,4 @@ control on your time-sensitive code's behavior.
252
305
The :class: `Symfony\\ Component\\ Clock\\ Test\\ ClockSensitiveTrait ` was introduced in Symfony 6.3.
253
306
254
307
.. _`PSR-20` : https://www.php-fig.org/psr/psr-20/
308
+ .. _`PHP 8.3's behavior` : https://wiki.php.net/rfc/datetime-exceptions
0 commit comments