Beispiel #1
0
 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));
 }
Beispiel #2
0
 public function getDateTime()
 {
     if ($this->getVersion() != 1) {
         throw new UnsupportedOperationException('Not a time-based UUID');
     }
     $time = $this->converter->fromHex($this->getTimestampHex());
     $ts = new \Moontoast\Math\BigNumber($time, 20);
     $ts->subtract('122192928000000000');
     $ts->divide('10000000.0');
     $ts->round();
     $unixTime = $ts->getValue();
     return new \DateTime("@{$unixTime}");
 }
Beispiel #3
0
 /**
  * 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');
 }