@@ -34,8 +34,58 @@ Installation
34
34
35
35
.. include :: /components/require_autoload.rst.inc
36
36
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
+
37
87
NativeClock
38
- -----------
88
+ ~~~~~~~~~~~
39
89
40
90
A clock service replaces creating a new ``DateTime `` or
41
91
``DateTimeImmutable `` object for the current time. Instead, you inject the
@@ -60,7 +110,7 @@ determine the current time::
60
110
}
61
111
62
112
MockClock
63
- ---------
113
+ ~~~~~~~~~
64
114
65
115
The ``MockClock `` is instantiated with a time and does not move forward on its own. The time is
66
116
fixed 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::
97
147
}
98
148
99
149
Monotonic Clock
100
- ---------------
150
+ ~~~~~~~~~~~~~~~
101
151
102
152
The ``MonotonicClock `` allows you to implement a precise stopwatch; depending on
103
153
the system up to nanosecond precision. It can be used to measure the elapsed
104
154
time between two calls without being affected by inconsistencies sometimes introduced
105
155
by the system clock, e.g. by updating it. Instead, it consistently increases time,
106
156
making it especially useful for measuring performance.
107
157
158
+ .. _clock_use-inside-a-service :
159
+
108
160
Using a Clock inside a Service
109
161
------------------------------
110
162
@@ -147,3 +199,5 @@ being in a month or another.
147
199
.. versionadded :: 6.3
148
200
149
201
The :class: `Symfony\\ Component\\ Clock\\ ClockAwareTrait ` was introduced in Symfony 6.3.
202
+
203
+ .. _`PSR-20` : https://www.php-fig.org/psr/psr-20/
0 commit comments