Skip to content

Commit 513d0ee

Browse files
authored
Merge pull request #1254 from ashnazg/clock
Grammar, Spelling, Formatting
2 parents 7d0c5d5 + e166096 commit 513d0ee

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

proposed/clock-meta.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22

33
## 1. Summary
44

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.
66

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.
88

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.
1010

1111
## 2. Why Bother?
1212

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.
1414

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.
1616

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.
1818

1919
Pros:
2020

2121
* 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.
2323

2424
Cons:
2525

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()`.
2828

2929
## 3. Scope
3030

@@ -35,17 +35,15 @@ calling `time()` or `date()`.
3535

3636
### 3.2 Non-Goals
3737

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.
4240
* This PSR does not handle any scheduling methods like `sleep()` or `wait()` because such methods are not related to retrieving the current time.
4341

4442
## 4. Approaches
4543

4644
### 4.1 Chosen Approach
4745

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:
4947

5048
* [`lcobucci/clock`](https://packagist.org/packages/lcobucci/clock)
5149
* [`kreait/clock`](https://packagist.org/packages/kreait/clock)
@@ -54,18 +52,17 @@ We have decided to formalize the existing practices used by several other packag
5452

5553
(This list is not exhaustive!)
5654

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.
5956

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.
6158

6259
### 4.2 Timezones
6360

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.
6562

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.
6764

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.
6966

7067
These actions are not defined in this interface.
7168

@@ -135,7 +132,7 @@ final class FrozenClock implements \Psr\Clock\ClockInterface
135132

136133
This document stems from the work of many people in previous years, we recognize their effort:
137134

138-
*
135+
*
139136
_**Note:** Order descending chronologically._
140137

141138
## 9. FAQ

proposed/clock.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
88
interpreted as described in [RFC 2119][].
99

1010
The final implementations MAY decorate the objects with more
11-
functionality than the one proposed but they MUST implement the indicated
11+
functionality than the one proposed, but they MUST implement the indicated
1212
interfaces/functionality first.
1313

1414
[RFC 2119]: http://tools.ietf.org/html/rfc2119
@@ -17,10 +17,10 @@ interfaces/functionality first.
1717

1818
## 1.1 Introduction
1919

20-
Creating a standard way of accessing the clock would allow interopability
21-
during testing, when testing behavior that has timing based side effects.
20+
Creating a standard way of accessing the clock would allow interoperability
21+
during testing, when testing behavior that has timing-based side effects.
2222
Common ways to get the current time include calling `\time()` or
23-
`new DateTimeImmutable('now')`. However, this makes mocking the current time
23+
`new \DateTimeImmutable('now')`. However, this makes mocking the current time
2424
impossible in some situations.
2525

2626
## 1.2 Definitions
@@ -32,11 +32,9 @@ Jan 1, 1970 00:00:00 UTC.
3232

3333
### 1.3 Usage
3434

35-
There are some common usage patterns, which are outlined below:
36-
3735
**Get the current timestamp**
3836

39-
This should be done by using the `getTimestamp()` method on the returned `\DateTimeImmutable` like so:
37+
This should be done by using the `getTimestamp()` method on the returned `\DateTimeImmutable`:
4038
```php
4139
$timestamp = $clock->now()->getTimestamp();
4240
```
@@ -45,10 +43,10 @@ $timestamp = $clock->now()->getTimestamp();
4543

4644
## 2.1 ClockInterface
4745

48-
The clock interface defines the most basic operation to read the current time and date from the clock.
49-
It MUST return the time as a `DateTimeImmutable`.
46+
The clock interface defines the most basic operations to read the current time and date from the clock.
47+
It MUST return the time as a `\DateTimeImmutable`.
5048

51-
~~~php
49+
```php
5250
<?php
5351

5452
namespace Psr\Clock;
@@ -61,4 +59,4 @@ interface ClockInterface
6159
public function now(): \DateTimeImmutable;
6260

6361
}
64-
~~~
62+
```

0 commit comments

Comments
 (0)