Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. GenericTimeConverter.php

class GenericTimeConverter

GenericTimeConverter uses the provided calculator to calculate and convert time values

@psalm-immutable

Hierarchy

  • class \Ramsey\Uuid\Converter\Time\GenericTimeConverter implements \Ramsey\Uuid\Converter\TimeConverterInterface

Expanded class hierarchy of GenericTimeConverter

2 files declare their use of GenericTimeConverter
BuilderCollection.php in vendor/ramsey/uuid/src/Builder/BuilderCollection.php
FeatureSet.php in vendor/ramsey/uuid/src/FeatureSet.php

File

vendor/ramsey/uuid/src/Converter/Time/GenericTimeConverter.php, line 35

Namespace

Ramsey\Uuid\Converter\Time
View source
class GenericTimeConverter implements TimeConverterInterface {
    
    /**
     * The number of 100-nanosecond intervals from the Gregorian calendar epoch
     * to the Unix epoch.
     */
    private const GREGORIAN_TO_UNIX_INTERVALS = '122192928000000000';
    
    /**
     * The number of 100-nanosecond intervals in one second.
     */
    private const SECOND_INTERVALS = '10000000';
    
    /**
     * The number of 100-nanosecond intervals in one microsecond.
     */
    private const MICROSECOND_INTERVALS = '10';
    public function __construct(CalculatorInterface $calculator) {
    }
    public function calculateTime(string $seconds, string $microseconds) : Hexadecimal {
        $timestamp = new Time($seconds, $microseconds);
        // Convert the seconds into a count of 100-nanosecond intervals.
        $sec = $this->calculator
            ->multiply($timestamp->getSeconds(), new IntegerObject(self::SECOND_INTERVALS));
        // Convert the microseconds into a count of 100-nanosecond intervals.
        $usec = $this->calculator
            ->multiply($timestamp->getMicroseconds(), new IntegerObject(self::MICROSECOND_INTERVALS));
        // Combine the seconds and microseconds intervals and add the count of
        // 100-nanosecond intervals from the Gregorian calendar epoch to the
        // Unix epoch. This gives us the correct count of 100-nanosecond
        // intervals since the Gregorian calendar epoch for the given seconds
        // and microseconds.
        
        /** @var IntegerObject $uuidTime */
        $uuidTime = $this->calculator
            ->add($sec, $usec, new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS));
        $uuidTimeHex = str_pad($this->calculator
            ->toHexadecimal($uuidTime)
            ->toString(), 16, '0', STR_PAD_LEFT);
        return new Hexadecimal($uuidTimeHex);
    }
    public function convertTime(Hexadecimal $uuidTimestamp) : Time {
        // From the total, subtract the number of 100-nanosecond intervals from
        // the Gregorian calendar epoch to the Unix epoch. This gives us the
        // number of 100-nanosecond intervals from the Unix epoch, which also
        // includes the microtime.
        $epochNanoseconds = $this->calculator
            ->subtract($this->calculator
            ->toInteger($uuidTimestamp), new IntegerObject(self::GREGORIAN_TO_UNIX_INTERVALS));
        // Convert the 100-nanosecond intervals into seconds and microseconds.
        $unixTimestamp = $this->calculator
            ->divide(RoundingMode::HALF_UP, 6, $epochNanoseconds, new IntegerObject(self::SECOND_INTERVALS));
        $split = explode('.', (string) $unixTimestamp, 2);
        return new Time($split[0], $split[1] ?? 0);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
GenericTimeConverter::calculateTime public function Uses the provided seconds and micro-seconds to calculate the count of
100-nanosecond intervals since UTC 00:00:00.00, 15 October 1582, for
RFC 4122 variant UUIDs
Overrides TimeConverterInterface::calculateTime
GenericTimeConverter::convertTime public function Converts a timestamp extracted from a UUID to a Unix timestamp Overrides TimeConverterInterface::convertTime
GenericTimeConverter::GREGORIAN_TO_UNIX_INTERVALS private constant The number of 100-nanosecond intervals from the Gregorian calendar epoch
to the Unix epoch.
GenericTimeConverter::MICROSECOND_INTERVALS private constant The number of 100-nanosecond intervals in one microsecond.
GenericTimeConverter::SECOND_INTERVALS private constant The number of 100-nanosecond intervals in one second.
GenericTimeConverter::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal