Skip to content

Commit b8f7b6d

Browse files
[Clock] Add Clock class and now() function documentation
1 parent efb857d commit b8f7b6d

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

components/clock.rst

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3787
NativeClock
38-
-----------
88+
~~~~~~~~~~~
3989

4090
A 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

62112
MockClock
63-
---------
113+
~~~~~~~~~
64114

65115
The ``MockClock`` is instantiated with a time and does not move forward on its own. The time is
66116
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::
97147
}
98148

99149
Monotonic Clock
100-
---------------
150+
~~~~~~~~~~~~~~~
101151

102152
The ``MonotonicClock`` allows you to implement a precise stopwatch; depending on
103153
the system up to nanosecond precision. It can be used to measure the elapsed
104154
time between two calls without being affected by inconsistencies sometimes introduced
105155
by the system clock, e.g. by updating it. Instead, it consistently increases time,
106156
making it especially useful for measuring performance.
107157

158+
.. _clock_use-inside-a-service:
159+
108160
Using a Clock inside a Service
109161
------------------------------
110162

@@ -147,3 +199,5 @@ being in a month or another.
147199
.. versionadded:: 6.3
148200

149201
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

Comments
 (0)