-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 10c02d2
Showing
28 changed files
with
986 additions
and
0 deletions.
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,21 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.{neon,neon.dist}] | ||
indent_style = tab | ||
|
||
[*.{yml,yaml}] | ||
indent_size = 2 |
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,9 @@ | ||
/examples/ export-ignore | ||
/tests/ export-ignore | ||
|
||
/.editorconfig export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/phpstan.neon.dist export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/psalm.xml.dist export-ignore |
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,54 @@ | ||
name: Tests | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- 'main' | ||
|
||
jobs: | ||
tests: | ||
name: PHP ${{ matrix.php }} | ||
runs-on: ubuntu-20.04 | ||
|
||
strategy: | ||
matrix: | ||
php: | ||
- "7.4" | ||
- "8.0" | ||
- "8.1" | ||
dependencies: | ||
- "lowest" | ||
- "highest" | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
tools: composer:v2 | ||
coverage: xdebug | ||
env: | ||
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- uses: "ramsey/composer-install@v1" | ||
with: | ||
dependency-versions: "${{ matrix.dependencies }}" | ||
composer-options: "${{ matrix.composer-options }}" | ||
|
||
- name: Setup problem matchers for PHP | ||
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" | ||
|
||
- name: Run PHPStan | ||
run: vendor/bin/phpstan analyse --error-format=github | ||
|
||
- name: Run Psalm | ||
run: vendor/bin/psalm --output-format=github | ||
|
||
- name: Setup Problem Matchers for PHPUnit | ||
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" | ||
|
||
- name: Run PHPUnit | ||
run: vendor/bin/phpunit --testdox --coverage-text |
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,7 @@ | ||
/.build/ | ||
/vendor/ | ||
|
||
/composer.lock | ||
/phpstan.neon | ||
/phpunit.xml | ||
/psalm.xml |
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,8 @@ | ||
# CHANGELOG | ||
|
||
## [Unreleased] | ||
|
||
## 1.0.0 - 2021-12-10 | ||
Initial Release | ||
|
||
[Unreleased]: https://github.com/beste/clock/compare/1.0.0...main |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Jérôme Gamez, https://github.com/beste <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,146 @@ | ||
# Clock | ||
|
||
[![Current version](https://img.shields.io/packagist/v/beste/clock.svg?logo=composer)](https://packagist.org/packages/beste/clock) | ||
[![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/beste/clock)](https://packagist.org/packages/beste/clock) | ||
[![Tests](https://github.com/beste/clock/actions/workflows/tests.yml/badge.svg)](https://github.com/beste/clock/actions/workflows/tests.yml) | ||
|
||
A collection of Clock implementations. | ||
|
||
## Table of Contents | ||
|
||
- [Installation](#installation) | ||
- [Clocks](#clocks) | ||
- [`SystemClock`](#systemclock) - Time, as your computer (k)nows it | ||
- [`LocalizedClock`](#localizedclock) - A clock in a(nother) time zone | ||
- [`UTCClock`](#utcclock) - The clock that you should™ use | ||
- [`FrozenClock`](#frozenclock) - A clock that stopped moving (perfect for tests) | ||
- [`MinuteClock`](#minuteclock) - Who cares about seconds or even less? | ||
- [Running Tests](#running-tests) | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require beste/clock | ||
``` | ||
|
||
## Clocks | ||
|
||
### `SystemClock` | ||
|
||
A System Clock will return a time just as if you would use `new DateTimeImmutable()`. The time zone of the returned | ||
value is determined by the clock's environment, for example by the time zone that has been configured in your | ||
application, by a previously used `date_default_timezone_set()` or by the value of `date.timezone` in the | ||
`php.ini`. If none of these are explicitly set, it uses the `UTC` timezone. | ||
|
||
```php | ||
# examples/system_clock.php | ||
|
||
use Beste\Clock\SystemClock; | ||
|
||
$clock = SystemClock::create(); | ||
|
||
printf("On your system, the current date and time is %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); | ||
|
||
date_default_timezone_set('America/Los_Angeles'); | ||
|
||
printf("Now it's %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); | ||
|
||
date_default_timezone_set('Europe/Berlin'); | ||
|
||
printf("Now it's %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); | ||
``` | ||
|
||
### `LocalizedClock` | ||
|
||
A localized clock is aware of the time zone in which it is located. While the time zone of the `SystemClock` is | ||
determined from the environment (your PHP configuration), this clock uses the time zone that you initialize it with. | ||
|
||
```php | ||
# examples/localized_clock.php | ||
|
||
use Beste\Clock\LocalizedClock; | ||
|
||
$berlin = LocalizedClock::in('Europe/Berlin'); | ||
$denver = LocalizedClock::in(new DateTimeZone('America/Denver')); | ||
|
||
printf("Berlin: %s\n", $berlin->now()->format('Y-m-d H:i:s T (P)')); | ||
printf("Denver: %s\n", $denver->now()->format('Y-m-d H:i:s T (P)')); | ||
``` | ||
|
||
### `UTCClock` | ||
|
||
`UTC` is the abbreviation for [Coordinated Universal Time](https://en.wikipedia.org/wiki/Coordinated_Universal_Time) | ||
and a special kind of time zone that is not affected by daylight saving time. It is commonly used for the communication | ||
of time across different systems (e.g. between your PHP application and a database, or between a backend | ||
and a frontend). An `UTCClock` instance behaves exactly the same as an instance of `LocalizedClock::in('UTC')`. | ||
|
||
```php | ||
# examples/utc_clock.php | ||
|
||
use Beste\Clock\UTCClock; | ||
|
||
$clock = UTCClock::create(); | ||
|
||
$anotherTimeZone = 'Africa/Casablanca'; | ||
|
||
date_default_timezone_set($anotherTimeZone); | ||
|
||
printf("The system time zone is %s.\n", $anotherTimeZone); | ||
printf("The clock's time zone is %s.\n", $clock->now()->getTimezone()->getName()); | ||
``` | ||
|
||
### `FrozenClock` | ||
|
||
A frozen clock doesn't move - the time we set it with will stay the same... unless we change it. That makes the | ||
frozen clock perfect for testing the behaviour of your time-based use cases, for example in Unit Tests. | ||
|
||
```php | ||
# examples/frozen_clock.php | ||
|
||
use Beste\Clock\FrozenClock; | ||
use Beste\Clock\SystemClock; | ||
|
||
$frozenClock = FrozenClock::withNowFrom(SystemClock::create()); | ||
|
||
printf("\nThe clock is frozen at %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); | ||
printf("\nLet's wait a second…"); | ||
sleep(1); | ||
printf("\nIt's one second later, but the clock is still frozen at %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); | ||
|
||
$frozenClock->setTo($frozenClock->now()->modify('-5 minutes')); | ||
printf("\nAfter turning back the clock 5 minutes, it's %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); | ||
``` | ||
|
||
### `MinuteClock` | ||
|
||
In some cases, microseconds, milliseconds, or even seconds are too precise for some use cases - sometimes it's just | ||
enough if something happened in the same minute. Using the minute | ||
|
||
```php | ||
# examples/minute_clock.php | ||
|
||
use Beste\Clock\FrozenClock; | ||
use Beste\Clock\MinuteClock; | ||
|
||
$frozenClock = FrozenClock::at(new DateTimeImmutable('01:23:45')); | ||
$clock = MinuteClock::wrapping($frozenClock); | ||
|
||
printf("For %s, the minute clock returns %s\n", | ||
$frozenClock->now()->format('H:i:s'), | ||
$clock->now()->format('H:i:s') | ||
); | ||
|
||
$frozenClock->setTo($frozenClock->now()->modify('+10 seconds')); // 01:23:55 | ||
|
||
printf("For %s, the minute clock still returns %s\n", | ||
$frozenClock->now()->format('H:i:s'), | ||
$clock->now()->format('H:i:s') | ||
); | ||
|
||
``` | ||
|
||
## Running tests | ||
|
||
```shell | ||
composer test | ||
``` |
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,55 @@ | ||
{ | ||
"name": "beste/clock", | ||
"type": "library", | ||
"description": "A collection of Clock implementations", | ||
"keywords": ["clock", "clock-interface", "psr20", "psr-20"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jérôme Gamez", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "~7.4.0 || ~8.0.0 || ~8.1.0" | ||
}, | ||
"require-dev": { | ||
"phpstan/extension-installer": "^1.1", | ||
"phpstan/phpstan": "^1.0", | ||
"phpstan/phpstan-phpunit": "^1.0", | ||
"phpstan/phpstan-strict-rules": "^1.0", | ||
"phpunit/phpunit": "^9.5.10", | ||
"psalm/plugin-phpunit": "^0.16.0", | ||
"vimeo/psalm": "^4.15" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Beste\\Clock\\": "src/Clock" | ||
}, | ||
"files": [ | ||
"src/psr-polyfill.php", | ||
"src/Clock.php" | ||
] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Beste\\Clock\\Tests\\": "tests/Clock" | ||
} | ||
}, | ||
"provide": { | ||
"psr/clock-implementation": "1.0" | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"scripts": { | ||
"test": [ | ||
"@phpstan", | ||
"@psalm", | ||
"@phpunit" | ||
], | ||
"phpunit": "vendor/bin/phpunit", | ||
"phpstan": "vendor/bin/phpstan analyse", | ||
"psalm": "vendor/bin/psalm" | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
use Beste\Clock\FrozenClock; | ||
use Beste\Clock\SystemClock; | ||
|
||
$frozenClock = FrozenClock::withNowFrom(SystemClock::create()); | ||
|
||
printf("\nThe clock is frozen at %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); | ||
printf("\nLet's wait a second…"); | ||
sleep(1); | ||
printf("\nIt's one second later, but the clock is still frozen at %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); | ||
|
||
$frozenClock->setTo($frozenClock->now()->modify('-5 minutes')); | ||
printf("\nAfter turning back the clock 5 minutes, it's %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); |
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,11 @@ | ||
<?php | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
use Beste\Clock\LocalizedClock; | ||
|
||
$berlin = LocalizedClock::in('Europe/Berlin'); | ||
$denver = LocalizedClock::in(new DateTimeZone('America/Denver')); | ||
|
||
printf("Berlin: %s\n", $berlin->now()->format('Y-m-d H:i:s T (P)')); | ||
printf("Denver: %s\n", $denver->now()->format('Y-m-d H:i:s T (P)')); |
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,21 @@ | ||
<?php | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
use Beste\Clock\FrozenClock; | ||
use Beste\Clock\MinuteClock; | ||
|
||
$frozenClock = FrozenClock::at(new DateTimeImmutable('01:23:45')); | ||
$clock = MinuteClock::wrapping($frozenClock); | ||
|
||
printf("For %s, the minute clock returns %s\n", | ||
$frozenClock->now()->format('H:i:s'), | ||
$clock->now()->format('H:i:s') | ||
); | ||
|
||
$frozenClock->setTo($frozenClock->now()->modify('+10 seconds')); // 01:23:55 | ||
|
||
printf("For %s, the minute clock still returns %s\n", | ||
$frozenClock->now()->format('H:i:s'), | ||
$clock->now()->format('H:i:s') | ||
); |
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,17 @@ | ||
<?php | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
use Beste\Clock\SystemClock; | ||
|
||
$clock = SystemClock::create(); | ||
|
||
printf("On your system, the current date and time is %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); | ||
|
||
date_default_timezone_set('America/Los_Angeles'); | ||
|
||
printf("Now it's %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); | ||
|
||
date_default_timezone_set('Europe/Berlin'); | ||
|
||
printf("Now it's %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); |
Oops, something went wrong.