-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from FastyBird/feature/use-interface
Refactored to interface + separate services for easy mocking
- Loading branch information
Showing
8 changed files
with
154 additions
and
47 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
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,25 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* Clock.php | ||
* | ||
* @license More in LICENSE.md | ||
* @copyright https://www.fastybird.com | ||
* @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
* @package FastyBird:DateTimeFactory! | ||
* @subpackage common | ||
* @since 0.1.0 | ||
* | ||
* @date 08.03.20 | ||
*/ | ||
|
||
namespace FastyBird\DateTimeFactory; | ||
|
||
use DateTimeInterface; | ||
|
||
interface Clock | ||
{ | ||
|
||
public function getNow(): DateTimeInterface; | ||
|
||
} |
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
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,71 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* FrozenClock.php | ||
* | ||
* @license More in LICENSE.md | ||
* @copyright https://www.fastybird.com | ||
* @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
* @package FastyBird:DateTimeFactory! | ||
* @subpackage common | ||
* @since 0.1.0 | ||
* | ||
* @date 29.08.24 | ||
*/ | ||
|
||
namespace FastyBird\DateTimeFactory; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use DateTimeZone; | ||
use Nette; | ||
use function assert; | ||
use function date_default_timezone_get; | ||
use function floor; | ||
use function round; | ||
|
||
class FrozenClock implements Clock | ||
{ | ||
|
||
use Nette\SmartObject; | ||
|
||
private DateTimeImmutable $dt; | ||
|
||
public function __construct(float|DateTimeInterface $timestamp, DateTimeZone|null $timeZone = null) | ||
{ | ||
if ($timestamp instanceof DateTime) { | ||
$dt = DateTimeImmutable::createFromMutable($timestamp); | ||
|
||
} elseif ($timestamp instanceof DateTimeImmutable) { | ||
$dt = $timestamp; | ||
|
||
} else { | ||
[$seconds, $microseconds] = $this->getParts($timestamp); | ||
|
||
$dt = DateTimeImmutable::createFromFormat('U', (string) $seconds); | ||
assert($dt instanceof DateTimeImmutable); | ||
|
||
$dt = $dt->modify("+$microseconds microsecond"); | ||
} | ||
|
||
$this->dt = $dt->setTimezone($timeZone ?? new DateTimeZone(date_default_timezone_get())); | ||
} | ||
|
||
public function getNow(): DateTimeInterface | ||
{ | ||
return clone $this->dt; | ||
} | ||
|
||
/** | ||
* @return array{float, float} | ||
*/ | ||
private function getParts(float $seconds): array | ||
{ | ||
$wholeSeconds = floor($seconds); | ||
$microseconds = round(($seconds - $wholeSeconds) * 1E6); | ||
|
||
return [$wholeSeconds, $microseconds]; | ||
} | ||
|
||
} |
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
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
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
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
# | ||
# DateTime factory DI configuration | ||
# | ||
# @license More in LICENSE.md | ||
# @copyright https://www.fastybird.com | ||
# @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
# @package FastyBird:DateTimeFactory! | ||
# @subpackage config | ||
# @since 0.1.0 | ||
# @license More in LICENSE.md | ||
# @copyright https://www.fastybird.com | ||
# @author Adam Kadlec <adam.kadlec@fastybird.com> | ||
# @package FastyBird:DateTimeFactory! | ||
# @subpackage config | ||
# @since 0.1.0 | ||
# | ||
# @date 19.07.20 | ||
# @date 19.07.20 | ||
|
||
dateTimeFactory: | ||
timezone: 'Europe/Amsterdam' | ||
timeZone: 'Europe/Amsterdam' |