Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Handle Date objects #36

merged 1 commit into from

3 participants


Fixes #35


Well this is way more useful than doing nothing. I wonder how hard it would be to try and detect a date formatted string when unpacking and go back into a Date object? I've not thought through all of the unintended consequences of doing that. Seems to me that you would have to run date.parse() on every string you find, so that is likely too slow. Just thinking out loud.

One minor improvement would be to call Date.toUTCString() to get a timezone free string, but I am unfamiliar with how one invokes a method on a a JS object using the Handle. That is, I think you get ToString() for free, but may not get ToUTCString(). Oddly enough, a quick google on how one would do that failed me. Feel free to dig deeper, but if it's too much of a PITA, your patch is way better than current behavior.

Anyone else have thoughts?


Thought about the unpacking and came to the same conclusion. JSON parser also leaves it up to the client code to recreate Date objects.

Regarding toString vs toUTCString, the best option is actually toISOString because it preserves the milliseconds:

> var d = new Date
> new Date(d.toString()).getTime()
> new Date(d.toUTCString()).getTime()
> new Date(d.toISOString()).getTime()

I'm pretty new to native extensions and this seemed like the fastest approach to get something working. Will tackle switching to Date.toISOString next


And now with toIsoString

@godsflaw godsflaw merged commit 62cce69 into from

tagged and updated in npm too


This throws compile errors on Windows.

Getting the following error... error C2466: cannot allocate an array of constant size 0 [C:\Users\Sayantan\node_modules\zerorpc\node_modules\msgpack\build\msgpackBinding.vcxproj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 30, 2012
  1. @squamos

    Handle Date object packing

    squamos authored
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 1 deletion.
  1. +1 −1  package.json
  2. +10 −0 src/
  3. +5 −0 test/test.js
2  package.json
@@ -1,7 +1,7 @@
"name": "msgpack",
"description": "A space-efficient object serialization library for node.js",
- "version": "0.1.6",
+ "version": "0.1.7",
"homepage": "",
"author": "Peter Griess <>",
"contributors": [
10 src/
@@ -174,6 +174,16 @@ v8_to_msgpack(Handle<Value> v8obj, msgpack_object *mo, msgpack_zone *mz,
mo->via.raw.ptr = (char*) msgpack_zone_malloc(mz, mo->via.raw.size);
DecodeWrite((char*) mo->via.raw.ptr, mo->via.raw.size, v8obj, UTF8);
+ } else if (v8obj->IsDate()) {
+ mo->type = MSGPACK_OBJECT_RAW;
+ Handle<Date> date = Handle<Date>::Cast(v8obj);
+ Handle<Function> func = Handle<Function>::Cast(date->Get(String::New("toISOString")));
+ Handle<Value> argv[0] = {};
+ Handle<Value> result = func->Call(date, 0, argv);
+ mo->via.raw.size = static_cast<uint32_t>(DecodeBytes(result, UTF8));
+ mo->via.raw.ptr = (char*) msgpack_zone_malloc(mz, mo->via.raw.size);
+ DecodeWrite((char*) mo->via.raw.ptr, mo->via.raw.size, result, UTF8);
} else if (v8obj->IsArray()) {
Local<Object> o = v8obj->ToObject();
Local<Array> a = Local<Array>::Cast(o);
5 test/test.js
@@ -30,6 +30,11 @@ testEqual([1, 2, 3]);
testEqual([1, 'abc', false, null]);
testEqual({'a' : [1, 2, 3], 'b' : 'cdef', 'c' : {'nuts' : 'qqq'}});
+// Make sure dates are handled properly
+var date = new Date();
+var dateWrapper = {d: date};
+assert.deepEqual({d: date.toISOString()}, msgpack.unpack(msgpack.pack(dateWrapper)));
// Make sure we're catching circular references for arrays
var a = [1, 2, 3, 4];
Something went wrong with that request. Please try again.