@@ -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
@@ -193,6 +195,47 @@ being in a month or another.
193
195
194
196
The :class: `Symfony\\ Component\\ Clock\\ ClockAwareTrait ` was introduced in Symfony 6.3.
195
197
198
+ The ``DatePoint `` Class
199
+ -----------------------
200
+
201
+ The Clock component comes with the
202
+ :class: `Symfony\\ Component\\ Clock\\ DatePoint ` class. The main purpose of this
203
+ class is to offer a stricter alternative to ``DateTimeImmutable `` by offering
204
+ a stricter error handling, as well as stricter return types. ``DatePoint ``
205
+ actually extends ``DateTimeImmutable ``, which means you can widely use it in
206
+ your application if you already use ``DateTimeImmutable `` across your code base.
207
+
208
+ Creating a new ``DatePoint `` is as straightforward as creating other date
209
+ objects::
210
+
211
+ use Symfony\Component\Clock\DatePoint;
212
+
213
+ // create a DatePoint set at the current date and time
214
+ $now = new DatePoint();
215
+
216
+ // you can specify a timezone
217
+ $withTimezone = new DatePoint(timezone: new \DateTimezone('UTC'));
218
+
219
+ // you can even create a DatePoint from a DateTimeImmutable
220
+ $immutable = new \DateTimeImmutable();
221
+ $fromImmutable = new DatePoint(reference: $immutable);
222
+
223
+ One big advantage of ``DatePoint `` is that it fully uses the Clock
224
+ component. Indeed, when creating a new ``DatePoint `` object, the date
225
+ and time are being fetched
226
+ from the :class: `Symfony\\ Component\\ Clock\\ Clock ` class. This means
227
+ that if you did any changes to the clock as stated in the
228
+ :ref: `usage section <clock_usage >`, it will be reflected when creating
229
+ a new ``DatePoint ``. This can be particularly useful when
230
+ :ref: `writing time-sensitive tests <clock_writing-tests >`.
231
+
232
+ .. versionadded :: 6.4
233
+
234
+ The :class: `Symfony\\ Component\\ Clock\\ DatePoint ` class was introduced
235
+ in Symfony 6.4.
236
+
237
+ .. _clock_writing-tests :
238
+
196
239
Writing Time-Sensitive Tests
197
240
----------------------------
198
241
0 commit comments