Debian Codegraph @

vendor/ramsey/uuid/src/Rfc4122/TimeTrait.php

raw
1 <?php
2
3 /**
4 * This file is part of the ramsey/uuid library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12
13 declare(strict_types=1);
14
15 namespace Ramsey\Uuid\Rfc4122;
16
17 use DateTimeImmutable;
18 use DateTimeInterface;
19 use Ramsey\Uuid\Exception\DateTimeException;
20 use Throwable;
21
22 use function str_pad;
23
24 use const STR_PAD_LEFT;
25
26 /**
27 * Provides common functionality for getting the time from a time-based UUID
28 *
29 * @immutable
30 */
31 trait TimeTrait
32 {
33 /**
34 * Returns a DateTimeInterface object representing the timestamp associated with the UUID
35 *
36 * @return DateTimeImmutable A PHP DateTimeImmutable instance representing the timestamp of a time-based UUID
37 */
38 public function getDateTime(): DateTimeInterface
39 {
40 $time = $this->timeConverter->convertTime($this->fields->getTimestamp());
41
42 try {
43 return new DateTimeImmutable(
44 '@'
45 . $time->getSeconds()->toString()
46 . '.'
47 . str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
48 );
49 } catch (Throwable $e) {
50 throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
51 }
52 }
53 }