Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 <ben@benramsey.com>
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
 * @psalm-immutable
30
 */
31
trait TimeTrait
32
{
33
    /**
34
     * Returns a DateTimeInterface object representing the timestamp associated
35
     * with the UUID
36
     *
37
     * @return DateTimeImmutable A PHP DateTimeImmutable instance representing
38
     *     the timestamp of a time-based UUID
39
     */
40
    public function getDateTime(): DateTimeInterface
41
    {
42
        $time = $this->timeConverter->convertTime($this->fields->getTimestamp());
43
 
44
        try {
45
            return new DateTimeImmutable(
46
                '@'
47
                . $time->getSeconds()->toString()
48
                . '.'
49
                . str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
50
            );
51
        } catch (Throwable $e) {
52
            throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
53
        }
54
    }
55
}