@@ -34,8 +34,58 @@ Installation
3434
3535 .. include :: /components/require_autoload.rst.inc
3636
37+ Usage
38+ -----
39+
40+ The :class: `Symfony\\ Component\\ Clock\\ Clock ` class returns the current time and
41+ allows to use any implementation of the interface described by `PSR-20 `_ as a global
42+ clock in your application::
43+
44+ use Symfony\Component\Clock\Clock;
45+ use Symfony\Component\Clock\MockClock;
46+
47+ // You can set a custom clock implementation, or use the NativeClock default one
48+ Clock::set(new MockClock());
49+
50+ // Then, you can get the clock instance
51+ $clock = Clock::get();
52+
53+ // Additionally, you can set a timezone
54+ $clock->withTimeZone('Europe/Paris');
55+
56+ // From here, you are able to get the current time
57+ $now = $clock->now();
58+
59+ // And also to sleep for an amount of time
60+ $clock->sleep(2.5);
61+
62+ The Clock component also provides the ``now() `` function::
63+
64+ use function Symfony\Component\Clock\now;
65+
66+ // Get the current time as a DateTimeImmutable instance
67+ $now = now();
68+
69+ .. tip ::
70+
71+ If you want to use the Clock component in your services, you may
72+ have a look at the section about
73+ :ref: `using a clock inside your services <clock_use-inside-a-service >`.
74+
75+ .. versionadded :: 6.3
76+
77+ The :class: `Symfony\\ Component\\ Clock\\ Clock ` class and ``now() `` function
78+ were introduced in Symfony 6.3.
79+
80+ Available Clocks Implementations
81+ --------------------------------
82+
83+ The Clock component provides many ready-to-use implementations of the
84+ :class: `Symfony\\ Component\\ Clock\\ ClockInterface `, which you can use
85+ as global clocks in your application depending on your needs.
86+
3787NativeClock
38- -----------
88+ ~~~~~~~~~~~
3989
4090A clock service replaces creating a new ``DateTime `` or
4191``DateTimeImmutable `` object for the current time. Instead, you inject the
@@ -60,7 +110,7 @@ determine the current time::
60110 }
61111
62112MockClock
63- ---------
113+ ~~~~~~~~~
64114
65115The ``MockClock `` is instantiated with a time and does not move forward on its own. The time is
66116fixed until ``sleep() `` or ``modify() `` are called. This gives you full control over what your code
@@ -97,14 +147,16 @@ is expired or not, by modifying the clock's time::
97147 }
98148
99149Monotonic Clock
100- ---------------
150+ ~~~~~~~~~~~~~~~
101151
102152The ``MonotonicClock `` allows you to implement a precise stopwatch; depending on
103153the system up to nanosecond precision. It can be used to measure the elapsed
104154time between two calls without being affected by inconsistencies sometimes introduced
105155by the system clock, e.g. by updating it. Instead, it consistently increases time,
106156making it especially useful for measuring performance.
107157
158+ .. _clock_use-inside-a-service :
159+
108160Using a Clock inside a Service
109161------------------------------
110162
@@ -205,3 +257,5 @@ control on your time-sensitive code's behavior.
205257.. versionadded :: 6.3
206258
207259 The :class: `Symfony\\ Component\\ Clock\\ Test\\ ClockSensitiveTrait ` was introduced in Symfony 6.3.
260+
261+ .. _`PSR-20` : https://www.php-fig.org/psr/psr-20/
0 commit comments