@@ -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,49 @@ 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 is that it fully leverages the Clock component. Indeed, when creating a new
209
+ ``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 DateTimeImmutable
228
+ $immutable = new \DateTimeImmutable();
229
+ $fromImmutable = new DatePoint(reference: $immutable);
230
+
231
+ Also, ``DatePoint `` offers a stricter alternative to ``DateTimeImmutable `` by
232
+ having stricter error handling, as well as stricter return types. ``DatePoint ``
233
+ actually extends ``DateTimeImmutable ``, which means you can widely use it in
234
+ your application if you already type against ``DateTimeImmutable `` across your
235
+ code base.
236
+
237
+ .. versionadded :: 6.4
238
+
239
+ The :class: `Symfony\\ Component\\ Clock\\ DatePoint ` class was introduced
240
+ in Symfony 6.4.
241
+
242
+ .. _clock_writing-tests :
243
+
196
244
Writing Time-Sensitive Tests
197
245
----------------------------
198
246
0 commit comments