|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @project Castor UUID |
| 7 | + * @link https://github.com/castor-labs/php-lib-uuid |
| 8 | + * @package castor/uuid |
| 9 | + * @author Matias Navarro-Carter [email protected] |
| 10 | + * @license MIT |
| 11 | + * @copyright 2024 CastorLabs Ltd |
| 12 | + * |
| 13 | + * For the full copyright and license information, please view the LICENSE |
| 14 | + * file that was distributed with this source code. |
| 15 | + */ |
| 16 | + |
| 17 | +namespace Castor\Uuid\System\Time; |
| 18 | + |
| 19 | +use Brick\DateTime\Clock; |
| 20 | +use Brick\DateTime\Instant; |
| 21 | +use Brick\Math\BigInteger; |
| 22 | +use Brick\Math\RoundingMode; |
| 23 | +use Castor\Bytes; |
| 24 | +use Castor\Uuid\System\Time; |
| 25 | + |
| 26 | +use function Castor\Err\must; |
| 27 | + |
| 28 | +/** |
| 29 | + * Represents the time as the number of 100-nanosecond intervals since the gregorian epoch. |
| 30 | + */ |
| 31 | +readonly class Gregorian implements Time |
| 32 | +{ |
| 33 | + /** |
| 34 | + * The number of 100-nanosecond intervals from the Gregorian calendar epoch |
| 35 | + * to the Unix epoch. |
| 36 | + */ |
| 37 | + private const string GREGORIAN_TO_UNIX_OFFSET = '122192928000000000'; |
| 38 | + |
| 39 | + /** |
| 40 | + * The number of 100-nanosecond intervals in one second. |
| 41 | + */ |
| 42 | + private const string SECOND_INTERVALS = '10000000'; |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + public Bytes $bytes, |
| 46 | + ) { |
| 47 | + if ($this->bytes->len() !== 8) { |
| 48 | + throw new \InvalidArgumentException('Gregorian time must be 64 bits long'); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static function fromTimestamp(BigInteger $timestamp): self |
| 53 | + { |
| 54 | + return must(static function () use ($timestamp) { |
| 55 | + $hex = \str_pad($timestamp->toBase(16), 16, '0', STR_PAD_LEFT); |
| 56 | + |
| 57 | + return new self(Bytes::fromHex($hex)); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + public static function fromInstant(Instant $instant): self |
| 62 | + { |
| 63 | + return must(function () use ($instant) { |
| 64 | + $epochSeconds = BigInteger::of($instant->getEpochSecond()); |
| 65 | + $nanoSeconds = BigInteger::of($instant->getNano()); |
| 66 | + |
| 67 | + $secondsTicks = $epochSeconds->multipliedBy(self::SECOND_INTERVALS); |
| 68 | + $nanoTicks = $nanoSeconds->dividedBy(100, RoundingMode::DOWN); |
| 69 | + $ticksSinceEpoch = $secondsTicks->plus($nanoTicks); |
| 70 | + |
| 71 | + return self::fromTimestamp($ticksSinceEpoch->plus(self::GREGORIAN_TO_UNIX_OFFSET)); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + public static function now(Clock $clock): self |
| 76 | + { |
| 77 | + return self::fromInstant($clock->getTime()); |
| 78 | + } |
| 79 | + |
| 80 | + public function getInstant(): Instant |
| 81 | + { |
| 82 | + return must(function () { |
| 83 | + $ticksSinceEpoch = $this->getTimestamp()->minus(self::GREGORIAN_TO_UNIX_OFFSET); // Subtract gregorian offset |
| 84 | + |
| 85 | + $epochSeconds = $ticksSinceEpoch->dividedBy(self::SECOND_INTERVALS, RoundingMode::DOWN); |
| 86 | + $nanoSeconds = $ticksSinceEpoch->remainder(self::SECOND_INTERVALS)->multipliedBy(100); |
| 87 | + |
| 88 | + return Instant::of($epochSeconds->toInt(), $nanoSeconds->toInt()); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the number of 100 nanosecond intervals since 1582-10-15 00:00:00 UTC as a numeric string. |
| 94 | + */ |
| 95 | + public function getTimestamp(): BigInteger |
| 96 | + { |
| 97 | + return must(fn () => BigInteger::fromBase($this->bytes->toHex(), 16)); |
| 98 | + } |
| 99 | +} |
0 commit comments