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

Breadcrumb

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

class UnixTimeConverter

UnixTimeConverter converts Unix Epoch timestamps to/from hexadecimal values consisting of milliseconds elapsed since the Unix Epoch

@psalm-immutable

Hierarchy

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

Expanded class hierarchy of UnixTimeConverter

1 file declares its use of UnixTimeConverter
UuidBuilder.php in vendor/ramsey/uuid/src/Rfc4122/UuidBuilder.php

File

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

Namespace

Ramsey\Uuid\Converter\Time
View source
class UnixTimeConverter implements TimeConverterInterface {
    private const MILLISECONDS = 1000;
    public function __construct(CalculatorInterface $calculator) {
    }
    public function calculateTime(string $seconds, string $microseconds) : Hexadecimal {
        $timestamp = new Time($seconds, $microseconds);
        // Convert the seconds into milliseconds.
        $sec = $this->calculator
            ->multiply($timestamp->getSeconds(), new IntegerObject(self::MILLISECONDS));
        // Convert the microseconds into milliseconds; the scale is zero because
        // we need to discard the fractional part.
        $usec = $this->calculator
            ->divide(RoundingMode::DOWN, 0, $timestamp->getMicroseconds(), new IntegerObject(self::MILLISECONDS));
        
        /** @var IntegerObject $unixTime */
        $unixTime = $this->calculator
            ->add($sec, $usec);
        $unixTimeHex = str_pad($this->calculator
            ->toHexadecimal($unixTime)
            ->toString(), 12, '0', STR_PAD_LEFT);
        return new Hexadecimal($unixTimeHex);
    }
    public function convertTime(Hexadecimal $uuidTimestamp) : Time {
        $milliseconds = $this->calculator
            ->toInteger($uuidTimestamp);
        $unixTimestamp = $this->calculator
            ->divide(RoundingMode::HALF_UP, 6, $milliseconds, new IntegerObject(self::MILLISECONDS));
        $split = explode('.', (string) $unixTimestamp, 2);
        return new Time($split[0], $split[1] ?? '0');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
UnixTimeConverter::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
UnixTimeConverter::convertTime public function Converts a timestamp extracted from a UUID to a Unix timestamp Overrides TimeConverterInterface::convertTime
UnixTimeConverter::MILLISECONDS private constant
UnixTimeConverter::__construct public function

API Navigation

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