diff --git a/composer.json b/composer.json index 6127ff32..a1f1574d 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "vimeo/psalm": "^5.23.1", "php-standard-library/psalm-plugin": "^2.3.0", "php-coveralls/php-coveralls": "^2.7.0", - "roave/infection-static-analysis-plugin": "^1.35.0" + "roave/infection-static-analysis-plugin": "^1.35.0", + "psr/clock": "^1.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index a41893b1..1a92d65c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "97465cc59bebf30479efdae3608e548a", + "content-hash": "bbc684c64ae904ca971de3fd202e2d65", "packages": [ { "name": "revolt/event-loop", @@ -3326,6 +3326,54 @@ }, "time": "2021-02-03T23:26:27+00:00" }, + { + "name": "psr/clock", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/clock.git", + "reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/clock/zipball/e41a24703d4560fd0acb709162f73b8adfc3aa0d", + "reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d", + "shasum": "" + }, + "require": { + "php": "^7.0 || ^8.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Psr\\Clock\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for reading the clock.", + "homepage": "https://github.com/php-fig/clock", + "keywords": [ + "clock", + "now", + "psr", + "psr-20", + "time" + ], + "support": { + "issues": "https://github.com/php-fig/clock/issues", + "source": "https://github.com/php-fig/clock/tree/1.0.0" + }, + "time": "2022-11-25T14:36:26+00:00" + }, { "name": "psr/container", "version": "2.0.2", diff --git a/src/Psl/Clock/ClockInterface.php b/src/Psl/Clock/ClockInterface.php new file mode 100644 index 00000000..ecfd534f --- /dev/null +++ b/src/Psl/Clock/ClockInterface.php @@ -0,0 +1,12 @@ +dateTime = $dateTime; + } + + public function now(): DateTime\DateTimeInterface + { + return $this->dateTime; + } +} diff --git a/src/Psl/Clock/PsrClock.php b/src/Psl/Clock/PsrClock.php new file mode 100644 index 00000000..48155e42 --- /dev/null +++ b/src/Psl/Clock/PsrClock.php @@ -0,0 +1,30 @@ +psrClock->now(); + + $timestamp = DateTime\Timestamp::fromParts($nativeDateTime->getTimestamp()); + + $timezone = $nativeDateTime->getTimezone(); + invariant($timezone !== false, 'Could not get the timezone object.'); + + $timezone = DateTime\Timezone::from($timezone->getName()); + + return DateTime\DateTime::fromTimestamp($timestamp, $timezone); + } +} diff --git a/src/Psl/Clock/SystemClock.php b/src/Psl/Clock/SystemClock.php new file mode 100644 index 00000000..a080325b --- /dev/null +++ b/src/Psl/Clock/SystemClock.php @@ -0,0 +1,30 @@ +timezone); + } +} diff --git a/tests/unit/Clock/PsrClockTest.php b/tests/unit/Clock/PsrClockTest.php new file mode 100644 index 00000000..f89992cd --- /dev/null +++ b/tests/unit/Clock/PsrClockTest.php @@ -0,0 +1,44 @@ +now()->equalsIncludingTimezone( + DateTime\DateTime::parse( + '2020-01-01 00:00:00', + DateTime\FormatPattern::SqlDateTime, + DateTime\Timezone::AfricaTunis, + ) + ) + ); + } +} + +class FixedPsrClock implements PsrClockInterface +{ + public function __construct(private readonly \DateTimeImmutable $now) + { + } + + public function now(): \DateTimeImmutable + { + return $this->now; + } +}