-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #17.
- Loading branch information
Showing
12 changed files
with
661 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the rybakit/msgpack.php package. | ||
* | ||
* (c) Eugene Leonovich <gen.work@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace MessagePack\Extension; | ||
|
||
use MessagePack\BufferUnpacker; | ||
use MessagePack\Extension; | ||
use MessagePack\Packer; | ||
use MessagePack\Type\Timestamp; | ||
|
||
final class TimestampExtension implements Extension | ||
{ | ||
private const TYPE = -1; | ||
|
||
public function getType() : int | ||
{ | ||
return self::TYPE; | ||
} | ||
|
||
public function pack(Packer $packer, $value) : ?string | ||
{ | ||
if (!$value instanceof Timestamp) { | ||
return null; | ||
} | ||
|
||
$sec = $value->getSeconds(); | ||
$nsec = $value->getNanoseconds(); | ||
|
||
if ($sec >> 34) { | ||
return $packer->packExt(self::TYPE, \pack('NJ', $nsec, $sec)); | ||
} | ||
|
||
return (0 === $nsec && $sec <= 0xffffffff) | ||
? $packer->packExt(self::TYPE, \pack('N', $sec)) | ||
: $packer->packExt(self::TYPE, \pack('J', ($nsec << 34) | $sec)); | ||
} | ||
|
||
/** | ||
* @return Timestamp | ||
*/ | ||
public function unpackExt(BufferUnpacker $unpacker, int $extLength) | ||
{ | ||
if (4 === $extLength) { | ||
$data = $unpacker->read(4); | ||
|
||
$sec = \ord($data[0]) << 24 | ||
| \ord($data[1]) << 16 | ||
| \ord($data[2]) << 8 | ||
| \ord($data[3]); | ||
|
||
return new Timestamp($sec); | ||
} | ||
if (8 === $extLength) { | ||
$data = $unpacker->read(8); | ||
|
||
$num = \unpack('J', $data)[1]; | ||
$nsec = $num >> 34; | ||
if ($nsec < 0) { | ||
$nsec += 0x40000000; | ||
} | ||
|
||
return new Timestamp($num & 0x3ffffffff, $nsec); | ||
} | ||
|
||
$data = $unpacker->read(12); | ||
|
||
$nsec = \ord($data[0]) << 24 | ||
| \ord($data[1]) << 16 | ||
| \ord($data[2]) << 8 | ||
| \ord($data[3]); | ||
|
||
return new Timestamp(\unpack('J', $data, 4)[1], $nsec); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the rybakit/msgpack.php package. | ||
* | ||
* (c) Eugene Leonovich <gen.work@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace MessagePack\Type; | ||
|
||
final class Timestamp | ||
{ | ||
private $seconds; | ||
private $nanoseconds; | ||
|
||
public function __construct(int $seconds, int $nanoseconds = 0) | ||
{ | ||
$this->seconds = $seconds; | ||
$this->nanoseconds = $nanoseconds; | ||
} | ||
|
||
public static function now() : self | ||
{ | ||
$date = new \DateTime(); | ||
|
||
return new self($date->getTimestamp(), (int) $date->format('u') * 1000); | ||
} | ||
|
||
public static function fromDateTime(\DateTimeInterface $date) : self | ||
{ | ||
return new self($date->getTimestamp(), (int) $date->format('u') * 1000); | ||
} | ||
|
||
public function getSeconds() : int | ||
{ | ||
return $this->seconds; | ||
} | ||
|
||
public function getNanoseconds() : int | ||
{ | ||
return $this->nanoseconds; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.