3 namespace Drupal\Component\Datetime;
6 * Defines an interface for obtaining system time.
8 interface TimeInterface {
11 * Returns the timestamp for the current request.
13 * This method should be used to obtain the current system time at the start
14 * of the request. It will be the same value for the life of the request
15 * (even for long execution times).
17 * This method can replace instances of
19 * $request_time = $_SERVER['REQUEST_TIME'];
20 * $request_time = REQUEST_TIME;
21 * $request_time = $requestStack->getCurrentRequest()->server->get('REQUEST_TIME');
22 * $request_time = $request->server->get('REQUEST_TIME');
24 * and most instances of
30 * $request_time = \Drupal::time()->getRequestTime();
32 * or the equivalent using the injected service.
34 * Using the time service, rather than other methods, is especially important
35 * when creating tests, which require predictable timestamps.
40 * @see \Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()
41 * @see \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
42 * @see \Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()
44 public function getRequestTime();
47 * Returns the timestamp for the current request with microsecond precision.
49 * This method should be used to obtain the current system time, with
50 * microsecond precision, at the start of the request. It will be the same
51 * value for the life of the request (even for long execution times).
53 * This method can replace instances of
55 * $request_time_float = $_SERVER['REQUEST_TIME_FLOAT'];
56 * $request_time_float = $requestStack->getCurrentRequest()->server->get('REQUEST_TIME_FLOAT');
57 * $request_time_float = $request->server->get('REQUEST_TIME_FLOAT');
59 * and many instances of
61 * $microtime = microtime();
62 * $microtime = microtime(TRUE);
66 * $request_time = \Drupal::time()->getRequestMicroTime();
68 * or the equivalent using the injected service.
70 * Using the time service, rather than other methods, is especially important
71 * when creating tests, which require predictable timestamps.
74 * A Unix timestamp with a fractional portion.
76 * @see \Drupal\Component\Datetime\TimeInterface::getRequestTime()
77 * @see \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
78 * @see \Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()
80 public function getRequestMicroTime();
83 * Returns the current system time as an integer.
85 * This method should be used to obtain the current system time, at the time
86 * the method was called.
88 * This method can replace many instances of
94 * $request_time = \Drupal::time()->getCurrentTime();
96 * or the equivalent using the injected service.
98 * This method should only be used when the current system time is actually
99 * needed, such as with timers or time interval calculations. If only the
100 * time at the start of the request is needed,
101 * use TimeInterface::getRequestTime().
103 * Using the time service, rather than other methods, is especially important
104 * when creating tests, which require predictable timestamps.
109 * @see \Drupal\Component\Datetime\TimeInterface::getRequestTime()
110 * @see \Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()
111 * @see \Drupal\Component\Datetime\TimeInterface::getCurrentMicroTime()
113 public function getCurrentTime();
116 * Returns the current system time with microsecond precision.
118 * This method should be used to obtain the current system time, with
119 * microsecond precision, at the time the method was called.
121 * This method can replace many instances of
123 * $microtime = microtime();
124 * $microtime = microtime(TRUE);
128 * $request_time = \Drupal::time()->getCurrentMicroTime();
130 * or the equivalent using the injected service.
132 * This method should only be used when the current system time is actually
133 * needed, such as with timers or time interval calculations. If only the
134 * time at the start of the request and microsecond precision is needed,
135 * use TimeInterface::getRequestMicroTime().
137 * Using the time service, rather than other methods, is especially important
138 * when creating tests, which require predictable timestamps.
141 * A Unix timestamp with a fractional portion.
143 * @see \Drupal\Component\Datetime\TimeInterface::getRequestTime()
144 * @see \Drupal\Component\Datetime\TimeInterface::getRequestMicroTime()
145 * @see \Drupal\Component\Datetime\TimeInterface::getCurrentTime()
147 public function getCurrentMicroTime();