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

Breadcrumb

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

function PhpTimeConverter::splitTime

Parameters

float|int $time The time to split into seconds and microseconds:

Return value

string[]

1 call to PhpTimeConverter::splitTime()
PhpTimeConverter::convertTime in vendor/ramsey/uuid/src/Converter/Time/PhpTimeConverter.php
Converts a timestamp extracted from a UUID to a Unix timestamp

File

vendor/ramsey/uuid/src/Converter/Time/PhpTimeConverter.php, line 128

Class

PhpTimeConverter
PhpTimeConverter uses built-in PHP functions and standard math operations available to the PHP programming language to provide facilities for converting parts of time into representations that may be used in UUIDs

Namespace

Ramsey\Uuid\Converter\Time

Code

private function splitTime(float|int $time) : array {
    $split = explode('.', (string) $time, 2);
    // If the $time value is a float but $split only has 1 element, then the
    // float math was rounded up to the next second, so we want to return
    // an empty array to allow use of the fallback converter.
    if (is_float($time) && count($split) === 1) {
        return [];
    }
    if (count($split) === 1) {
        return [
            'sec' => $split[0],
            'usec' => '0',
        ];
    }
    // If the microseconds are less than six characters AND the length of
    // the number is greater than or equal to the PHP precision, then it's
    // possible that we lost some precision for the microseconds. Return an
    // empty array, so that we can choose to use the fallback converter.
    if (strlen($split[1]) < 6 && strlen((string) $time) >= $this->phpPrecision) {
        return [];
    }
    $microseconds = $split[1];
    // Ensure the microseconds are no longer than 6 digits. If they are,
    // truncate the number to the first 6 digits and round up, if needed.
    if (strlen($microseconds) > 6) {
        $roundingDigit = (int) substr($microseconds, 6, 1);
        $microseconds = (int) substr($microseconds, 0, 6);
        if ($roundingDigit >= 5) {
            $microseconds++;
        }
    }
    return [
        'sec' => $split[0],
        'usec' => str_pad((string) $microseconds, 6, '0', STR_PAD_RIGHT),
    ];
}

API Navigation

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