-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Clock] A new component to decouple applications from the system clock #46715
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
This file contains hidden or 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,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains hidden or 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 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.2 | ||
--- | ||
|
||
* Add the component |
This file contains hidden or 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,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Clock; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface ClockInterface | ||
{ | ||
public function now(): \DateTimeImmutable; | ||
|
||
public function sleep(float|int $seconds): void; | ||
|
||
public function withTimeZone(\DateTimeZone|string $timezone): static; | ||
} |
This file contains hidden or 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,19 @@ | ||
Copyright (c) 2022 Fabien Potencier | ||
|
||
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 hidden or 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,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Clock; | ||
|
||
/** | ||
* A clock that always returns the same date, suitable for testing time-sensitive logic. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
final class MockClock implements ClockInterface | ||
{ | ||
private \DateTimeImmutable $now; | ||
|
||
public function __construct(\DateTimeImmutable|string $now = 'now', \DateTimeZone|string $timezone = null) | ||
{ | ||
if (\is_string($timezone)) { | ||
$timezone = new \DateTimeZone($timezone); | ||
} | ||
|
||
if (\is_string($now)) { | ||
$now = new \DateTimeImmutable($now, $timezone ?? new \DateTimeZone('UTC')); | ||
} | ||
|
||
$this->now = null !== $timezone ? $now->setTimezone($timezone) : $now; | ||
} | ||
|
||
public function now(): \DateTimeImmutable | ||
{ | ||
return clone $this->now; | ||
} | ||
|
||
public function sleep(float|int $seconds): void | ||
{ | ||
$now = explode('.', $this->now->format('U.u')); | ||
|
||
if (0 < $s = (int) $seconds) { | ||
$now[0] += $s; | ||
} | ||
|
||
if (0 < ($us = $seconds - $s) && 1E6 <= $now[1] += $us * 1E6) { | ||
++$now[0]; | ||
$now[1] -= 1E6; | ||
} | ||
|
||
$datetime = '@'.$now[0].'.'.str_pad($now[1], 6, '0', \STR_PAD_LEFT); | ||
$timezone = $this->now->getTimezone(); | ||
|
||
$this->now = (new \DateTimeImmutable($datetime, $timezone))->setTimezone($timezone); | ||
} | ||
|
||
public function withTimeZone(\DateTimeZone|string $timezone): static | ||
{ | ||
$clone = clone $this; | ||
$clone->now = $clone->now->setTimezone(\is_string($timezone) ? new \DateTimeZone($timezone) : $timezone); | ||
|
||
return $clone; | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Clock; | ||
|
||
/** | ||
* A monotonic clock suitable for performance profiling. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
final class MonotonicClock implements ClockInterface | ||
{ | ||
private int $sOffset; | ||
private int $usOffset; | ||
private \DateTimeZone $timezone; | ||
|
||
public function __construct(\DateTimeZone|string $timezone = null) | ||
{ | ||
if (false === $offset = hrtime()) { | ||
throw new \RuntimeException('hrtime() returned false: the runtime environment does not provide access to a monotonic timer.'); | ||
} | ||
|
||
$time = gettimeofday(); | ||
$this->sOffset = $time['sec'] - $offset[0]; | ||
$this->usOffset = $time['usec'] - (int) ($offset[1] / 1000); | ||
|
||
if (\is_string($timezone ??= date_default_timezone_get())) { | ||
$this->timezone = new \DateTimeZone($timezone); | ||
} else { | ||
$this->timezone = $timezone; | ||
} | ||
} | ||
|
||
public function now(): \DateTimeImmutable | ||
{ | ||
[$s, $us] = hrtime(); | ||
|
||
if (1000000 <= $us = (int) ($us / 1000) + $this->usOffset) { | ||
++$s; | ||
$us -= 1000000; | ||
} elseif (0 > $us) { | ||
--$s; | ||
$us += 1000000; | ||
} | ||
|
||
if (6 !== \strlen($now = (string) $us)) { | ||
$now = str_pad($now, 6, '0', \STR_PAD_LEFT); | ||
} | ||
|
||
$now = '@'.($s + $this->sOffset).'.'.$now; | ||
|
||
return (new \DateTimeImmutable($now, $this->timezone))->setTimezone($this->timezone); | ||
} | ||
|
||
public function sleep(float|int $seconds): void | ||
{ | ||
if (0 < $s = (int) $seconds) { | ||
sleep($s); | ||
} | ||
|
||
if (0 < $us = $seconds - $s) { | ||
usleep($us * 1E6); | ||
} | ||
} | ||
|
||
public function withTimeZone(\DateTimeZone|string $timezone): static | ||
{ | ||
$clone = clone $this; | ||
$clone->timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone; | ||
|
||
return $clone; | ||
} | ||
} |
This file contains hidden or 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 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Clock; | ||
|
||
/** | ||
* A clock that relies the system time. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
final class NativeClock implements ClockInterface | ||
{ | ||
private \DateTimeZone $timezone; | ||
|
||
public function __construct(\DateTimeZone|string $timezone = null) | ||
{ | ||
if (\is_string($timezone ??= date_default_timezone_get())) { | ||
$this->timezone = new \DateTimeZone($timezone); | ||
} else { | ||
$this->timezone = $timezone; | ||
} | ||
} | ||
|
||
public function now(): \DateTimeImmutable | ||
{ | ||
return new \DateTimeImmutable('now', $this->timezone); | ||
} | ||
|
||
public function sleep(float|int $seconds): void | ||
{ | ||
if (0 < $s = (int) $seconds) { | ||
sleep($s); | ||
} | ||
|
||
if (0 < $us = $seconds - $s) { | ||
usleep($us * 1E6); | ||
} | ||
} | ||
|
||
public function withTimeZone(\DateTimeZone|string $timezone): static | ||
{ | ||
$clone = clone $this; | ||
$clone->timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone; | ||
|
||
return $clone; | ||
} | ||
} |
This file contains hidden or 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,47 @@ | ||
Clock Component | ||
=============== | ||
|
||
Symfony Clock decouples applications from the system clock. | ||
|
||
Getting Started | ||
--------------- | ||
|
||
``` | ||
$ composer require symfony/clock | ||
``` | ||
|
||
```php | ||
use Symfony\Component\Clock\NativeClock; | ||
use Symfony\Component\Clock\ClockInterface; | ||
|
||
class MyClockSensitiveClass | ||
{ | ||
public function __construct( | ||
private ClockInterface $clock, | ||
) { | ||
// Only if you need to force a timezone: | ||
//$this->clock = $clock->withTimeZone('UTC'); | ||
} | ||
|
||
public function doSomething() | ||
{ | ||
$now = $this->clock->now(); | ||
// [...] do something with $now, which is a \DateTimeImmutable object | ||
|
||
$this->clock->sleep(2.5); // Pause execution for 2.5 seconds | ||
} | ||
} | ||
|
||
$clock = new NativeClock(); | ||
$service = new MyClockSensitiveClass($clock); | ||
$service->doSomething(); | ||
``` | ||
|
||
Resources | ||
--------- | ||
|
||
* [Documentation](https://symfony.com/doc/current/clock.html) | ||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.