Skip to content
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 58 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
31f93b6
Create Clock PSR
cseufert Mar 4, 2021
faa28e1
Added Clock PSR Meta
cseufert Mar 4, 2021
0065811
added method to retreieve current timezone
cseufert Mar 4, 2021
d6c75b2
Updated timezone return types
cseufert Mar 4, 2021
9974dfb
Update proposed/clock-meta.md to fix typos
cseufert Mar 4, 2021
0c66162
Update proposed/clock-meta.md to fix typos
cseufert Mar 4, 2021
abb943b
Update proposed/clock-meta.md to fix typos
cseufert Mar 4, 2021
0df6d63
Update proposed/clock-meta.md to fix typos
cseufert Mar 4, 2021
072514e
Update purpose of document
cseufert Mar 4, 2021
2bc8e21
DateTime is out
cseufert Mar 4, 2021
fed0313
Update clock.md fixed typo
cseufert Mar 4, 2021
9c3d13f
Update clock.md update code formatting
cseufert Mar 4, 2021
82f07eb
Update proposed/clock-meta.md
cseufert Mar 8, 2021
c8bcefe
Update proposed/clock-meta.md
cseufert Mar 8, 2021
5415f5f
Simplify the ClockInterface
cseufert Mar 8, 2021
3386929
Include example implementations
cseufert Mar 8, 2021
4395fc3
Added names for sponsors and working group
cseufert Mar 9, 2021
d1bf6cf
Updated Working Group Members
cseufert Mar 10, 2021
506b1fc
added Luis to WG
cseufert Mar 11, 2021
380089c
Update proposed/clock.md
cseufert Mar 11, 2021
5c17173
Update proposed/clock-meta.md
cseufert Mar 11, 2021
650f6d2
Update proposed/clock-meta.md
cseufert Mar 15, 2021
05f2fba
Update proposed/clock-meta.md
cseufert Mar 16, 2021
18585b5
escape package names
cseufert Mar 18, 2021
8d3e388
clean up relevant links format
cseufert Mar 18, 2021
0ad441a
code formatted interface name
cseufert Mar 18, 2021
078ec7b
Update proposed/clock-meta.md
cseufert Mar 18, 2021
2e13cb5
Update proposed/clock-meta.md
cseufert Mar 18, 2021
a79da23
Update proposed/clock-meta.md
cseufert Mar 18, 2021
0d7157c
Update proposed/clock-meta.md
cseufert Mar 18, 2021
4f13a8e
Update proposed/clock.md
cseufert Mar 18, 2021
fa9323d
Update proposed/clock.md
cseufert Mar 18, 2021
17f364a
Update proposed/clock.md
cseufert Mar 18, 2021
8a701b2
Update proposed/clock-meta.md
cseufert Mar 18, 2021
509163c
Update proposed/clock-meta.md
cseufert Mar 18, 2021
8b34135
Update proposed/clock-meta.md
cseufert Mar 18, 2021
6bd1916
Update clock-meta.md
cseufert Mar 18, 2021
0338682
Added entrace vote link
cseufert Mar 22, 2021
2734e16
Update proposed/clock-meta.md
cseufert Mar 24, 2021
c279903
Update proposed/clock-meta.md
cseufert Mar 24, 2021
6ada868
Update proposed/clock-meta.md
cseufert Mar 24, 2021
4b53d45
Update proposed/clock-meta.md
cseufert Mar 24, 2021
92c4244
Update proposed/clock-meta.md
cseufert Mar 24, 2021
3a8c4b1
Update proposed/clock-meta.md
cseufert Mar 24, 2021
87c4002
Update proposed/clock-meta.md
cseufert Mar 24, 2021
5e05b77
Update proposed/clock-meta.md
cseufert Mar 24, 2021
9f8115b
Update proposed/clock-meta.md
cseufert Mar 24, 2021
1ced885
Update proposed/clock.md
cseufert Mar 24, 2021
12e1c1c
Update proposed/clock.md
cseufert Mar 24, 2021
2de88c5
Update proposed/clock.md
cseufert Mar 24, 2021
9401671
Re-ordered WG member list
cseufert Mar 24, 2021
3688544
Update Reference example implemtations
cseufert Mar 24, 2021
9ef42e5
Fix Order
cseufert Mar 24, 2021
8826633
Removed timezones from usage patterns
cseufert Mar 24, 2021
8e9aace
Update symfony details & added Chronos details
cseufert Mar 24, 2021
66fac09
Update proposed/clock-meta.md
cseufert Mar 24, 2021
81cb449
Update proposed/clock-meta.md
cseufert Mar 24, 2021
3caffc3
Update proposed/clock-meta.md
cseufert Mar 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions proposed/clock-meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# PHPDoc Meta Document
Copy link

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.

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

86 changes: 86 additions & 0 deletions proposed/clock.md
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Reads the current time as a unix timestamp
* Returns the current time as a unix timestamp

*
* @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

}
~~~