You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposed/clock-meta.md
+18-21Lines changed: 18 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,29 +2,29 @@
2
2
3
3
## 1. Summary
4
4
5
-
Getting the current time in applications is typically achieved using the `time()` or `microtime` functions, or by using a `new \DateTimeImmutable()` class.
5
+
Getting the current time in applications is typically achieved using the `\time()` or `\microtime` functions, or by using a `new \DateTimeImmutable()` class.
6
6
7
-
Due to the nature of time progression these methods cannot be used when predictable results are needed, such as during testing.
7
+
Due to the nature of time progression, these methods cannot be used when predictable results are needed, such as during testing.
8
8
9
-
This `ClockInterface` aims to provide a standard way to consume time that allows interoperability not only when consuming the "real" time but also when predictable results need to be available. This avoids the need to use PHP extensions for testing or redeclare the `time()` function in a local namespace.
9
+
This `ClockInterface` aims to provide a standard way to consume time that allows interoperability, not only when consuming the "real" time, but also when predictable results need to be available. This avoids the need to use PHP extensions for testing or redeclaring the `\time()` function in a local namespace.
10
10
11
11
## 2. Why Bother?
12
12
13
-
There are currently a few libraries that provide this functionality, however there is no interopability between these different libraries, as they ship with their own clock interfaces.
13
+
There are currently a few libraries that provide this functionality. However, there is no interoperability between these different libraries, as they ship with their own clock interfaces.
14
14
15
-
Symfony provides a package called `symfony/phpunit-bridge` that has a `Symfony\Bridge\PhpUnit\ClockMock` class, which allows mocking PHP's built-in time and date functions, however this does not solve mocking calls to `new \DateTimeImmutable()`. It also does not fully mock time when called from other libraries that rely on the system time.
15
+
Symfony provides a package called `symfony/phpunit-bridge` that has a `\Symfony\Bridge\PhpUnit\ClockMock` class, which allows mocking PHP's built-in time and date functions. However, this does not solve mocking calls to `new \DateTimeImmutable()`. It also does not fully mock time when called from other libraries that rely on the system time.
16
16
17
-
`Carbon\Carbon`, and its fork `Cake\Chronos\Chronos`, do provide mocking via a static `setTestNow()` method, but this provides no isolation and must be called again to stop mocking.
17
+
`\Carbon\Carbon`, and its fork `\Cake\Chronos\Chronos`, do provide mocking via a static `setTestNow()` method, but this provides no isolation and must be called again to stop mocking.
18
18
19
19
Pros:
20
20
21
21
* Consistent interface to get the current time;
22
-
* Easy to mock the wall clock time for repeatablility.
22
+
* Easy to mock the wall clock time for repeatability.
23
23
24
24
Cons:
25
25
26
-
* Extra overhead and developer effort to get the current time, not as simple as
27
-
calling `time()` or `date()`.
26
+
* Extra overhead and developer effort to get the current time;
27
+
* Not as simple as calling `\time()` or `\date()`.
28
28
29
29
## 3. Scope
30
30
@@ -35,17 +35,15 @@ calling `time()` or `date()`.
35
35
36
36
### 3.2 Non-Goals
37
37
38
-
* This PSR does not provide a recommendation on how and when to use the concepts
39
-
described in this document, so it is not a coding standard;
40
-
* This PSR does not provide a recommendation on how to handle timezones when
41
-
retrieving the current time. This is left up to the implementation.
38
+
* This PSR does not provide a recommendation on how and when to use the concepts described in this document, so it is not a coding standard;
39
+
* This PSR does not provide a recommendation on how to handle timezones when retrieving the current time. This is left up to the implementation.
42
40
* This PSR does not handle any scheduling methods like `sleep()` or `wait()` because such methods are not related to retrieving the current time.
43
41
44
42
## 4. Approaches
45
43
46
44
### 4.1 Chosen Approach
47
45
48
-
We have decided to formalize the existing practices used by several other packages. Some popular packages providing this functionality are:
46
+
We have decided to formalize the existing practices used by several other packages. Some popular packages providing this functionality are:
@@ -54,18 +52,17 @@ We have decided to formalize the existing practices used by several other packag
54
52
55
53
(This list is not exhaustive!)
56
54
57
-
Some of these provide interfaces and some rely on extending a clock class to mock the
58
-
current time.
55
+
Some of these provide interfaces and some rely on extending a clock class to mock the current time.
59
56
60
-
These implementations all provide a `now()` method which returns a `DateTimeImmutable` object. As the `DateTimeImmutable` object allows retrieving the Unix timestamp, by calling `getTimestamp()` or `format('u.U')`, this interface does not define any special methods to retrieve a Unix timestamp or any other time information that is not available from a `DateTimeImmutable` object.
57
+
These implementations all provide a `now()` method which returns a `\DateTimeImmutable` object. As the `\DateTimeImmutable` object allows retrieving the Unix timestamp, by calling `getTimestamp()` or `format('u.U')`, this interface does not define any special methods to retrieve a Unix timestamp or any other time information that is not available from a `\DateTimeImmutable` object.
61
58
62
59
### 4.2 Timezones
63
60
64
-
Time by now is defined by interaction of electromagnetic radiation with the excited states of certain atoms where the SI defines one second as the duration of 9192631770 cycles of radiation corresponding to the transition between two energy levels of the ground state of the caesium-133 atom at 0K. This means that retrieving the current time will always return the same time, no matter where it is observed. While the timezone defines *where* the time was observed it does not modify the actual "slice" of time.
61
+
Time by now is defined by interaction of electromagnetic radiation with the excited states of certain atoms, where the SI defines one second as the duration of 9192631770 cycles of radiation corresponding to the transition between two energy levels of the ground state of the caesium-133 atom at 0K. This means that retrieving the current time will always return the same time, no matter where it is observed. While the timezone defines *where* the time was observed, it does not modify the actual "slice" of time.
65
62
66
-
This means that for the sake of this PSR the timezone is considered an implementation detail of the interface.
63
+
This means that, for the sake of this PSR, the timezone is considered an implementation detail of the interface.
67
64
68
-
It is up to the implementation to make sure that the timezone is handled according to the business logic of the application. That is either by making sure that a call to `now()` will only return a `DateTimeImmutable` object with a known timezone (implicit contract) or by explicitly changing the timezone to be correct for the application. This can be done by calling `setTimezone()` to create a new `DateTimeImmutable` object with the given timezone.
65
+
It is up to the implementation to make sure that the timezone is handled according to the business logic of the application. That is either by making sure that a call to `now()` will only return a `\DateTimeImmutable` object with a known timezone (implicit contract) or by explicitly changing the timezone to be correct for the application. This can be done by calling `setTimezone()` to create a new `\DateTimeImmutable` object with the given timezone.
69
66
70
67
These actions are not defined in this interface.
71
68
@@ -135,7 +132,7 @@ final class FrozenClock implements \Psr\Clock\ClockInterface
135
132
136
133
This document stems from the work of many people in previous years, we recognize their effort:
0 commit comments