public function calculateTime($seconds, $microSeconds) { $uuidTime = new \Moontoast\Math\BigNumber('0'); $sec = new \Moontoast\Math\BigNumber($seconds); $sec->multiply('10000000'); $usec = new \Moontoast\Math\BigNumber($microSeconds); $usec->multiply('10'); $uuidTime->add($sec)->add($usec)->add('122192928000000000'); $uuidTimeHex = sprintf('%016s', $uuidTime->convertToBase(16)); return array('low' => substr($uuidTimeHex, 8), 'mid' => substr($uuidTimeHex, 4, 4), 'hi' => substr($uuidTimeHex, 0, 4)); }
/** * Calculates the UUID time fields from a UNIX timestamp * * UUID time is a 60-bit time value as a count of 100-nanosecond intervals * since 00:00:00.00, 15 October 1582. * * @param int $sec Seconds since the Unix Epoch * @param int $usec Microseconds * @return array * @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system * and Moontoast\Math\BigNumber is not present */ protected static function calculateUuidTime($sec, $usec) { if (self::is64BitSystem()) { // 0x01b21dd213814000 is the number of 100-ns intervals between the // UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. $uuidTime = $sec * 10000000 + $usec * 10 + 0x1b21dd213814000; return array('low' => sprintf('%08x', $uuidTime & 0xffffffff), 'mid' => sprintf('%04x', $uuidTime >> 32 & 0xffff), 'hi' => sprintf('%04x', $uuidTime >> 48 & 0xfff)); } if (self::hasBigNumber()) { $uuidTime = new \Moontoast\Math\BigNumber('0'); $sec = new \Moontoast\Math\BigNumber($sec); $sec->multiply('10000000'); $usec = new \Moontoast\Math\BigNumber($usec); $usec->multiply('10'); $uuidTime->add($sec)->add($usec)->add('122192928000000000'); $uuidTimeHex = sprintf('%016s', $uuidTime->convertToBase(16)); return array('low' => substr($uuidTimeHex, 8), 'mid' => substr($uuidTimeHex, 4, 4), 'hi' => substr($uuidTimeHex, 0, 4)); } throw new Exception\UnsatisfiedDependencyException('When calling ' . __METHOD__ . ' on a 32-bit system, ' . 'Moontoast\\Math\\BigNumber must be present in order ' . 'to generate version 1 UUIDs'); }