-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for the timestamp extension #17
Comments
Looks like this is still outstanding. I tried sending a serialized
I wasn't able to get a type transformer to work either. The class DateTimeTransformer implements \MessagePack\TypeTransformer\Unpackable
{
private $type;
public function __construct(int $type)
{
$this->type = $type;
}
public function getType() : int
{
return $this->type;
}
public function unpack(\MessagePack\BufferUnpacker $unpacker, int $extLength)
{
//how does one unpack a timestamp?
return new \DateTimeImmutable($unpacker->unpack???());
}
}
$transformer = new DateTimeTransformer(-1);
$unpacker = new \MessagePack\BufferUnpacker();
$unpacker->registerTransformer($transformer);
$data = $unpacker->reset($packed)->unpack(); |
@doc987 I took msgpack-lite library as an example from your other issue (msgpack/msgpack-php#127). As you noticed, it has native support for namespace App\MessagePack;
use MessagePack\BufferUnpacker;
use MessagePack\TypeTransformer\Unpackable;
final class MsgpackLiteJsDateTransformer implements Unpackable
{
public function getType() : int
{
return 0x0d;
}
public function unpack(BufferUnpacker $unpacker, int $extLength) : \DateTimeImmutable
{
return \DateTimeImmutable::createFromFormat('U.u', $unpacker->unpackFloat() / 1000);
}
} Test: namespace App\MessagePack;
use MessagePack\BufferUnpacker;
// msgpack.encode(new Date('2018-08-07T22:21:45.580Z')) = c7090dcb42765167b52ec000
$packedJsDate = hex2bin('c7090dcb42765167b52ec000');
$unpacker = new BufferUnpacker($packedJsDate);
$unpacker->registerTransformer(new MsgpackLiteJsDateTransformer());
var_dump($unpacker->unpack());
/*
class DateTimeImmutable#4 (3) {
public $date =>
string(26) "2018-08-07 22:21:45.580000"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+00:00"
}
*/ |
Ref: https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
The text was updated successfully, but these errors were encountered: