-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New PSR for ClockInterface #1224
Merged
Merged
Changes from 4 commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
31f93b6
Create Clock PSR
cseufert faa28e1
Added Clock PSR Meta
cseufert 0065811
added method to retreieve current timezone
cseufert d6c75b2
Updated timezone return types
cseufert 9974dfb
Update proposed/clock-meta.md to fix typos
cseufert 0c66162
Update proposed/clock-meta.md to fix typos
cseufert abb943b
Update proposed/clock-meta.md to fix typos
cseufert 0df6d63
Update proposed/clock-meta.md to fix typos
cseufert 072514e
Update purpose of document
cseufert 2bc8e21
DateTime is out
cseufert fed0313
Update clock.md fixed typo
cseufert 9c3d13f
Update clock.md update code formatting
cseufert 82f07eb
Update proposed/clock-meta.md
cseufert c8bcefe
Update proposed/clock-meta.md
cseufert 5415f5f
Simplify the ClockInterface
cseufert 3386929
Include example implementations
cseufert 4395fc3
Added names for sponsors and working group
cseufert d1bf6cf
Updated Working Group Members
cseufert 506b1fc
added Luis to WG
cseufert 380089c
Update proposed/clock.md
cseufert 5c17173
Update proposed/clock-meta.md
cseufert 650f6d2
Update proposed/clock-meta.md
cseufert 05f2fba
Update proposed/clock-meta.md
cseufert 18585b5
escape package names
cseufert 8d3e388
clean up relevant links format
cseufert 0ad441a
code formatted interface name
cseufert 078ec7b
Update proposed/clock-meta.md
cseufert 2e13cb5
Update proposed/clock-meta.md
cseufert a79da23
Update proposed/clock-meta.md
cseufert 0d7157c
Update proposed/clock-meta.md
cseufert 4f13a8e
Update proposed/clock.md
cseufert fa9323d
Update proposed/clock.md
cseufert 17f364a
Update proposed/clock.md
cseufert 8a701b2
Update proposed/clock-meta.md
cseufert 509163c
Update proposed/clock-meta.md
cseufert 8b34135
Update proposed/clock-meta.md
cseufert 6bd1916
Update clock-meta.md
cseufert 0338682
Added entrace vote link
cseufert 2734e16
Update proposed/clock-meta.md
cseufert c279903
Update proposed/clock-meta.md
cseufert 6ada868
Update proposed/clock-meta.md
cseufert 4b53d45
Update proposed/clock-meta.md
cseufert 92c4244
Update proposed/clock-meta.md
cseufert 3a8c4b1
Update proposed/clock-meta.md
cseufert 87c4002
Update proposed/clock-meta.md
cseufert 5e05b77
Update proposed/clock-meta.md
cseufert 9f8115b
Update proposed/clock-meta.md
cseufert 1ced885
Update proposed/clock.md
cseufert 12e1c1c
Update proposed/clock.md
cseufert 2de88c5
Update proposed/clock.md
cseufert 9401671
Re-ordered WG member list
cseufert 3688544
Update Reference example implemtations
cseufert 9ef42e5
Fix Order
cseufert 8826633
Removed timezones from usage patterns
cseufert 8e9aace
Update symfony details & added Chronos details
cseufert 66fac09
Update proposed/clock-meta.md
cseufert 81cb449
Update proposed/clock-meta.md
cseufert 3caffc3
Update proposed/clock-meta.md
cseufert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# PHPDoc Meta Document | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## 1. Summary | ||
|
||
The purpose of using the ClockInterface would allow mocking time in many situations where | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
you cant easily install PHP extenions or use hacks like re-declaring the time() funciton | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
in other namespaces. | ||
|
||
## 2. Why Bother? | ||
|
||
There are currently a few libraries that do provide the functionality on packagist, however | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
there is no interopability between these different libraries, as they ship with thier own | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clock interfaces. Symphony has a TimeMock library which uses namespace hacks to override the | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`time()`, `date()`, `microtime()`, etc functions, however this does not solve mocking calls to | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`new \DateTime()` | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Pros: | ||
|
||
* Consistent interface to get the current time | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Easy to mock the wall clock time for repeatablility. | ||
|
||
Cons: | ||
|
||
* Extra overhead and developer effor to get the current time, not as simple as | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
calling `time()` or `date()`. | ||
|
||
## 3. Scope | ||
|
||
### 3.1 Goals | ||
|
||
* Provide a simple way mockable way to read the current time | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Allow interopability between libraries when reading the clock | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### 3.2 Non-Goals | ||
|
||
* 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. | ||
* This PSR does not provide a reccomendation on how to handle timezones when retrieving the current time. | ||
|
||
## 4. Approaches | ||
|
||
### 4.1 Chosen Approach | ||
|
||
We have decided to formalize the existing practices, use by several other packages out in the wild. Some of the popular | ||
packages providing this functionality are: lcobucci/clock, kreait/clock, ergebnis/clock, and mangoweb/clock. Some | ||
providing interfaces, some relying on overloading a class to mock the current time. | ||
|
||
## 5. People | ||
|
||
### 5.1 Editor | ||
|
||
* Chris Seufert | ||
|
||
### 5.2 Sponsor | ||
|
||
* | ||
|
||
### 5.3 Working group members | ||
|
||
* | ||
|
||
## 6. Votes | ||
|
||
* | ||
|
||
## 7. Relevant Links | ||
|
||
* https://github.com/lcobucci/clock/blob/2.1.x/src/Clock.php | ||
* https://github.com/kreait/clock-php/blob/main/src/Clock.php | ||
* https://github.com/ergebnis/clock/blob/main/src/Clock.php | ||
* https://github.com/mangoweb-backend/clock/blob/master/src/Clock.php | ||
* https://github.com/icecave/chrono/blob/master/src/Clock/ClockInterface.php | ||
* https://github.com/Kdyby/DateTimeProvider/blob/master/src/DateTimeProviderInterface.php | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## 8. Past contributors | ||
|
||
Since this document stems from the work of a lot of people in previous years, we should recognize their effort: | ||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
* | ||
_**Note:** Order descending chronologically._ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,86 @@ | ||||||
Common Interface for Accessing the Clock | ||||||
======================================== | ||||||
|
||||||
This document describes a simple yet extensible interface for a cache item and | ||||||
a cache driver. | ||||||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", | ||||||
"SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be | ||||||
interpreted as described in [RFC 2119][]. | ||||||
|
||||||
The final implementations MAY decorate the objects with more | ||||||
functionality than the one proposed but they MUST implement the indicated | ||||||
interfaces/functionality first. | ||||||
|
||||||
[RFC 2119]: http://tools.ietf.org/html/rfc2119 | ||||||
|
||||||
# 1. Specification | ||||||
|
||||||
## 1.1 Introduction | ||||||
|
||||||
Creating a standard way of accessing the clock, would allow interopability | ||||||
during testing, when testing behavior that has timing based side affects. | ||||||
Common ways to get the current time include calling `\time()` or | ||||||
`new DateTimeImmutable('now')` however this makes mocking the current time | ||||||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
impossible in some situations. | ||||||
|
||||||
## 1.2 Definitions | ||||||
|
||||||
* **Clock** - The clock is able to read the current time and date. | ||||||
|
||||||
* **Timestamp** - The current time as an integer number of seconds since | ||||||
Jan 1, 1970 00:00:00 UTC. | ||||||
|
||||||
# 2. Interfaces | ||||||
|
||||||
## 2.1 ClockInterface | ||||||
|
||||||
The clock interface defines the most basic operations to read the current time and date from the clock. | ||||||
It MUST return the current time as a DateTimeImmutable, DateTime, timestamp integer and timestamp float, | ||||||
and MUST return the current time zone. | ||||||
|
||||||
~~~php | ||||||
<?php | ||||||
|
||||||
namespace Psr\Clock; | ||||||
|
||||||
interface ClockInterface | ||||||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
/** | ||||||
* Reads the current time as a unix timestamp | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* @return int The current time as a unix timestamp | ||||||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
*/ | ||||||
public function timestamp():int; | ||||||
|
||||||
/** | ||||||
* Reads the current time as a DateTimeImmutable Object | ||||||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
* | ||||||
* @return \DateTimeImmutable The current time as a DateTimeImmutable Object | ||||||
cseufert marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
*/ | ||||||
public function immutable():\DateTimeImmutable; | ||||||
|
||||||
|
||||||
/** | ||||||
* Reads the current time as a DateTime Object | ||||||
* | ||||||
* @return \DateTime The current time as a DateTime Object | ||||||
*/ | ||||||
public function datetime():\DateTime | ||||||
|
||||||
/** | ||||||
* Reads the current time as with microsecond resolution | ||||||
* | ||||||
* @return float The current time as a float with microsecond resolution | ||||||
*/ | ||||||
public function microtime():float | ||||||
|
||||||
/** | ||||||
* Retreieves the current time zone | ||||||
* | ||||||
* @return \DateTimeZone The current timezone as a DateTimeZone object | ||||||
*/ | ||||||
public function timezone():\DateTimeZone | ||||||
|
||||||
} | ||||||
~~~ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This article is not for PHPDoc.
Maybe replaced by "Clock Meta Document", like Container Meta Document.